We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31104
    • 108 Posts
    Hi smiley

    Is there a way to tell formit to cross check the value of a field before validating something like "if value != to pre-set value then validate, else show error"?

    Here's what I have:

    [[!FormIt?
       &hooks=`email,redirect`
       &emailTpl=`send_form`
       &emailTo=`[email protected]`
       &emailFrom=`[email protected]`
       &emailSubject=`Reservation from website`
       &redirectTo=`11`
       &submitVar=`reservation`
       &validate=`name:required,email:required,time:required,date:required,party:required`
    ]]
    
     <p>Make your reservation online today!</p>
     
    [[!+fi.error_message:notempty=`<p>[[!+fi.error_message]]</p>`]]
     
    <form action="[[~[[*id]]]]" method="post" class="form">
    
    <span class="error">[[!+fi.error.name]]</span><br />
    <input type="text" name="name" value="Your name..." onFocus="clearText(this)" onBlur="clearText(this)" />
    
    <span class="error">[[!+fi.error.email]]</span><br />
    <input type="text" name="email" value="Your email..." onFocus="clearText(this)" onBlur="clearText(this)" />
    
    <span class="error">[[!+fi.error.time]]</span><br />
    <input type="text" name="time" value="Time of reservation..." onFocus="clearText(this)" onBlur="clearText(this)" />
    
    <span class="error">[[!+fi.error.date]]</span><br />
    <input id="datepicker" type="text" name="date" value="Date..." onFocus="clearText(this)" onBlur="clearText(this)" />
    
    Party size...
    
    <select name="party" >
      <option value="1" selected="selected">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10 or more</option>
    </select><br />
    
    
    Observations, allergies, etc...
    
    <textarea name="message_text" id="message_text"></textarea><br />
    
    <input type="submit" value="Send" name="reservation" />
    
    </form> 
    


    Notice that I have pre-set values in the input fields that I change onBlur and onFocus with js, the problem is that it won't validate since it's not empty. (I need these pre-set values in the field to save screen realestate)

    I checked the rtfm and there are validators that check if if the password equals something or if the field contains a certain word but nothing to tell it to check for default values.

    Perhaps I'm not looking in the right direction and I should validate with js? (I was just hoping formit could do it out of the box).

    Any help appreciated.


      • 3109 ☆ A M B ☆
      • 894 Posts
      You need to make a custom hook so it checks the values of the FormIt fields to see if they equal the default values so it prevents the form from submitting.

      Create a new snippet and call it: checkDefaultValues

      Then paste this code.

      <?php
      $name = $hook->getValue('name');
      
      $email = $hook->getValue('email';);
      
      $time = $hook->getValue('time';);
      
      $date = $hook->getValue('date';);
      
      if( $name == 'Your name...'){
          $errorMsg = 'Please enter your name';
          $hook->addError('name',$errorMsg);
          return false;  
      }else{
          return true;
      }
      
      if( $email == 'Your email...'){
          $errorMsg = 'Please enter an email';
          $hook->addError('email',$errorMsg);  
          return false;  
      }else{
          return true;
      }
      
      if( $time == 'Time of reservation...'){
          $errorMsg = 'Please enter a time';
          $hook->addError('time',$errorMsg);  
          return false;  
      }else{
          return true;
      }
      
      if( $date == 'Date...'){
          $errorMsg = 'Please enter a date';
          $hook->addError('date',$errorMsg);  
          return false;  
      }else{
          return true;
      }
      


      Then in your FormIt &hooks call add the custom hook before email since you want it to check the default values first before the other hooks.

      &hooks=`checkDefaultValues, email,redirect`
      


      This should work I'm not a PHP guru so I'm sure the code could be optimize but it will work for what you want it to do.

      You can also use a preHook so it inserts the default values when rendering the form instead of hard coding them.

      Hope that helps. [ed. note: benmarte last edited this post 14 years, 1 month ago.]
        Benjamin Marte
        Interactive Media Developer
        Follow Me on Twitter | Visit my site | Learn MODX
        • 31104
        • 108 Posts
        Wow, thank you kindly Benmarte for the full script, I'll give it a try.

        You da man!
          • 3109 ☆ A M B ☆
          • 894 Posts
          Let me know if it works smiley
            Benjamin Marte
            Interactive Media Developer
            Follow Me on Twitter | Visit my site | Learn MODX