/* Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license */ package { import fl.controls.Button; import fl.controls.CheckBox; import fl.controls.ComboBox; import fl.controls.TextArea; import fl.controls.TextInput; import fl.data.DataProvider; import com.yahoo.astra.containers.formClasses.FormItem; import com.yahoo.astra.containers.formClasses.FormLayoutStyle; import com.yahoo.astra.events.FormDataManagerEvent; import com.yahoo.astra.fl.containers.Form; import com.yahoo.astra.fl.managers.AlertManager; import com.yahoo.astra.fl.utils.FlValueParser; import com.yahoo.astra.layout.LayoutContainer; import com.yahoo.astra.layout.modes.BoxLayout; import com.yahoo.astra.managers.FormDataManager; import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.MouseEvent; public class SimpleForm extends Sprite { //-------------------------------------- // Properties //-------------------------------------- private var myForm : Form; private var formDataManager : FormDataManager; // See the Library for AdobeDataValidation.swc. private var validator : AdobeDataValidation; private var nameInput : TextInput; private var emailInput : TextInput; private var addressInputLine1 : TextInput; private var addressInputLine2 : TextInput; private var stateComboBox : ComboBox; private var zipcodeInput : TextInput; private var sendMeSampleItemcontainer : LayoutContainer; private var addressFormItem : FormItem; private var stateZipFormItem : FormItem; private var sampleCheckBox : CheckBox; private var termsAndConditoinCheckBox : CheckBox; private var websiteInput : TextInput; private var commentInput : TextArea; private var submitButton : Button; //-------------------------------------- // Constructor //-------------------------------------- public function SimpleForm() { super(); initUI(); initForm(); initData(); } private function initUI() : void { nameInput = new TextInput(); nameInput.width = 200; emailInput = new TextInput(); emailInput.width = 200; websiteInput = new TextInput(); websiteInput.width = 200; websiteInput.text = "http://"; // Request sample field will be nested by LayoutContainer in order to add/remove address field. sampleCheckBox = new CheckBox(); sampleCheckBox.label = "Send me a sample."; sampleCheckBox.width = 250; sampleCheckBox.addEventListener(MouseEvent.CLICK, handlerSampleCheckBox); var mode : BoxLayout = new BoxLayout(); mode.direction = "vertical"; sendMeSampleItemcontainer = new LayoutContainer(mode); sendMeSampleItemcontainer.addChild(sampleCheckBox); addressInputLine1 = new TextInput(); addressInputLine2 = new TextInput(); addressInputLine1.width = addressInputLine2.width = 200; stateComboBox = statesComboBox(); zipcodeInput = new TextInput(); zipcodeInput.width = 40; zipcodeInput.maxChars = 6; zipcodeInput.restrict = "0-9"; addressFormItem = new FormItem("Shipping address", addressInputLine1, addressInputLine2); addressFormItem.labelAlign = FormLayoutStyle.TOP; addressFormItem.itemAlign = FormLayoutStyle.VERTICAL; addressFormItem.required = true; addressFormItem.showErrorMessageText = true; stateZipFormItem = new FormItem(stateComboBox, "zipcode", zipcodeInput); stateZipFormItem.indicatorLocation = FormLayoutStyle.RIGHT; stateZipFormItem.required = true; stateZipFormItem.showErrorMessageText = true; commentInput = new TextArea(); commentInput.width = 200; commentInput.height = 100; termsAndConditoinCheckBox = new CheckBox(); termsAndConditoinCheckBox.label = "I Agree to the Terms & Conditions"; termsAndConditoinCheckBox.width = 200; submitButton = new Button(); submitButton.label = "SUBMIT"; } private function initForm() : void { // Init Form with FormHeading(String); myForm = new Form("Contact Us"); // Decide your form size //autoSize or set size like... myForm.setSize(400,300); myForm.autoSize = true; myForm.subFormHeading("* is required field."); myForm.paddingLeft = myForm.paddingRight = myForm.paddingTop = myForm.paddingBottom = 20; myForm.showErrorMessageText = true; myForm.setStyle("skin", "FormSkin"); // Attach Form on the stage. this.addChild(myForm); } private function initData() : void { // Init FormDataManager to Form to collect user input data. formDataManager = new FormDataManager(FlValueParser); // Init functions to be called by VALIDATION_PASSED and VALIDATION_FAILED event. formDataManager.functionValidationPassed = handlerValidationPassed; formDataManager.functionValidationFailed = handlerValidationFaileded; // Attach a trigger on a button(or any Display object) to collect and to validate user inputs. formDataManager.addTrigger(submitButton, handlerDataCollectionSuccess, handlerDataCollectionFail); myForm.formDataManager = formDataManager; // Init validator to be used. // In this Example, we will use SWC of AdobeDataValidation. // See the details from http://code.google.com/p/flash-validators validator = new AdobeDataValidation(); // Init data Array to set dataSource. var myFormDataArr : Array = [{label:"Name", items:nameInput, id:"Name", source:nameInput}, {label:"Email", items:emailInput, id:"Email", instructionText :"Your email address will not be saved", source:emailInput, required:true, validator:validator.isEmail}, {label:"Website", items:websiteInput, id:"Website", source:websiteInput}, {label:"", items:sendMeSampleItemcontainer, id:"samplerequest", source:sampleCheckBox}, {label:"Message", items:commentInput, required:true, id:"Message", source:commentInput, validator:validator.isNotEmpty}, {label:"", items:termsAndConditoinCheckBox, required:true, id:"termsandcondition", source:termsAndConditoinCheckBox, instructionText:"Please read terms and conditions.", errorString:"You need to agree to the terms and conditions", validator:isThisTrue}, {label:"", items:submitButton}]; myForm.dataSource = myFormDataArr; } // return boolean value of the result. private function isThisTrue(str : Object) : Boolean { return str as Boolean; } private function handlerSampleCheckBox(e : MouseEvent) : void { var cb : CheckBox = CheckBox(e.target); if(cb.selected) { sendMeSampleItemcontainer.addChild(addressFormItem); sendMeSampleItemcontainer.addChild(stateZipFormItem); //addItem(id : String, source : Object, property : Object = null , required : Boolean = false , validation : Function = null, validatorExtraParam : Object = null,eventTargetObj : DisplayObject = null,functionValidationPassed : Function = null,functionValidationFailed : Function = null, errorString : String = null) formDataManager.addItem("address", [addressInputLine1,"\n", addressInputLine2], null, true, validator.isNotEmpty, null, addressFormItem); formDataManager.addItem("state", stateComboBox, "abbreviation"); formDataManager.addItem("zip", zipcodeInput, null, true, validator.isZip, null, stateZipFormItem); } else { sendMeSampleItemcontainer.removeChild(addressFormItem); sendMeSampleItemcontainer.removeChild(stateZipFormItem); formDataManager.removeItem("address"); formDataManager.removeItem("state"); formDataManager.removeItem("zip"); } } // This will be called when each FormItem receives FormDataManagerEvent.VALIDATION_PASSED private function handlerValidationPassed(e : FormDataManagerEvent) : void { if(e.target is FormItem) { var requiredIndicator : DisplayObject = (e.target as FormItem).requiredIndicator; if(requiredIndicator) requiredIndicator.visible = false; } } // This will be called when each FormItem receives FormDataManagerEvent.VALIDATION_FAILED private function handlerValidationFaileded(e : FormDataManagerEvent) : void { if(e.target is FormItem) { var requiredIndicator : DisplayObject = (e.target as FormItem).requiredIndicator; if(requiredIndicator) requiredIndicator.visible = true; } } // This will be called when all the required fields passed validation(FormDataManagerEvent.DATACOLLECTION_SUCCESS). private function handlerDataCollectionSuccess(e : FormDataManagerEvent) : void { submitButton.enabled = false; // Let's collect what is in the collectedData. var resultTxt : String = ""; for (var i:String in FormDataManager.collectedData) { resultTxt += i + " : " + FormDataManager.collectedData[i] + " \n \n"; } AlertManager.createAlert(this, resultTxt, "Collected Data from your form."); } // This will be called when there is any invalid required field(FormDataManagerEvent.DATACOLLECTION_FAIL). private function handlerDataCollectionFail(e : FormDataManagerEvent) : void { // Let's collect error messages is in the failedData. var resultTxt : String = ""; for (var i:String in FormDataManager.failedData) { var field : String = (i == "termsandcondition") ? "Terms & Condition" : i; resultTxt += field + " : " + FormDataManager.failedData[i] + " \n \n"; } AlertManager.createAlert(this, resultTxt, "Errors in your form."); } private function statesComboBox() : ComboBox { var items : XML = ; var dp : DataProvider = new DataProvider(items); var comboBox : ComboBox = new ComboBox(); comboBox.dataProvider = dp; return comboBox; } } }