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.

validating name field

  1. I am trying to validate the name field in one of my forms to avoid submission of the default value (which the design uses in place of the label). See the footer form: http://www.envisiondemo.com/acumen/

    There is a bit of complication due to the fact that the name field has the first and the last name input. For now, I am trying to validate the first name input.

    I've tried this function (based on http://www.gravityhelp.com/documentation/page/Using_the_Gravity_Forms_%22gform_validation%22_Hook):

    function validate_footer() {
    	$form = $validation_result["form"];
    	foreach($form['fields'] as &$field){
    		if(strpos($field['cssClass'], 'form_name') === false) continue;
    		$field_f_value = rgpost("input_{$field['input_2_2_3']}");
    		$is_f_valid = is_val_fname($field_f_value);
    		if($is_f_valid) continue;
    		$validation_result['is_valid'] = false;
    		$field['failed_validation'] = true;
    		$field['validation_message'] = 'The first name that you have entered is not valid.';
    		$validation_result['form'] = $form;
    		return $validation_result;
    	}
    }
    add_filter('gform_validation_2', 'validate_footer');
    
    function is_val_fname($field_f_value) {
    	if ($field_f_value != 'first *') { return true; }
    }

    I added the class "form_name" to the CSS Class name field in the form editor, which applies to the wrapper of both the first and last name inputs.

    I also found another page that gave a different example (http://www.gravityhelp.com/documentation/page/Gform_field_validation), so I tried this:

    add_filter("gform_field_validation_2_2_3", "custom_validation", 10, 4);
    function custom_validation($result, $value, $form, $field){
    
        if($result["is_valid"] && ($value != 'first *')){
            $result["is_valid"] = false;
            $result["message"] = "Please enter your first name.";
        }
        return $result;
    }

    When I click "Send" (submit), the form either spins indefinitely (with Ajax) or says "Oops we could not locate your form" (without Ajax) with the first function and just submits with the default value still with the second function. What am I doing wrong?

    Posted 9 years ago on Thursday July 12, 2012 | Permalink