<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Gravity Support Forums Topic: Dynamically populate age</title>
		<link>https://legacy.forums.gravityhelp.com/topic/dynamically-populate-age</link>
		<description>Gravity Support Forums Topic: Dynamically populate age</description>
		<language>en-US</language>
		<pubDate>Sat, 04 Apr 2026 03:26:48 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>https://legacy.forums.gravityhelp.com/search.php</link>
		</textInput>
		<atom:link href="https://legacy.forums.gravityhelp.com/rss/topic/dynamically-populate-age" rel="self" type="application/rss+xml" />

		<item>
			<title>Rob Harrell on "Dynamically populate age"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/dynamically-populate-age#post-60636</link>
			<pubDate>Tue, 29 May 2012 11:47:35 +0000</pubDate>
			<dc:creator>Rob Harrell</dc:creator>
			<guid isPermaLink="false">60636@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Thanks for posting your solution!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gingram815 on "Dynamically populate age"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/dynamically-populate-age#post-60484</link>
			<pubDate>Sun, 27 May 2012 15:13:42 +0000</pubDate>
			<dc:creator>gingram815</dc:creator>
			<guid isPermaLink="false">60484@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Ok, I figured it out.&#60;br /&#62;
Here is the code with comments for anyone who else who wants to do this.&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;add_filter(&#38;#39;gform_validation_2&#38;#39;, &#38;#39;find_age&#38;#39;);
function find_age($validation_result) {

    // 2 - Get the form object from the validation result
    $form = $validation_result[&#38;quot;form&#38;quot;];

    // 3 - Get the current page being validated
    $current_page = rgpost(&#38;#39;gform_source_page_number_&#38;#39; . $form[&#38;#39;id&#38;#39;]) ? rgpost(&#38;#39;gform_source_page_number_&#38;#39; . $form[&#38;#39;id&#38;#39;]) : 1;

    // 4 - Loop through the form fields
    foreach($form[&#38;#39;fields&#38;#39;] as &#38;amp;$field){

        // 5 - If the field does not have our designated CSS class, skip it
        if(strpos($field[&#38;#39;cssClass&#38;#39;], &#38;#39;dob&#38;#39;) === false)
            continue;

        // 6 - Get the field&#38;#39;s page number
        $field_page = $field[&#38;#39;pageNumber&#38;#39;];

        // 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 &#124;&#124; $is_hidden)
            continue;

        // 9 - Get the submitted value from the $_POST
		$field_value = rgpost(&#38;quot;input_{$field[&#38;#39;id&#38;#39;]}&#38;quot;);
			if(is_array($field_value)){
				$field_value = implode(&#38;#39;/&#38;#39;, $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 &#38;lt; 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
$age= floor($ageYears); //this is calculated age

		foreach($form[&#38;#39;fields&#38;#39;] as &#38;amp;$field2){
			if(strpos($field2[&#38;#39;cssClass&#38;#39;], &#38;#39;age&#38;#39;) === false) //loop through fields again to find the &#38;quot;Age&#38;quot; field
            continue;

			$field2[&#38;quot;defaultValue&#38;quot;] = $age; //add calculated age as default value to &#38;quot;Age&#38;quot; field
}}
$validation_result[&#38;#39;form&#38;#39;] = $form; //manipulate $validation_result with modified $form

    // 15 - Return the validation result
    return $validation_result;

}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;You will need to edit your form so that the css class of your date-of-birth field is &#34;dob&#34; and your Age field is &#34;age.&#34;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gingram815 on "Dynamically populate age"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/dynamically-populate-age#post-60483</link>
			<pubDate>Sun, 27 May 2012 14:02:43 +0000</pubDate>
			<dc:creator>gingram815</dc:creator>
			<guid isPermaLink="false">60483@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;I am trying to dynamically populate a field called &#34;Age&#34; with the age of the person filling out the form based on the date of birth they already filled out.&#60;/p&#62;
&#60;p&#62;This is on a multi-page form.  The DOB field is a date field on the 1st page.&#60;br /&#62;
I placed the &#34;Age&#34; field on the second page so the DOB field will have to validate before seeing the &#34;Age&#34; field.  It is important that this happens on validation, not on submission as I have a lot of conditional logic based on age.&#60;/p&#62;
&#60;p&#62;Here is my code which I placed in functions.php:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;add_filter(&#38;quot;gform_pre_submission_filter_2&#38;quot;, &#38;quot;age&#38;quot;);
function age($form){
$ageTime = strtotime($_POST[&#38;quot;input_5&#38;quot;]);  // Get the person&#38;#39;s birthday timestamp
$t = time(); // current time
$age = ($ageTime &#38;lt; 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
$age= floor($ageYears);
$form[&#38;quot;fields&#38;quot;][6][&#38;quot;defaultValue&#38;quot;] = $age; //set default value of Age field to the calculated age
 return $form;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Nothing is showing up in the Age field when I run this at this point.  What am I doing wrong?
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
