Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 49

Thread: Form <-> Model binding

  1. #11
    Sencha User
    Join Date
    Feb 2011
    Posts
    106
    Vote Rating
    2
      0  

    Default

    updateRecord will work for me for the values. It's not automatic. But I suppose I could live with that aspect.

    Anybody know at this point in time what the validation rules on the model are good for? I looked at
    loadRecord for Forms and I don't see it doing anything with the validation rules. I am starting
    to get a little disappointed. I had this whole architectural idea of using reflection
    on my server's CSLA business object layer to dynamically create a ExtJS model protoype with validation rules.
    It was going to be real cool. But I don't see any of the UI using the validation on the model.

    I am hoping a Sencha developer can correct me and show me where I am going wrong.

    Thoughts?

  2. #12
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Vote Rating
    1272
      2  

    Default

    There is no automatic, bidirectional updating from Model < - > Form. You need to use loadRecord to load a record (model instance) into the form and then when you save the form, you will need to update the record that you loaded via basicForm.updateRecord(rec)

    Code:
        /**
         * Persists the values in this form into the passed {@link Ext.data.Model} object in a beginEdit/endEdit block.
         * @param {Ext.data.Record} record The record to edit
         * @return {Ext.form.Basic} this
         */
        updateRecord: function(record) {
            //record.beginEdit();
            var fields = record.fields,
                values = this.getValues(),
                name,
                obj = {};
            fields.each(function(f) {
                name = f.name;
                if (name in values) {
                    obj[name] = values[name];
                }
            });
            record.set(obj);
            //record.endEdit();
            return this;
        },
    As for validations, that is for if you try to update/create a record. If it passes the validation, it will return no errors. If there are errors, it will return an Ext.data.Errors object. Love this approach. More robust than just using vtypes and allowBlank.


    So on saving you can do this:

    Code:
    var basicForm = this.getForm(),
        rec = this.rec;
    
    basicForm.updateRecord(rec);
    var success = rec.validate().isValid();
    if (success) {
        //handle success
    } else {
        //handle failure
    }
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #13
    Sencha User
    Join Date
    Feb 2011
    Posts
    106
    Vote Rating
    2
      0  

    Default

    Ok that sounds good, but how do I use this "errors object" that returns from the model to show up in the UI?

    To me that would be very important in using the validation logic on the model (because this is all pretty much just client side type of validation anyway).

  4. #14
    Sencha User
    Join Date
    Feb 2011
    Posts
    106
    Vote Rating
    2
      0  

    Default

    Ahh I think I found it on basic form... markInvalid(..)

    Ok, I am starting to feel better again

    Would be nicer if it was all automatic like .NET... But I can live with this.

  5. #15
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Vote Rating
    1272
      2  

    Default

    Yes, you should not 100% rely on JS code but for 99% of users aren't hackers. Weird things happen so even though only 1% of users may have bad intent, something funky can go wrong for the 99% of people.

    Anyway, the API docs for Ext.data.Errors has a pretty good example:

    Code:
    var errors = myModel.validate();
    
    errors.isValid(); //false
    
    errors.length; //2
    errors.getByField('name');  // [{field: 'name',  error: 'must be present'}]
    errors.getByField('title'); // [{field: 'title', error: 'is too short'}]
    All the Ext.data.Errors is is an instance of MixedCollection. Notice that when you getByField("..."), it returns an array. This tripped me up.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  6. #16
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Vote Rating
    1272
      0  

    Default

    Quote Originally Posted by wizkid View Post
    Ahh I think I found it on basic form... markInvalid(..)
    Whoa, I missed that. I was doing it manually myself. Nice catch!
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  7. #17
    Sencha User
    Join Date
    Feb 2011
    Posts
    106
    Vote Rating
    2
      0  

    Default

    I agree when I code I assume anything coming into my server is hostile and should not be trusted. The client side validation is just a "nicety" for the UI. All it takes is some kid to use a JS debugger and you are in trouble unless you protected the server.

  8. #18
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Vote Rating
    1272
      0  

    Default

    Quote Originally Posted by wizkid View Post
    The client side validation is just a "nicety" for the UI.
    Yes and another huge benefit is that it cuts down on server stress also. Imagine you are the size of Facebook and if you didn't have client side validations, your calls to the server would quadruple when it doesn't need to.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  9. #19
    Sencha User
    Join Date
    Feb 2011
    Posts
    106
    Vote Rating
    2
      0  

    Default

    Lol, I wish I had that problem! I do code however for that day

  10. #20
    Sencha User
    Join Date
    Feb 2011
    Posts
    106
    Vote Rating
    2
      0  

    Default

    I want each field to validate itself on focus out. No point in getting people all the way down to the save button just to tell them a client side rule failed. I suppose I could hook validating the model on every focus out event?

Page 2 of 5 FirstFirst 1234 ... LastLast

Similar Threads

  1. Replies: 5
    Last Post: 15 May 2012, 8:06 PM
  2. Form Binding to BeanModel
    By zathril in forum Ext GWT: Discussion
    Replies: 0
    Last Post: 28 Mar 2010, 1:08 PM
  3. Form binding / update model
    By tsegismont in forum Ext GWT: Help & Discussion (1.x)
    Replies: 0
    Last Post: 2 Apr 2009, 1:22 AM
  4. Form binding
    By exo in forum Ext 1.x: Help & Discussion
    Replies: 1
    Last Post: 16 Apr 2007, 4:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •