PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Custom validation help needed

  1. cazwilson
    Member

    Hi,

    I need to apply an ABN (Australian Business Number) validation formula to 3 fields in my form, and am having trouble getting it to function.

    I have applied this validation code - http://www.clearwater.com.au/code - to hook samples and this is what I have come up with:

    <?php
    
    // Application form ABN validation
    
    add_filter('gform_validation_2', 'custom_validation');
    function custom_validation($validation_result){
    
    function ValidateABN($abn)
    {
        $weight = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
        $form = $validation_result["form"];
    
    	foreach($form['fields'] as &$field){
    
            // ABN field ID
            if($field['id'] = input_2_16||input_2_26||input_2_49)
                continue;
    
        // strip anything other than digits
        $abn = preg_replace("/[^\d]/","",$abn);
    
        // check length is 11 digits
        if (strlen($abn)!=11) {
            // ABN is not valid - must have 11 digits
            return false;
        }
    
        // apply ato check method 
    
        $sum = 0;
        for ($i=0;$i<11;$i++) {
            $digit = $abn[$i] - ($i ? 0 : 1);
            $sum += $weight[$i] * $digit;
        }
        return ($sum % 89)==0;
    
       $field['failed_validation'] = true;
       $field['validation_message'] = 'The ABN you have entered is not valid.';
    
       $validation_result['is_valid'] = false;
       $validation_result['form'] = $form;
    
       break;
    }
    
        // we end by returning the validation result
        return $validation_result;
    }
    }
    
    ?>

    where the form ID is 2 and the fields to be validated are input_2_16, input_2_26 and input_2_49.

    Filling in a wrong number is not setting off an error message.

    Help with this would be greatly appreciated.

    Posted 13 years ago on Friday March 25, 2011 | Permalink
  2. Hi Cazwilson,

    Paste the following code into your theme's functions.php file: http://pastie.org/1721964

    Instead of validating these by the field ID, I've updated the code to validate by a CSS class. Any field you'd like to validate with the ABN function, just add this to the CSS class property: "validate-abn". Here is an exampe: http://grab.by/9I0b

    Posted 13 years ago on Sunday March 27, 2011 | Permalink
  3. cazwilson
    Member

    Hi David,

    Thank you so much for your time.

    On a single page form with no conditional logic it does works in so much as displaying this message:
    "There was a problem with your submission. Errors have been highlighted below".
    But there is no field specific error message.

    Also, if the field is not visible - that is, on another page of a multi-page form, or hidden due to conditional logic, the above error message appears - because nothing has been entered in the field.

    Would it be possible to work around these issues?

    Thanks again.
    CW

    Posted 13 years ago on Monday March 28, 2011 | Permalink
  4. Hi Cazwilson,

    Here is some updated code that addresses the multi-page and non-visible scenarios: http://pastie.org/1727617

    I was unable to recreate the first issue you reported. The field in error is correctly labeled in my testing of this code. I used the sample ABN number provided on the following page to test:

    http://www.ato.gov.au/businesses/content.asp?doc=/content/13187.htm

    Posted 13 years ago on Monday March 28, 2011 | Permalink
  5. cazwilson
    Member

    Hi David,

    The multi-page and non-visible fixes are working - thank you!

    I am still not getting an error message, though. The form isn't unusual in any way except I have added two column css classes to some fields (not the ABN one).

    I tested the field as Required - the standard "This field is required" message appears if the field is empty. But if I add less than 11 digits, or a wrong number, the same thing happens. The error message appears at the top of the page, but no error message on the field.

    I tested it without Required checked, too. No success.

    I have copied and pasted your code without editing. And applied the validate-abn css class to the field. There is no error if the number is correct, too, btw. So that is working.

    I'm stumped at what to look at next.

    The form's website is currently password protected during development. Otherwise I could post the URL here.

    Posted 13 years ago on Tuesday March 29, 2011 | Permalink
  6. @cazwilson Without being able to see the form it's real difficult to determine the issue.

    Posted 13 years ago on Tuesday March 29, 2011 | Permalink
  7. cazwilson
    Member

    OK. Carl. I have sent the access info via the Priority Support form.

    Thanks,
    Carolyn

    Posted 13 years ago on Thursday March 31, 2011 | Permalink
  8. cazwilson
    Member

    Thank you, David, for your help via email, and your fix:

    Just add this line:
    $validation_result['form'] = $form;
    ...directly before returning the $validation_result, like so: http://grab.by/9MHO

    That worked perfectly!

    Your assistance has been wholehearted and has really saved my day.

    Posted 13 years ago on Tuesday April 5, 2011 | Permalink

This topic has been resolved and has been closed to new replies.