Confirm password validator ExtJs 4 - extjs4

I want to add a simple validator to verify that "password" and "confirm password" are equal, here is the code :
Ext.apply('Ext.form.VTypes',{
password : function(val, field) {
if (field.initialPassField) {
var pwd = Ext.getCmp(field.initialPassField);
return (val == pwd.getValue());
}
return true;
},
passwordText : 'Passwords do not match'
});
var genders = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'],
data : [
{"abbr":"AL", "name":"Male"},
{"abbr":"AK", "name":"Female"}
]
});
//****************************************
Ext.define('AM.view.user.Inscription', {
extend: 'Ext.form.Panel',
alias: 'widget.inscription',
title: 'Formulaire',
fieldDefaults: {
labelAlign: 'right',
msgTarget: 'side'
},
items: [{
xtype: 'container',
anchor: '100%',
layout:'column',
items:[{
xtype:'textfield',
fieldLabel: 'First Name',
name: 'first',
allowBlank:false,
anchor:'40%',
//maxLength : '10'
},{
xtype:'textfield',
fieldLabel: 'Last Name',
name: 'last',
allowBlank:false,
anchor:'40%'
},{
xtype:'textfield',
inputType: 'password',
fieldLabel:'Password',
name:'pass',
id : 'pass',
allowBlank:false,
anchor:'40%'
},{
xtype:'textfield',
inputType: 'password',
fieldLabel:'Confirm Password',
name:'confirmPass',
allowBlank:false,
vtype : 'password',
initialPassField : 'pass',
//autoRender : true,
anchor:'40%'
}]
},{
xtype: 'container',
columnWidth:.5,
layout: 'anchor',
items: [{
xty pe:'combobox',
fieldLabel: 'Sexe',
store: genders,
queryMode: 'local',
displayField: 'name',
allowBlank : false,
name: 'gender',
editable : false,
anchor:'30%',
},{
xtype:'textfield',
fieldLabel: 'N° Téléphone',
name: 'phone',
allowBlank:false,
anchor:'40%'
}]
}]
}],
buttons: [{
text: 'Save'
},{
text: 'Reset'
},{
text: 'Cancel'
}]
});
whene I start write in the confirm password textfield I get this in chrome developer tool :
Uncaught TypeError: Object [object Object] has no method 'password'
can someone tell me what I miss here ? thank you at advance.
UPDATE
the application is an MVC application and this file (the code above) is in View folder, and I'm calling this view in the app.js like this :
Ext.require([
'Ext.panel.*',
'Ext.toolbar.*',
'Ext.button.*',
'Ext.container.ButtonGroup',
'Ext.layout.container.Table'
]);
Ext.application({
name: 'AM',
appFolder: 'app',
controllers: [
'Users'
],
launch: function() {
Ext.create('Ext.container.Viewport', {
layout : 'auto',
//layout : 'vbox',
renderTo: document.body,
items: [{
xtype : 'usertoolbar',
},{
html : '<br><br>'
},{
xtype: 'inscription',
},{
xtype: 'userlist',
}]
});
}
});
Also any notes about my methodology of structuring code are welcome

Change Ext.form.VTypes to Ext.form.field.VTypes in the first line. See if it helps.
Update: change your validate function to:
password : function(val, field) {
console.log(val, field);
if (field.initialPassField) {
var pwd = Ext.getCmp(field.initialPassField);
console.log(pwd);
return (val == pwd.getValue());
}
return true;
},

Change Ext.form.VTypes to Ext.form.field.VTypes. And try to remove quotes around 'Ext.form.field.VTypes'. E.g.:
Ext.apply(Ext.form.field.VTypes, {
password : function(val, field) {
if (field.initialPassField) {
var pwd = Ext.getCmp(field.initialPassField);
return (val == pwd.getValue());
}
return true;
}

The solution is to remove quotes around 'Ext.form.field.VTypes'
Ext.apply(Ext.form.field.VTypes, {
password: function (val, field) {
if (field.initialPassField) {
var pwd = Ext.getCmp(field.initialPassField);
return (val == pwd.getValue());
}
return true;
},
passwordText: 'Passwords do not match'
});

Related

Set the edited row in EXTJS grid after editing without refreshing page

I am editing a row in EXTJS grid on clicking the edit action link. On clicking the edit link of a row a new window opens with all the data of the row and "Save" and "Cancel" button.
On clicking the "Save" button, it is saving the record in database. But I want the row should also get refreshed without refreshing the page.
I am new to EXTJS.
Can any one help me to do the same.
Here is my code.
Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.util.*',
'Ext.grid.plugin.BufferedRenderer'
]);
Ext.define('TestResult', {
extend: 'Ext.data.Model',
fields: [
{
name: 'ID',
type: 'int'
},
{
name: 'jobNo',
type: 'int'
},
{
name: 'stageCode',
type: 'String'
},
{
name: 'productTitle',
type: 'String'
},
{
name: 'brand',
type: 'String'
},
{
name: 'category',
type: 'String'
},
{
name: 'ftpDate',
type: 'Date'
}],
idField: 'ID'
});
Ext.onReady(function() {
var store = Ext.create('Ext.data.Store', {
model: 'TestResult',
autoLoad: true,
autoSync: true,
proxy: {
type: 'ajax',
url : 'data.jsp',
reader :
{
type : 'json'
},
writer :
{
type : 'json'
}
}
});
var grid = Ext.create('Ext.grid.Panel', {
width: 700,
height: 500,
title: 'Buffered Grid of records',
store: store,
loadMask: true,
plugins: 'bufferedrenderer',
selModel: {
pruneRemoved: false
},
viewConfig: {
trackOver: false
},
features: [{
ftype: 'groupingsummary',
groupHeaderTpl: 'Department: {name}',
showSummaryRow: false
}],
// grid columns
columns:[{
text: 'ID',
sortable: true,
dataIndex: 'ID',
groupable: false,
locked: true,
width: 70
}, {
text: 'Job No.',
sortable: true,
dataIndex: 'jobNo',
groupable: false,
locked: true,
width: 120
}, {
text: 'Version',
dataIndex: 'stageCode',
groupable: false
}, {
text: 'Product Title',
dataIndex: 'productTitle',
groupable: false
}, {
text: 'Brand',
dataIndex: 'brand',
groupable: false
}, {
text: 'Category',
dataIndex: 'category',
width: 200,
groupable: false
}, {
text: 'FTP Date',
dataIndex: 'ftpDate',
xtype: 'datecolumn',
groupable: false
},
{
xtype:'actioncolumn',
header:'Edit',
width:50,
items: [{
icon: 'assets/images/edit.png', // Use a URL in the icon config
tooltip: 'Edit',
handler: function(grid, rowIndex, colIndex) {
var rec = grid.getStore().getAt(rowIndex);
editForm.show();
editForm.down('form').loadRecord(rec);]
}],
renderTo: Ext.getBody()
});
var editForm = new Ext.Window({
title: 'Edit Window',
items:[
{
xtype: 'form',
url: 'UpdateController',
items: [
{
xtype: 'hidden',
fieldLabel: 'ID',
name: 'ID',
allowBlank: false,
readOnly: true
},
{
xtype: 'textfield',
fieldLabel: 'Job No.',
name: 'jobNo',
allowBlank: false,
readOnly: true
},
{
xtype: 'textfield',
fieldLabel: 'Version',
name: 'stageCode',
allowBlank: false,
readOnly: true
},
{
xtype: 'textfield',
fieldLabel: 'Product Title',
name: 'productTitle',
allowBlank: false
},
{
xtype: 'textfield',
fieldLabel: 'Category',
name: 'category',
allowBlank: false
},
{
xtype: 'textfield',
fieldLabel: 'Brand',
name: 'brand',
allowBlank: false
},
{
xtype: 'datefield',
fieldLabel: 'FTP Date',
name: 'ftpDate',
allowBlank: false
}],
buttons : [{
text : 'Save',
handler: function()
{
this.up('form').getForm().submit(
{
success: function(f,a)
{
store.save();
var win = Ext.WindowManager.getActive();
if (win)
{
win.hide();
}
},
failure: function(f,a)
{
//Ext.Msg.alert('Warning', 'Error');
Ext.Msg.alert('Warning', a.result.errormsg);
this.up('window').hide();
}
});
}
},
{
text: 'Cancel',
handler: function()
{
this.up('window').hide();
}
}]
}
]
});
});
Thanks
In your this.up('form').getForm().submit success handler you can update record, which you loaded into form, by using Ext.form.Basic updateRecord method.
So just add into success handler code:
// update record with form data
f.updateRecord();
// mark record as synchronized with server (because you already sent data to server with form submit method)
f.getRecord().commit();

add text field values to localstore from field set in sencha touch 2

i am new to sencha touch , i am get an error in this below code is tab change function
error:Uncaught TypeError: Object [object Object] has no method 'getValues'
var uswrfeild = Ext.getCmp('User_details');
//var fieldset = Ext.create('FieldSet_PersonalSettings');
//var fieldset= this.tab;
var values = uswrfeild.getValues();
cart = Ext.create('Items');
//alert(values);
//cart.add({field1:'value1',field2:'value2'});
cart.add(values);
cart.sync();
below code is feild set code
{
xtype: 'container',
title: 'User',
id: 'User_details',
itemId: 'mycontainer3',
scrollable: 'vertical',
items: [
{
xtype: 'fieldset',
id: 'FieldSet_PersonalSettings',
itemId: 'myfieldset12',
margin: '2%',
title: 'User',
items: [
{
xtype: 'textfield',
label: 'Name',
name: 'name',
maxLength: 31,
placeHolder: 'given-name family-name'
},
{
xtype: 'emailfield',
label: 'Email',
name: 'email',
required: true,
maxLength: 63,
placeHolder: 'name#example.com'
},
{
xtype: 'textfield',
label: 'Street',
name: 'street',
required: true,
placeHolder: 'streetname'
},
{
xtype: 'textfield',
label: 'House Number',
required: true,
placeHolder: '123'
},
{
xtype: 'textfield',
label: 'Zipcode',
required: true,
autoCapitalize: true,
maxLength: 10,
placeHolder: '1234AA'
},
{
xtype: 'textfield',
label: 'Country',
required: true,
placeHolder: 'NL'
}
]
},
{
xtype: 'fieldset',
margin: '2%',
title: 'Sharing information',
items: [
{
xtype: 'checkboxfield',
label: 'Receive email',
labelWidth: '75%',
checked: true
},
{
xtype: 'checkboxfield',
height: 49,
label: 'Upload statistics (anonymously)',
labelWidth: '75%',
checked: true
}
]
}
belo code is model
Ext.define('iFP.model.item', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'name',
type: 'string'
},
{
name: 'email',
type: 'string'
},
{
name: 'street',
type: 'string'
},
{
name: 'hno',
type: 'auto'
},
{
name: 'zipcode',
type: 'int'
},
{
name: 'country',
type: 'string'
}
]
}
});
below code is store
Ext.define('iFP.store.userlocalsettings', {
extend: 'Ext.data.Store',
requires: [
'iFP.model.item'
],
config: {
autoSync: false,
model: 'iFP.model.item',
storeId: 'usersettingslocalstore',
proxy: {
type: 'localstorage'
}
}
});
my aim is the text feid values are stored in browser localstore.
please help me any buddy.
Thanks in advance.
You should first set id for each field, like this for example
{
xtype: 'textfield',
label: 'Name',
name: 'name',
maxLength: 31,
placeHolder: 'given-name family-name',
itemId: 'tfName'
},
then you can get value of field after some event in function
var namefield = this.down('#tfName');
var namevalue = namefield.getValue();
...
and then you can add that value to store
var store = Ext.getStore('userlocalsettings');
store.add({name: namevalue});
store.sync();
Hope this will help.
you should set ID for your form.
config: {
itemId: 'form1'
}
Then in your controller add reference to that ID
refs: {
myPanel: '#form1'
}
I can't see your button listener. suppose the name of the listener is "onTapSave" then you should try this,
onTapSave: function(component,button, options){
var uswrfeild = this.getMyPanel();
var values = uswrfeild.getValues();
}

How to do I insert a form into a view?

I am new to sencha touch and I am having trouble loading a form into a view.
In my app.js I am loading the main view as such
Ext.Viewport.add(Ext.create('app.view.Main'));
My full app.js is as follows
Ext.application({
name: 'app',
requires: [
'Ext.MessageBox',
'Ext.form.FieldSet',
],
views: ['Main'],
launch: function() {
// Initialize the main view
Ext.Viewport.add(Ext.create('PlanPouch.view.Main'));
},
});
My main view is:
Ext.define("app.view.Main", {
extend: 'Ext.Container',
config:{
items:[
form
]
}
});
var form = Ext.create('Ext.form.Panel', {
fullscreen: true,
items: [
{
xtype: 'textfield',
name: 'name',
label: 'Name'
},
{
xtype: 'emailfield',
name: 'email',
label: 'Email'
},
{
xtype: 'passwordfield',
name: 'password',
label: 'Password'
}
]
});
I took the form from the official documentation and the form doesn't load for some reason. So how can I add the form to my main view?
First let it solve by your approach. Make the following changes & it should work in your system (Running on mine):-
1.) In app.js
Ext.Viewport.add(Ext.create('app.view.Main'));
2). In Main.js
Ext.define("app.view.Main", {
extend: 'Ext.Container',
config:{
items:[
{
xtype: form
}
]
}
});
var form = Ext.create('Ext.form.Panel', {
fullscreen: true,
items: [
{
xtype: 'textfield',
name: 'name',
label: 'Name'
},
{
xtype: 'emailfield',
name: 'email',
label: 'Email'
},
{
xtype: 'passwordfield',
name: 'password',
label: 'Password'
}
]
});
But i will not prefer to call the formpanel in the way you are doing. Rather you can follow ARolek answer OR follow this approach :-
1) app.js
Ext.application({
name: "app",
views: ["Main","second"],
launch: function () {
Ext.Viewport.add(Ext.create('app.view.Main'));
}
});
2) Main.js
Ext.define("app.view.Main", {
extend: "Ext.Container",
requires:["app.view.second"],
config:{
fullscreen: true,
layout:
{
type: 'fit'
},
items:[
{
xtype: "form"
}
]
}
});
3) second.js
Ext.define("app.view.second", {
extend: "Ext.form.Panel",
requires:['Ext.form.FieldSet','Ext.Toolbar'],
alias: "widget.form",
config:
{
items:
[
{
xtype: 'fieldset',
title: 'About You',
items: [
{
xtype: 'textfield',
name: 'name',
label: 'Name'
},
{
xtype: 'emailfield',
name: 'email',
label: 'Email'
},
{
xtype: 'passwordfield',
name: 'password',
label: 'Password'
}
]
}
]
}
});
If you want the form to always be in app.view.Main, then simply do:
Ext.define("app.view.Main", {
extend: 'Ext.Container',
config:{
items:[
{
xtype:'formpanel',
items: [
{
xtype: 'textfield',
name: 'name',
label: 'Name'
},
{
xtype: 'emailfield',
name: 'email',
label: 'Email'
},
{
xtype: 'passwordfield',
name: 'password',
label: 'Password'
}
]
}
]
}
});

ExtJs:Show Grid after Button is clicked

I have a form in which the values for the Grid is populated as soon as the Form is displayed.But i need to populate the grid after the button is clicked based on text box values.
ExtJs:
var url = location.href.substring(0,location.href.indexOf('/', 14));
var grid;
var store;
var userStore;
Ext.onReady(function(){
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
Ext.define('userList', {
extend: 'Ext.data.Model',
fields: [{ name: 'id', mapping: 'id' },
{ name: 'name', mapping: 'name' },
{ name: 'firstName' ,mapping:'personalInfo.firstName'},
{ name: 'lastName' ,mapping:'personalInfo.lastName'},
{ name: 'gender' ,mapping:'personalInfo.gender'},
{ name: 'email' ,mapping:'personalInfo.address.email'}
]
});
store = Ext.create('Ext.data.Store', {
model: 'userList',
autoLoad: true,
proxy: {
type: 'ajax',
url : url+'/lochweb/loch/users/getUser',
reader: {
type: 'json',
root: 'Users'
}
}
});
function swapStrore(){
//userStore = store;
userStore = Ext.create('Ext.data.Store', {
model: 'userList',
autoLoad: true,
proxy: {
type: 'ajax',
url : url+'/lochweb/loch/users/getUser',
reader: {
type: 'json',
root: 'Users'
}
}
});
Ext.getCmp('userGrid').getView().refresh();
return userStore;
}
function updateUsers(id){
window.location = url+'/lochportal/createUser.do';
}
var searchUsers = new Ext.FormPanel({
renderTo: "searchUsers",
frame: true,
title: 'Search Users',
bodyStyle: 'padding:5px',
width: 900,
items:[{
xtype:'textfield',
fieldLabel: 'Username',
name: 'userName'
},{
xtype:'textfield',
fieldLabel: 'First Name',
name: 'firstName'
},{
xtype:'textfield',
fieldLabel: 'Last Name',
name: 'lastName'
},
{
xtype: 'button',
text: 'Search',
listeners: {
click: function(){
Ext.Ajax.request({
method:'GET',
url : url+'/lochweb/loch/users/getUser',
params : searchUsers.getForm().getValues(),
success : function(response){
//console.log(response);
//swapStrore();
}
});
}
}
},{
xtype: 'button',
text: 'Cancel',
listeners: {
click: function(){
window.location = url+'/lochportal/viewSuccessForm.do';
}
}
},
grid = Ext.create('Ext.grid.Panel', {
//plugins: [rowEditing],
id:'userGrid',
width: 900,
height: 300,
frame: true,
store: store,
iconCls: 'icon-user',
columns: [{
text: 'ID',
width: 40,
sortable: true,
dataIndex: 'id'
},
{
text: 'Name',
flex: 1,
sortable: true,
dataIndex: 'name',
field: {
xtype: 'textfield'
}
},
{
text: 'FirstName',
flex: 1,
sortable: true,
dataIndex: 'firstName',
field: {
xtype: 'textfield'
}
},
{
text: 'LastName',
flex: 1,
sortable: true,
dataIndex: 'lastName',
field: {
xtype: 'textfield'
}
},
{
text: 'Gender',
flex: 1,
sortable: true,
dataIndex: 'gender',
field: {
xtype: 'textfield'
}
},
{
text: 'Email',
flex: 1,
sortable: true,
dataIndex: 'email',
field: {
xtype: 'textfield'
}
},
{
xtype: 'actioncolumn',
width: 50,
items:[{
icon : '/lochportal/extJS/resources/themes/images/access/window/edit.gif', // Use a URL in the icon config
tooltip: 'Sell stock',
handler: function(grid, rowIndex, colIndex) {
var rec = store.getAt(rowIndex);
//alert("Sell " + rec.get('id'));
updateUsers(rec.get('id'));
}
}]
}]
})]
});
var win = new Ext.Window({
layout:'fit',
closable: false,
resizable: true,
plain: true,
border: false,
items: [searchUsers]
});
win.show();
});
How to populate the grid after search button is clicked?
Thanks
Use bindStore() method of the grid to assign new or different store to grid which is already displayed. If you want to first show an empty grid - either filter all records out or assign store property to null initially.

Sencha touche form validation

I am creating my first app with sencha touch.
I have a problem with form validation.
This is the form:
var form = new Ext.form.FormPanel({
title: 'Activity',
items: [{
xtype: 'fieldset',
items: [{
xtype: 'textfield',
name : 'sector',
label: 'Sector',
required: true
},
{
xtype: 'selectfield',
name : 'city',
label: 'City',
options: [
{
text: '*',
value: '*'
},
{
text: 'First Option',
value: 'first'
},
{
text: 'Second Option',
value: 'second'
},
{
text: 'Third Option',
value: 'third'
}]
},
{
xtype: 'textfield',
name : 'nation',
label: 'Nation',
required: true
},
{
xtype: 'toolbar',
docked: 'bottom',
layout: {
pack: 'center'
},
items: [{
xtype: 'button',
text: 'Cerca',
handler: function() {
formSettori.submit({
url: 'book.php',
method: 'GET',
success: function() {
Ext.Msg.alert('OK');
},
failure: function() {
Ext.Msg.alert('NO');
}
});
}
},
{
xtype: 'button',
text: 'Reset',
handler: function() {
form.reset();
}
}]
}]
}]
});
the form has only three fields:
-activities
-city​​
-nation
all fields are required.
activities and the nation must not be empty, while the city should not be equal to *
how do I control the fields?
thank you!
There is not built in way to do form validation. You must do it yourself.
The easiest way is to use the private Ext.form.Panel method getFields to loop through each of the fields and ensure they are not empty.
var fields = form.getFields(),
field, name, isEmpty;
for (name in fields) {
field = fields[name];
isEmpty = (!field.getValue() || field.getValue() == "");
if (isEmpty) {
field.addCls('x-invalid');
} else {
field.removeCls('x-invalid');
}
}
If the field is empty, then we add the x-invalid class (so you can style it).