This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Form Validation Rules - More than just forced value or email???


  1. mpivon
    Member

    Hello,

    I am interested in creating a form with validation logic that checks to see if a user is from a pre-defined (or authorized) list.

    i.e.: The code would look something like this:

    if ( strcasecmp($inv,"1111") != 0 && strcasecmp($inv,"2222") != 0 && strcasecmp($inv,"3333") != 0 && strcasecmp($inv,"4444") != 0 && strcasecmp($inv,"5555") != 0 )

    {
    die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Registration is only open to recipients with an authorized promo code. Please try again or contact XXXX. </font></p>");
    }

    How would THIS be implemented?

    Posted 16 years ago #
  2. yuniar

    Hi,

    To do this you will need to modify two files and add these codes.
    1) Modify includes/post-functions.php, insert the code below into line 772, above the query line.

    //post_validation hook
    if(function_exists("form{$form_id}_hook_post_validation")){
    	$result = call_user_func("form{$form_id}_hook_post_validation",$user_input);
    	if($result !== true){
    		$process_result['custom_error'] = $result;
    	}
    }


    This code would call our custom validation code after the built-in validation being called.

    2) Modify hooks/custom_hooks.php, add this code:

    function form99_hook_post_validation($user_input){
    
       $inv = $user_input['element_1'];
    
       if ( strcasecmp($inv,"1111") != 0 && strcasecmp($inv,"2222") != 0
       			 && strcasecmp($inv,"3333") != 0 && strcasecmp($inv,"4444") != 0
       			 && strcasecmp($inv,"5555") != 0 ){
       	$status = 'Registration is only open to recipients with an authorized promo code.
       					   Please try again or contact XXXX';
       }else{
       	$status = true;
       }
    
       return $status;
      }


    This is our custom validation code.

    In this case my form_id is 99 and my element name is element_1. Simply adjust these to match your form id and element name.

    If those sounds too difficult, send me an email and I'll help you figuring out.


    MachForm Founder

    Posted 16 years ago #
  3. mpivon
    Member

    Okay. Cool. This worked... sort of. However, I have more than one field on the form that requires error checking. So, following your example, I did the following:

    Into "Post-Functions.php" I inserted the following code:

    //post_validation hook

    if(function_exists("form{$form_id}_hook_post_validation")){
    $result = call_user_func("form{$form_id}_hook_post_validation",$user_input);
    if($result !== true){
    $process_result['custom_error'] = $result;
    }
    }

    //ticket validation hook
    if(function_exists("form{$form_id}_hook_tix_validation")){
    $result = call_user_func("form{$form_id}_hook_tix_validation",$user_input);
    if($result !== true){
    $process_result['custom_error'] = $result;
    }
    }

    THEN... I added the following code to the Hooks file:

    function form2_hook_post_validation($user_input){

    $inv = $user_input['element_1'];

    // Insert office codes here - these are for ONTARIO ONLY

    if (
    strcasecmp($inv,"2788") != 0
    && strcasecmp($inv,"2816") != 0
    && strcasecmp($inv,"2906") != 0
    && strcasecmp($inv,"3104") != 0

    //Promo code

    && strcasecmp($inv,"9999") != 0
    )

    {
    $status = 'Registration is only open to persons with a valid Promotional code';
    }
    else{
    $status = true;
    }
    return $status;
    }

    function form2_hook_tix_validation($user_input){

    $tix = $user_input['element_7'];

    if (
    strcasecmp($tix,"1") != 0
    && strcasecmp($tix,"2") != 0
    && strcasecmp($tix,"3") != 0
    && strcasecmp($tix,"4") != 0
    && strcasecmp($tix,"5") != 0
    )

    {
    $status = 'Max five tickets';

    }

    else{
    $status = true;
    }

    return $status;
    }

    So... I can't figure this out? Help?

    Posted 16 years ago #
  4. yuniar

    Okay, I think you won't need to create another code inside post-functions.php. You can remove that code.

    Now, what you need to do is to merge the code in hooks/custom_hooks.php
    You've created two function, simple merge the code from form2_hook_tix_validation() to form2_hook_post_validation(). So the whole function would be something like this:

    function form2_hook_post_validation($user_input){
    
    	$inv = $user_input['element_1'];
    
    	// Insert office codes here - these are for ONTARIO ONLY
    	if (strcasecmp($inv,"2788") != 0
    		&& strcasecmp($inv,"2816") != 0
    		&& strcasecmp($inv,"2906") != 0
    		&& strcasecmp($inv,"3104") != 0
    		&& strcasecmp($inv,"9999") != 0)
    	{
    		$inv_status = 'Registration is only open to persons with a valid Promotional code';
    	}
    	else{
    		$inv_status = true;
    	}
    
    	if($inv_status !== true){
    		return $inv_status;
    	}
    
    	$tix = $user_input['element_7'];
    	if (
    		strcasecmp($tix,"1") != 0
    		&& strcasecmp($tix,"2") != 0
    		&& strcasecmp($tix,"3") != 0
    		&& strcasecmp($tix,"4") != 0
    		&& strcasecmp($tix,"5") != 0
    	){
    		$tix_status = 'Max five tickets';
    	}else{
    		$tix_status = true;
    	}
    
    	return $tix_status;
    
    }

    If you have more fields, do the same as above, just add the code into that function.
    Let me know if you have any difficulty.


    MachForm Founder

    Posted 16 years ago #
  5. mpivon
    Member

    That's AWESOME.

    I'm sure that a lot of other folks will (eventually) benefit from this!

    Thanks.

    Posted 16 years ago #
  6. yuniar

    That's just great. Glad to help.


    MachForm Founder

    Posted 16 years ago #
  7. mmason
    Member

    I would like to add a post validation hook and I see how this is done from reading these posts. However since I am not blessed with line numbers in the php scripts. Could some one please give me another "landmark" to search for besides a line number as posted by Yuniar about 3 months ago:

    "Modify includes/post-functions.php, insert the code below into line 772, above the query line........"

    Tanks very much,
    Mike

    Posted 16 years ago #
  8. yuniar

    Mike, you will need to use text editor which has line numbers.

    One free alternative is to use this: http://notepad-plus.sourceforge.net/uk/site.htm

    "Notepad++ is a free source code editor and Notepad replacement, which supports several programming languages, running under the MS Windows environment."


    MachForm Founder

    Posted 16 years ago #
  9. mmason
    Member

    Yuniar - Thanks very much. - Mike

    Posted 16 years ago #

RSS feed for this topic

Reply