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.

Dynamically populate age

  1. I am trying to dynamically populate a field called "Age" with the age of the person filling out the form based on the date of birth they already filled out.

    This is on a multi-page form. The DOB field is a date field on the 1st page.
    I placed the "Age" field on the second page so the DOB field will have to validate before seeing the "Age" field. It is important that this happens on validation, not on submission as I have a lot of conditional logic based on age.

    Here is my code which I placed in functions.php:

    add_filter("gform_pre_submission_filter_2", "age");
    function age($form){
    $ageTime = strtotime($_POST["input_5"]);  // Get the person's birthday timestamp
    $t = time(); // current time
    $age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
    $year = 60 * 60 * 24 * 365;
    $ageYears = $age / $year;
    $age= floor($ageYears);
    $form["fields"][6]["defaultValue"] = $age; //set default value of Age field to the calculated age
     return $form;
    }

    Nothing is showing up in the Age field when I run this at this point. What am I doing wrong?

    Posted 11 years ago on Sunday May 27, 2012 | Permalink
  2. Ok, I figured it out.
    Here is the code with comments for anyone who else who wants to do this.

    add_filter('gform_validation_2', 'find_age');
    function find_age($validation_result) {
    
        // 2 - Get the form object from the validation result
        $form = $validation_result["form"];
    
        // 3 - Get the current page being validated
        $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;
    
        // 4 - Loop through the form fields
        foreach($form['fields'] as &$field){
    
            // 5 - If the field does not have our designated CSS class, skip it
            if(strpos($field['cssClass'], 'dob') === false)
                continue;
    
            // 6 - Get the field's page number
            $field_page = $field['pageNumber'];
    
            // 7 - Check if the field is hidden by GF conditional logic
            $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());
    
            // 8 - If the field is not on the current page OR if the field is hidden, skip it
            if($field_page != $current_page || $is_hidden)
                continue;
    
            // 9 - Get the submitted value from the $_POST
    		$field_value = rgpost("input_{$field['id']}");
    			if(is_array($field_value)){
    				$field_value = implode('/', $field_value);}//turns the date array into a string
    
    $ageTime = strtotime($field_value);  // Transform DOB from date format to timestamp
    $t = time(); // current timestamp
    $age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
    $year = 60 * 60 * 24 * 365;
    $ageYears = $age / $year;
    $age= floor($ageYears); //this is calculated age
    
    		foreach($form['fields'] as &$field2){
    			if(strpos($field2['cssClass'], 'age') === false) //loop through fields again to find the "Age" field
                continue;
    
    			$field2["defaultValue"] = $age; //add calculated age as default value to "Age" field
    }}
    $validation_result['form'] = $form; //manipulate $validation_result with modified $form
    
        // 15 - Return the validation result
        return $validation_result;
    
    }

    You will need to edit your form so that the css class of your date-of-birth field is "dob" and your Age field is "age."

    Posted 11 years ago on Sunday May 27, 2012 | Permalink
  3. Thanks for posting your solution!

    Posted 11 years ago on Tuesday May 29, 2012 | Permalink

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