Results 1 to 5 of 5

Thread: Custom validation problem

  1. #1
    Sencha User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    62

    Default Custom validation problem

    Hello !

    I set a custom vtype for validation. The goal is to force the user to enter a value either in one field or into another but not into both.

    Code:
    Ext.apply(Ext.form.VTypes, {
       redirect: function(value, field)
       {
       
          if (field.name=='link') {
              var check = TestForm.getForm().findField('redirect');
          }
          if (field.name=='redirect') {
              var check = TestForm.getForm().findField('link');
          }
    
          var check_value = check.getValue();
    
          if (value=='' && check_value=='') {
              this.redirectText = 'Enter a value. Both fields are empty.';
              return (false);
          }
    
          if (field.value!='' && check_value!='') {
              this.redirectText = 'Please enter a value either in one field or the other';
              return (false);
          }
    
          return (true);
       },
    
       redirectText: 'Please enter a value either in one field or the other'
    });
    This works so far. Both textfields are defined with allowBlank:true.

    I have a problem when I submit the form and leave both fields blank. The fields do not get validated. When I set allowBlank to true this overrides my custom validation. Manually calling .validate() or validateValue() gives me always true.

    What am I missing for an elegant solution? Thank you for your hints !

    Greetz
    Achim

  2. #2
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    Unfortunately the allowBlank tests is executed before the vtype test, so the only way you could do this is by initializing both fields with allowBlank:false and change it to true in the vtype test.

    ps. Another way would be to use the 'validator' function instead of a 'vtype' (the validator is executed before the allowBlank test).

  3. #3
    Sencha User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    62

    Default

    Code:
                        validator: function(value) {
                              var check_value = TestForm.getForm().findField('link').getValue();
    
                              if (value=='' && check_value=='') {
                                  this.setActiveError('Both fields are empty',true);
                                  return (false);
                              }
    
                              if (value!='' && check_value!='') {
                                  this.setActiveError('Please fill either one or the other',true);
                                  return (false);
                              }
    
                              return (true);
    
                        }
    Thank you! The validator did it !

    Now I have the problem that I cannot set the custom error message in the Quicktip. I tried setActiveError and markInvalid. Both were not successful. Do you have another hint?

  4. #4
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    The validator should return the error message instead of false.

  5. #5
    Sencha User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    62

    Default

    Oh well .. reading the API carefully sometimes could help
    I've totally overlooked this .. thank you very much!

Similar Threads

  1. NumberField Custom validation
    By Kishore.buturi in forum Ext 2.x: Help & Discussion
    Replies: 3
    Last Post: 10 Dec 2010, 12:29 PM
  2. Completely custom validation
    By 448191 in forum Ext 3.x: Help & Discussion
    Replies: 1
    Last Post: 7 Oct 2009, 12:47 AM
  3. Custom validation
    By aityahia in forum Ext 2.x: Help & Discussion
    Replies: 3
    Last Post: 15 Mar 2009, 9:12 AM
  4. Custom Validation keeps firing
    By MacSimon in forum Ext 2.x: Help & Discussion
    Replies: 0
    Last Post: 24 Feb 2009, 1:26 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
  •