dojo.require("dojo.parser"); dojo.require("dijit.Dialog"); dojo.require("dijit.form.Form"); dojo.require("dijit.form.ValidationTextBox"); dojo.require("dijit.form.CheckBox"); dojo.require("dijit.form.RadioButton"); dojo.require("dijit.form.Select"); dojo.require("dijit.form.Button"); dojo.require("dojox.form.PasswordValidator"); dojo.require("dojox.form.manager._Mixin"); dojo.require("dojox.form.manager._NodeMixin"); dojo.require("dojox.form.manager._EnableMixin"); dojo.require("dojox.form.manager._ValueMixin"); dojo.require("dojox.form.manager._DisplayMixin"); /* Disabled for JSFiddle. dojo.requireLocalization("my", "SignupForm"); */ dojo.ready(function() { function submitForm(data) { var dfd = new dojo.Deferred(); console.log("Submitting data", data); setTimeout(dojo.hitch(dfd, "resolve"), 2000); return dfd.promise; } var i18n = /* Disabled for JSFiddle. dojo.i18n.getLocalization("my", "SignupForm"); */ { username: "Username", usernameRules: "Username must be letters and numbers only, and at least 4 characters long", password: "Password", verifyPassword: "Verify password", agreeTerms: "I agree to the T&Cs", newsletter: "Newsletter?", yes: "Yes", no: "No", frequency: "Frequency", daily: "Daily", weekly: "Weekly", monthly: "Monthly", signUp: "Sign up", pleaseWait: "Please wait…", success: "It worked! Yay!", ok: "OK!" }; dojo.declare("my.SignupForm", [dijit.form.Form, dojox.form.manager._Mixin, dojox.form.manager._NodeMixin, dojox.form.manager._EnableMixin, dojox.form.manager._ValueMixin, dojox.form.manager._DisplayMixin], { templateString: dojo.byId("SignupFormTemplate").innerHTML, i18n: i18n, postCreate: function(){ this.watch("state", this.checkValid); this.inherited(arguments); }, startup: function(){ if(this.dialog){ new dijit.form.Button({ type: "button", label: this.i18n.ok, onClick: dojo.hitch(this, function(){ this.dialog.hide(); }) }).placeAt(this.successNode); } this.inherited(arguments); }, checkValid: function(){ this.enable({ submitForm: this.isValid() && this.elementValue("agree") }); }, checkNewsletter: function(value){ this.show({ newsletterFrequency: value === "1" }); this.dialog && this.dialog.layout(); }, onSubmit: function(evt) { evt.preventDefault(); submitForm(this.get("value")).then(dojo.hitch(this, function(data) { this.show({ spinnerNode: false, successNode: true }); this.dialog && this.dialog.layout(); }), dojo.hitch(this, function(err) { this.show({ spinnerNode: false, formNode: true }); this.dialog && this.dialog.layout(); })); this.show({ spinnerNode: true, formNode: false }); this.dialog && this.dialog.layout(); } }); app = { dialog: new dijit.Dialog().placeAt(document.body), showSignupForm: function() { this.dialog.set({ title: i18n.signUp, content: new my.SignupForm({ dialog: this.dialog }) }); this.dialog.show(); } }; dojo.parser.parse(); });
<script id="SignupFormTemplate" type="text/html"> <form dojoAttachPoint="containerNode" dojoAttachEvent="onreset:_onReset,onsubmit:_onSubmit" ${!nameAttrSetting}> <div dojoAttachPoint="formNode"> <div>${i18n.username}: <input dojoType="dijit.form.ValidationTextBox" name="username" required regExp="^[A-Za-z0-9]{4,}$" promptMessage="${i18n.usernameRules}"></div> <div dojoType="dojox.form.PasswordValidator" name="password"> <div>${i18n.password}: <input type="password" pwType="new"></div> <div>${i18n.verifyPassword}: <input type="password" pwType="verify"></div> </div> <div><label><input dojoType="dijit.form.CheckBox" name="agree" observer="checkValid"> ${i18n.agreeTerms}</label></div> <fieldset> <legend>${i18n.newsletter}</legend> <label class="i"><input dojoType="dijit.form.RadioButton" name="newsletter" value="1" observer="checkNewsletter"> ${i18n.yes}</label> <label class="i"><input dojoType="dijit.form.RadioButton" name="newsletter" value="0" checked observer="checkNewsletter"> ${i18n.no}</label> <div dojoAttachPoint="newsletterFrequency" style="display:none"> ${i18n.frequency}: <select dojoType="dijit.form.Select" name="frequency"> <option value="daily">${i18n.daily}</option> <option value="weekly">${i18n.weekly}</option> <option value="monthly">${i18n.monthly}</option> </select> </div> </fieldset> <input dojoType="dijit.form.Button" name="submitForm" type="submit" label="${i18n.signUp}" disabled> </div> <div dojoAttachPoint="spinnerNode" style="display:none">${i18n.pleaseWait}</div> <div dojoAttachPoint="successNode" style="display:none">${i18n.success}</div> </form> </script> <input dojoType="dijit.form.Button" type="button" label="Sign up now" onclick="app.showSignupForm()">