In Part 4 of this Post Series, we are going to see how we can use single Sencha Touch form for both add and edit feature. If you are new to this Post Series, Please read the previous post. Following are those
How to apply Add/Edit while Saving Form Data
This onFormSave() method will be trigged when you click on save button. The scope of this article is to differenciate between add and edit form. If you need to know, how to attach event listeners to form, apply validation and store form data locally, Please refer previous post series.
First, using getValues() method we are getting the Login form values, then we are checking whether id field is null. if yes, then we are coming from add form. so we are adding the form data to the store using add().
If id field is not null, we are from edit form. so we are updating the model records using update(). Finally sync() method is used to sync the store records with the Local Storage.
Hope, you enjoyed this Post Series. Please let me know your feedbacks through Comments. Thanks.
- Part 1 - How to create a basic form using Sencha Touch 2.
- Part 2 - Apply Model Validation to the Form using Sencha Touch 2.
- Part3 - Save Form data locally using local storage in Sencha Touch 2.
Ext.define('MyApp.model.User',{ extend: 'Ext.data.Model', alias: 'model.User', config: { fields: [ { name: 'id' }, { name: 'username' }, { name: 'password' }, { name: 'email' }, { name: 'website' }, { name: 'aboutyou' } ], validations: [ { type: 'presence', field: 'username' }, { type: 'presence', field: 'password' }, { type: 'presence', field: 'email' }, { type: 'presence', field: 'website' }, { type: 'format', field: 'username', matcher: /[0-9A-Za-z]{6,15}/, message: 'Username should be alphanumeric' }, { type: 'length', field: 'password', min: 6, message: 'Password must contain min 6 characters' }, { type: 'email', field: 'email', message: 'Email format is invalid' }] } });Then, we are going to use to Login Form that we have already created in part1 of this Post Series. We are going to add one hidden field with name id which is used to differenciate between add and edit form.
Ext.define('MyApp.view.FormPanel', { extend: 'Ext.form.Panel', alias: 'widget.FormPanel', config: { itemId: 'registrationform', items: [ { xtype: 'fieldset', title: 'Registration Form', items: [ { xtype: 'hidden', name: 'id' }, { xtype: 'textfield', label: 'Username', labelWrap: true, name: 'username', placeHolder: 'Enter Username' }, { xtype: 'textfield', label: 'Password', labelWrap: true, name: 'password', placeHolder: 'Enter Password' }, { xtype: 'emailfield', label: 'Email', labelWrap: true, name: 'email', placeHolder: 'email@example.com' }, { xtype: 'urlfield', label: 'Website', labelWrap: true, name: 'website', placeHolder: 'http://example.com' }, { xtype: 'textareafield', label: 'About You', labelWrap: true, name: 'aboutyou', placeHolder: 'Tel me about yourself' } ] }, { xtype: 'button', itemId: 'save', width: '30%', text: 'Save' } ] } });Following is the HTML form that will be rendered in Google Chrome,
How to apply Add/Edit while Saving Form Data
This onFormSave() method will be trigged when you click on save button. The scope of this article is to differenciate between add and edit form. If you need to know, how to attach event listeners to form, apply validation and store form data locally, Please refer previous post series.
onFormSave: function(button,e,options){ var formObj = button.up('FormPanel'); var userStore = Ext.getStore('userstore'); var formData = formObj.getValues(); if(formData.id === null) {//add form var usr = Ext.create('MyApp.model.User',{ username: formData.username, password: formData.password, email: formData.email, website: formData.website, aboutyou: formData.aboutyou }); userStore.add(usr); } else {//edit form var usr = userStore.getById(formData.id); usr.set('username', formData.username); usr.set('password', formData.password); usr.set('email', formData.email); } userStore.sync();
First, using getValues() method we are getting the Login form values, then we are checking whether id field is null. if yes, then we are coming from add form. so we are adding the form data to the store using add().
If id field is not null, we are from edit form. so we are updating the model records using update(). Finally sync() method is used to sync the store records with the Local Storage.
Hope, you enjoyed this Post Series. Please let me know your feedbacks through Comments. Thanks.
Thank you so much for your post ...its really awesome work..helped me a lot ...
ReplyDeletehow to view the saved data back in this form?
ReplyDeleteHi dhinesh,
ReplyDeleteYou need to load the saved data store in to the list, then by tapping list items, you need to take the user to the edit form. you can refer the above code. Thanks
How to implement Add/Edit Feature in single form using Sencha Touch 2
ReplyDeletewhile tapping edit button in my case it throws
Uncaught TypeError: Cannot call method 'set' of null
onFormEdit: function(button,e,options){
var formObj = button.up('FormPanel');
var customerStore = Ext.getStore('cusotmerstore');
var formData = formObj.getValues();
if(formData.id == null) {//add form
var Customer = Ext.create('CustomerForm.model.Customer',{
firstname: formData.firstname,
lastname: formData.lastname,
address1: formData.address1,
address2: formData.address2,
postalcode: formData.postalcode,
city: formData.city,
state: formData.state,
home: formData.home,
work: formData.work,
mobile: formData.mobile,
email:formData.email
});
customerStore.add(Customer);
} else {//edit form
var test=formData.id;
var customer = customerStore.getById(formData.id);
customer.set('firstname', formData.firstname);
customer.set('lastname', formData.lastname);
customer.set('address1', formData.address1);
customer.set('address2', formData.address2);
customer.set('postalcode', formData.postalcode);
customer.set('city', formData.city);
customer.set('state', formData.state);
customer.set('home', formData.home);
customer.set('work', formData.work);
customer.set('mobile', formData.mobile);
customer.set('email', formData.email);
}
Hi Sathish,
DeleteI am seeing an issue in this below line
var customer = customerStore.getById(formData.id);
When you are adding model data into store. Sencha touch will insert dynamic id (say 'Ext-xx-12') under 'id' column. so i recommenr don't use this 'id' column.
try these 2 options,
option 1:
------------
Create an model field say 'customerid', and manually autoincrement that id while you are adding model data to the store
option 2:
-----------
if 'emai' field is unique. instead of 'id' column. you can query 'email' column.
var customer = customerStore.findRecord('email',formData.email);
Important Data: you can use console.log() for debugging which will display objects data.
console.log(formData);
Hope, this solves your issue
Suresh Ariya
How to list previous record?
ReplyDeleteCan you please let me know, which previous record list you are mentioning here?
Deleterecords which we save previously
ReplyDeleteHi Satish,
DeleteFirst, you need to create model instance with the form values. then you need to use setRecord() in form object. Something like this
var formObj = button.up('FormPanel');
var Customer = Ext.create('CustomerForm.model.Customer',{
firstname: formData.firstname,
lastname: formData.lastname,
.....
});
formObj.setRecord(Customer);
//redirect to the form edit page with animation
Ext.Viewport.animateActiveItem(formObj, { type: 'slide', direction: 'left'});
Hope, this helps
Thanks and Regards,
Suresh Ariya