In today’s post I am going to show you how to create a simple form in Sencha Touch with validation. I will create a simple Login form with username and password field and create a model which contains fields and validation logic.
Once user submit form for login, we get the validation logic and see if all the field get passed against our model validation logic, if no then will show error message alert and make the field highlighted in red border. So lets start first by creating Model
. Pay attention to validations
array, its an array of all validation rules that we want to apply for our model fields.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
Ext.define(“TechZoo.model.User”, {
extend: “Ext.data.Model”,
config: {
fields : [
{name : ‘username’, type: ‘string’},
{name : ‘password’, type: ‘string’}
],
validations : [{
type: ‘presence’,
field: ‘username’,
message: ‘can not be blank.’
},{
type : ‘presence’,
field : ‘password’,
message: ‘can not be blank.’
},{
type : ‘length’,
field : ‘username’,
min : 6,
max : 8,
message: ‘is not in valid range.’
},{
type : ‘length’,
field : ‘password’,
min : 6,
max : 8,
message: ‘is not in valid range.’
}]
}
});
|
Now, create a LoginForm by extending FormPanel
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
Ext.define(‘TechZoo.view.LoginForm’, {
extend: ‘Ext.form.Panel’,
alias : ‘widget.loginform’,
config: {
fullScreen : true,
scroll : ‘vertical’,
url : ”,
items :[{
xtype : ‘fieldset’,
title : ‘Enter Your Login Details’,
items : [{
xtype : ‘textfield’,
labelWidth: 120,
label : ‘Username’,
name : ‘username’,
required: true
},{
xtype : ‘passwordfield’,
label : ‘Password’,
labelWidth: 120,
name : ‘password’,
required: true
}]
},{
xtype : ‘toolbar’,
docked : ‘bottom’,
layout : {
pack : ‘center’,
type : ‘hbox’
},
items : [{
text : ‘Reset’,
handler: function(me) {
var form = me.up(‘formpanel’);
form.reset();
Ext.each(form.query(‘textfield’), function(tf){
tf.removeCls(‘invalidField’);
});
}
},{
text : ‘Login Now’,
ui : ‘confirm’,
handler: function(me) {
var form = me.up(‘formpanel’);
var user = Ext.create(‘TechZoo.model.User’, form.getValues());
var errors = user.validate();
if(!errors.isValid()) {
var errorMsg = “”;
errors.each(function (item, index, length) {
form.down(“field[name='”+item.getField()+“‘]”).addCls(‘invalidField’);
errorMsg += ‘ ‘ +item.getField()+‘ ‘+ item.getMessage() + ‘
‘; });
Ext.Msg.alert(“Validation Failed”, errorMsg);
}
else {
Ext.Msg.alert(“Validation Success”, “Form doesn’t contain any error.”);
}
}
}]
}]
}
});
|
Take a close look at login button handler. First, we are creating the model object and then calling validate()
method on it. Calling validate methods return Ext.data.Errors
object. We can call isValid()
function to see if form contain any error on not.
Output:
On Initial launch of form…
If form contains any error…
I hope you enjoy the post. Happy Coding 🙂
Source techzoo.org