We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3109 ☆ A M B ☆
    • 894 Posts
    So I made a form I used a prehook to populate it, that works great, and I made a hook to validate the input fields and I made errors for these in my hook using addError, I am also using the built in email validator.

    The problem I am having is FormIt is processing the email validation before my custom hook which validates the fields, so if I submit the form without filling it out this is what happens:



    After I fill out the email field with a valid email then my hook works:



    So I need to get my form validating all these fields at the same time not like its currently validating the email first and then running my custom hook.

    I tried looking into Custom Error Messages but don't quite understand how it works, I want a custom error for each input this seems more like a custom error for all fields.

    Here's what my FormIt call looks like:

    [[!FormIt?
    	&preHooks=`solartekPrehook`
      	&hooks=`solartekValidation,email,redirect`
    	&emailTpl=`contactFormTpl`
    	&emailFrom=`[email protected]`
    	&emailTo=`[email protected]`
    	&redirectTo=`2`
    	&validate=`Fname:required, Lname:required, email:email:required, zip-code:required, electric-bill:required, house-value:required`
      ]]
    


    This is my prehook:

    $hook->setValues(array(
      'Fname' => 'Enter Your First Name',
      'Lname' => 'Enter Your Last Name',
      'email' => 'Enter Email',
      'zip-code' => 'Enter Zip Code',
      'electric-bill' => 'Enter Average Annual Electricity Bill',
      'house-value' => 'Enter Estimated House Value',
    ));
    


    And this is my Custom Hook:

    <?php
    $i=0;
    
    if($hook->getValue('Fname')=="Enter Your First Name"){
      
      $hook->addError('Fname','Please enter your first name');
      
      $hook->setValue('Fname','Please enter your first name');
      
      $i=1;
      
      }
    
    if($hook->getValue('Lname')=="Enter Your Last Name"){
      
      $hook->addError('Lname','Please enter your last name');
      
      $hook->setValue('Lname','Please enter your last name');
      
      $i=1;
      
      }
    
    if($hook->getValue('email')=="Enter Email"){
      
    $hook->addError('email','Please enter your email');
      
      $hook->setValue('email','Please enter your email');
      
      $i=1;
      
      }
    
    if($hook->getValue('zip-code')=="Enter Zip Code"){
      
    $hook->addError('zip-code','Please enter your zip-code');
      
      $hook->setValue('zip-code','Please enter your zip-code');
      
      $i=1;
      
      }
    
    if($hook->getValue('electric-bill')=="Enter Average Annual Electricity Bill"){
      
    $hook->addError('electric-bill','Please enter your average electric bill');
      
      $hook->setValue('electric-bill','Please enter your average electric bill');
      
      $i=1;
      
      }
    
    if($hook->getValue('house-value')=="Enter Estimated House Value"){
      
    $hook->addError('house-value','Please enter your house value');
      
      $hook->setValue('house-value','Please enter your house value');
      
      $i=1;
      
      }
    
    if($i==1){
      return false;}
    else {
    	
          	return true;
    }
    


    Thanks for your help guys.
      Benjamin Marte
      Interactive Media Developer
      Follow Me on Twitter | Visit my site | Learn MODX
      • 28215
      • 4,149 Posts
      splittingred Reply #2, 15 years ago
      You should be using a custom validator, not a custom hook. Hooks do not run until after all validators have passed.

      http://rtfm.modx.com/display/ADDON/FormIt.Validators#FormIt.Validators-CustomValidators

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com