/* 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.events.FormDataManagerEvent; import com.yahoo.astra.fl.containers.Form; import com.yahoo.astra.fl.utils.FlValueParser; import com.yahoo.astra.managers.FormDataManager; import flash.display.Sprite; public class SimpleFormToBegin extends Sprite { //-------------------------------------- // Properties //-------------------------------------- private var myForm : Form; private var nameInput : TextInput; private var emailInput : TextInput; private var websiteInput : TextInput; private var addressInputLine1 : TextInput; private var addressInputLine2 : TextInput; private var zipcodeInput : TextInput; private var sampleCheckBox : CheckBox; private var stateComboBox : ComboBox; private var commentInput : TextArea; private var collectedDataOutput : TextArea; private var submitButton : Button; //-------------------------------------- // Constructor //-------------------------------------- public function SimpleFormToBegin() { 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://"; sampleCheckBox = new CheckBox(); sampleCheckBox.label = "Send me a sample."; sampleCheckBox.width = 250; 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"; commentInput = new TextArea(); commentInput.width = 200; commentInput.height = 100; submitButton = new Button(); submitButton.label = "SUBMIT"; } private function initForm() : void { // Init Form with FormHeading(String); myForm = new Form("Contact Us"); // Decide your form size myForm.autoSize = true; //or set size like... myForm.setSize(400,300) // Attach Form on the stage. this.addChild(myForm); } private function initData() : void { // Init FormDataManager to Form to collect user input data. var formDataManager : FormDataManager = new FormDataManager(FlValueParser); myForm.formDataManager = formDataManager; // Attach a trigger on a button(or any Display object) to collect and to validate user inputs. formDataManager.addTrigger(submitButton, handlerDataCollectionSuccess, handlerDataCollectionFail); // Init validator to be used. // See AdobeDataValidation.SWC from library. // Also, see the details at http://code.google.com/p/flash-validators var validator : AdobeDataValidation = 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:sampleCheckBox, id:"samplerequest", source:sampleCheckBox}, {label:"Address", items:[addressInputLine1,addressInputLine2], itemAlign:"vertical", id:"address", source:[addressInputLine1,"\n",addressInputLine2]}, {label:"", items:[stateComboBox,"zip", zipcodeInput], id:["state","zip"], source:[stateComboBox,zipcodeInput]}, {label:"Message", items:commentInput, required:true, id:"message", source:commentInput, validator:validator.isNotEmpty}, {label:"", items:submitButton}]; myForm.dataSource = myFormDataArr; } private function handlerDataCollectionSuccess(e : FormDataManagerEvent) : void { submitButton.enabled = false; // Let's collect what is in the collectedData. var resultTxt : String = "*** Collected Data *** \n\n"; for (var i:String in FormDataManager.collectedData) { resultTxt += i + " : " + FormDataManager.collectedData[i].toString() + " \n"; } // And show it in a textArea. if(!collectedDataOutput) this.addChild(addCollectedDataOutput()); collectedDataOutput.text = resultTxt; } private function handlerDataCollectionFail(e : FormDataManagerEvent) : void { // Let's collect error messages is in the failedData. var resultTxt : String = "### Errors in your form ### \n\n"; for (var i:String in FormDataManager.failedData) { resultTxt += i + " : " + FormDataManager.failedData[i] + " \n"; } // And show it in a textArea. if(!collectedDataOutput) this.addChild(addCollectedDataOutput()); collectedDataOutput.text = resultTxt; } private function addCollectedDataOutput() : TextArea { collectedDataOutput = new TextArea(); collectedDataOutput.width = 400; collectedDataOutput.height = 100; collectedDataOutput.editable = false; if(myForm) collectedDataOutput.y = myForm.height + 30; return collectedDataOutput; } private function statesComboBox() : ComboBox { var items : XML = ; var dp : DataProvider = new DataProvider(items); var comboBox : ComboBox = new ComboBox(); comboBox.dataProvider = dp; return comboBox; } } }