-
2 Feb 2012 7:50 AM #1
Answered: model validation will always pass, even when input is wrong
Answered: model validation will always pass, even when input is wrong
I'm a bit new to sencha touch but I have the following model:
in the handler of a button I have the following:PHP Code:Ext.define("Contact", {
extend: "Ext.data.Model",
fields: [
{name: 'inp_name', type: 'string'},
{name: 'inp_email', type: 'string'},
{name: 'txt_message',type: 'string'}
],
validation: [
{type: 'presence', name: 'inp_name', error: 'Name must be present'},
{type: 'length', name: 'inp_name', min: 5},
{type: 'presence', name: 'inp_email', error: 'Email must be present'},
{type: 'format', name: 'inp_email', matcher: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/},
{type: 'presence', name: 'txt_message', error: 'Message must be present'}
]
});
But the problem is that, when the input does not match the validation it still returns true. Why does the validation still passes? Did I forgot something?PHP Code:var data = Ext.create('Contact', {
inp_name: 'te',
inp_email: 'christophedevartbe',
txt_message: null
});
var errors = data.validate();
console.log('Is User valid?', errors.isValid()); //still returns true no matter what
console.log('All Errors:', errors.items); //remains empty
-
Best Answer Posted by mitchellsimoens
Try this:
Code:Ext.define("Contact", { extend: "Ext.data.Model", config : { fields: [ {name: 'inp_name', type: 'string'}, {name: 'inp_email', type: 'string'}, {name: 'txt_message',type: 'string'} ], validations: [ {type: 'presence', name: 'inp_name', error: 'Name must be present'}, {type: 'length', name: 'inp_name', min: 5}, {type: 'presence', name: 'inp_email', error: 'Email must be present'}, {type: 'format', name: 'inp_email', matcher: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/}, {type: 'presence', name: 'txt_message', error: 'Message must be present'} ] } }); Ext.setup({ onReady : function() { var data = Ext.create('Contact', { inp_name: 'te', inp_email: 'christophedevartbe', txt_message: null }); var errors = data.validate(); } });
-
2 Feb 2012 1:09 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 434
- Answers
- 3102
Try this:
Code:Ext.define("Contact", { extend: "Ext.data.Model", config : { fields: [ {name: 'inp_name', type: 'string'}, {name: 'inp_email', type: 'string'}, {name: 'txt_message',type: 'string'} ], validations: [ {type: 'presence', name: 'inp_name', error: 'Name must be present'}, {type: 'length', name: 'inp_name', min: 5}, {type: 'presence', name: 'inp_email', error: 'Email must be present'}, {type: 'format', name: 'inp_email', matcher: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/}, {type: 'presence', name: 'txt_message', error: 'Message must be present'} ] } }); Ext.setup({ onReady : function() { var data = Ext.create('Contact', { inp_name: 'te', inp_email: 'christophedevartbe', txt_message: null }); var errors = data.validate(); } });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.
-
2 Feb 2012 11:50 PM #3
sorry I should have mentioned I'm already using the following:
and in the function setupApp() I have defined the model, created the form panel and tab panel and in the button handler I try to run the validation.PHP Code://load application
Ext.application({
name: 'Sencha',
launch: function(){
setupApp();
},
});
PHP Code:{
xtype: 'button',
text: 'Send',
ui: 'confirm',
handler: function() {
//Validate input
var data = Ext.create('Contact', {
inp_name: 'te',
inp_email: 'christophedevartbe',
txt_message: null
});
var errors = data.validate();
console.log('Is User valid?', errors.isValid()); //returns 'false' as there were validation errors
console.log('All Errors:', errors.items); //returns the array of all errors found on this model instance
if(errors.isValid())
{
alert('Form Validated');
}
else
{
alert('Form Not Validated');
}
return false;
//send ajax request
Ext.Ajax.request({
url: 'contact.php',
method: 'post',
waitMsg: 'Submitting form',
//params: formBase.getForm().getValues(false),
success: function (response, opts) {
jsonResponse = Ext.decode(response.responseText);
if (jsonResponse.success == true) {
Ext.Msg.alert('Success', 'We received your email and will come back to you asap.');
} else {
Ext.Msg.alert('Error', 'We could not send your email, please very your input');
}
}
});
}
}
-
3 Feb 2012 3:46 AM #4
What looks like is wrong is that you have specified validation for the validations field object rather than "validations" - the "s" being important.
I had a similar problem before with "extends" vs "extend"
-
3 Feb 2012 4:15 AM #5
oh pleasssseeeeee ..... how could I miss that
thank you.
-
3 Feb 2012 5:57 AM #6Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 434
- Answers
- 3102
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.
-
3 Feb 2012 6:50 AM #7
-
3 Feb 2012 6:51 AM #8Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 434
- Answers
- 3102
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.
-
3 Feb 2012 6:55 AM #9
-
3 Feb 2012 6:56 AM #10Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 434
- Answers
- 3102
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.


Reply With Quote
