-
29 Jan 2013 1:15 AM #1
Unanswered: ST 2.1 - Custom form validation per field
Unanswered: ST 2.1 - Custom form validation per field
Hello,
I've got 2 forms in my app:
- Login form (email, password)
- Registration fom (name, surname, email, password)
I'm using model for validation:
After submitting Login form I get an error concerning field that are not in the form (name, surname). Is it possible to rewrite presence rule so it won't be applied when there is no such field? Presence rule should be disabled in Login form, but not in Registration form.Code:Ext.define('First.model.User', { extend : 'Ext.data.Model', config : { fields : [{ name: 'id', type: 'integer' }, { name: 'name', type: 'string' }, { name: 'surname', type: 'string' }, { name : 'email', type : 'string' }, { name : 'password', type : 'password' }], validations : [{ type : 'presence', name : 'name', message : 'Enter name' },{ type : 'presence', name : 'surname', message : 'Enter surname' },{ type : 'presence', name : 'email', message : "Enter email" }, { type : 'presence', name : 'password', message : "Enter Password" }, { type : 'format', name : 'email', matcher : /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/, message : 'Email format is invalid' }] } });
I've tried to make custom presence rule in Login form controller, but I've got no idea how to check field presence in form.
Thanks for help in advance
-
30 Jan 2013 4:51 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,655
- Vote Rating
- 435
- Answers
- 3107
No but you can choose to ignore the error you get back depending on how you are submitting and handling it. Not really a presence validation if you don't require the presence.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
30 Jan 2013 11:38 PM #3
Application listens confirm button and after tapping it validates and submit the form (or shows errors). Handler for this action is placed in Controller. This is the handler:
Code:// Checks form and log in btn_confirmLogin : function() { var me = this; var model = Ext.ModelMgr.create(me.getForm_loginForm().getValues(), 'First.model.User'); // Validate var errors = model.validate(); var message = ''; if (errors.isValid()) { // Validation successful - show loader Ext.Viewport.setMasked({ xtype : 'loadmask', message : 'Logging in...' }); // Login me.getForm_loginForm().submit({ url : 'http://localhost/first/?do=login', method : 'POST', success : function(form, result) { // Save user var user = Ext.create('First.model.User', { id : result.user.id, name : result.user.sFirstname, surname : result.user.sLastname, email : result.user.sEmail }); // Save user to store var users = Ext.create('First.store.Users'); // var users = Ext.getStore('usersStore'); users.add(user); // Loader off Ext.Viewport.setMasked(false); // Move to homepage me.push(me.getNvw_main(), 'First.view.HomePage'); }, failure : function(form, result) { // Loader off Ext.Viewport.setMasked(false); Ext.Msg.alert("Login Failed", result.error); } }); } else { // Validation failed var data = ""; errors.each(function(item, index, length) { data += item.getMessage() + '<br />'; }); Ext.Msg.alert("Validation Failed", data); return false; } }


Reply With Quote