Skip to content

Instantly share code, notes, and snippets.

@mujahidi
Last active May 30, 2025 18:26
Show Gist options
  • Save mujahidi/06339c67a3bc53c0e479f6dd201f8935 to your computer and use it in GitHub Desktop.
Save mujahidi/06339c67a3bc53c0e479f6dd201f8935 to your computer and use it in GitHub Desktop.
GravityForms - Use custom conditional logic to display other fields which are not supported out of the box by GF
<?php
/**
* Validates the custom mask date field format MM/YYYY.
*/
add_filter('gform_field_validation_8', function($result, $value, $form, $field) {
$date_field_ids = [119, 120, 121]; // Field IDs of the custom date field
if (!in_array($field->id, $date_field_ids)) {
return $result;
}
if (empty($value)) {
return $result;
}
$date_parts = explode('/', $value);
if (count($date_parts) !== 2) {
$result['is_valid'] = false;
$result['message'] = 'Please enter date in MM/YYYY format';
return $result;
}
$month = intval($date_parts[0]);
$year = intval($date_parts[1]);
$current_year = (int)date('Y');
if ($month < 1 || $month > 12) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid month (01-12)';
return $result;
}
if ($year < 1950 || $year > $current_year) {
$result['is_valid'] = false;
$result['message'] = sprintf('Please enter a year between 1950 and %d', $current_year);
return $result;
}
return $result;
}, 10, 4);
function validation_pass_based_on_date($field_value, $result) {
if (empty($field_value)) {
return $result;
}
$date_parts = explode('/', $field_value);
if (count($date_parts) !== 2) {
return $result;
}
$month = intval($date_parts[0]);
$year = intval($date_parts[1]);
if ($month < 1 || $month > 12) {
return $result;
}
$current_year = (int)date('Y');
if ($year < 1950 || $year > $current_year) {
return $result;
}
try {
$input_date = new DateTime(sprintf('%04d-%02d-01', $year, $month));
$current_date = new DateTime();
$interval = $current_date->diff($input_date);
$year_diff = $interval->y + ($interval->m / 12);
} catch (Exception $e) {
return $result;
}
if ($year_diff >= 2) {
$result['is_valid'] = true;
}
return $result;
}
/**
* Validates the address field based on the custom date field value.
*/
add_filter('gform_field_validation_8_17', function($result, $value, $form, $field) {
$address_date_value = rgpost('input_119'); // Date field ID
return validation_pass_based_on_date($address_date_value, $result);
}, 10, 4);
add_filter('gform_field_validation_8_123', function($result, $value, $form, $field) {
$address_date_value = rgpost('input_120'); // Date field ID
return validation_pass_based_on_date($address_date_value, $result);
}, 10, 4);
add_filter('gform_field_validation_8_124', function($result, $value, $form, $field) {
$address_date_value = rgpost('input_120'); // Date field ID
return validation_pass_based_on_date($address_date_value, $result);
}, 10, 4);
add_filter('gform_field_validation_8_125', function($result, $value, $form, $field) {
$currently_employed_field = rgpost('input_44');
if( strtolower(trim($currently_employed_field)) === 'no' ){
$result['is_valid'] = true;
return $result;
}
$address_date_value = rgpost('input_121'); // Date field ID
return validation_pass_based_on_date($address_date_value, $result);
}, 10, 4);
/**
* Adds JavaScript for controlling address field visibility.
*/
add_action('wp_footer', function() {
?>
<script type="text/javascript">
document.addEventListener('gform/post_render', (event) => {
const formId = 8;
if (event.detail.formId !== formId) {
return;
}
// Address related fields
const dateFieldId = 119;
const oldAddressFieldId = 17;
const oldAddressDurationFieldId = 80;
// Bank related fields
const bankDateFieldId = 120;
const previousBankFieldId = 123;
const previousBankDurationFieldId = 78;
const previousBankReasonFieldId = 124;
// Employment related fields
const employmentDateFieldId = 121;
const previousEmploymentNameFieldId = 125;
const previousEmploymentDurationFieldId = 79;
// Address related elements
const dateField = document.getElementById(`input_${formId}_${dateFieldId}`);
const oldAddressField = document.getElementById(`field_${formId}_${oldAddressFieldId}`);
const oldAddressDurationField = document.getElementById(`field_${formId}_${oldAddressDurationFieldId}`);
// Bank related elements
const bankDateField = document.getElementById(`input_${formId}_${bankDateFieldId}`);
const previousBankField = document.getElementById(`field_${formId}_${previousBankFieldId}`);
const previousBankDurationField = document.getElementById(`field_${formId}_${previousBankDurationFieldId}`);
const previousBankReasonField = document.getElementById(`field_${formId}_${previousBankReasonFieldId}`);
// Employment related elements
const employmentDateField = document.getElementById(`input_${formId}_${employmentDateFieldId}`);
const previousEmploymentNameField = document.getElementById(`field_${formId}_${previousEmploymentNameFieldId}`);
const previousEmploymentDurationField = document.getElementById(`field_${formId}_${previousEmploymentDurationFieldId}`);
if (!dateField || !oldAddressField || !oldAddressDurationField) return;
if (!bankDateField || !previousBankField || !previousBankDurationField || !previousBankReasonField) return;
if (!employmentDateField || !previousEmploymentNameField || !previousEmploymentDurationField) return;
// Initially hide all conditional fields
oldAddressField.style.display = 'none';
oldAddressDurationField.style.display = 'none';
previousBankField.style.display = 'none';
previousBankDurationField.style.display = 'none';
previousBankReasonField.style.display = 'none';
previousEmploymentNameField.style.display = 'none';
previousEmploymentDurationField.style.display = 'none';
function isValidDate(month, year) {
if (month < 1 || month > 12) return false;
const currentYear = new Date().getFullYear();
if (year < 1950 || year > currentYear) return false;
return true;
}
function checkDateAndToggleFields(dateValue, fieldsToToggle) {
const datePattern = /^(0[1-9]|1[0-2])\/([0-9]{4})$/;
if (!datePattern.test(dateValue)) {
fieldsToToggle.forEach(field => field.style.display = 'none');
return;
}
const [monthStr, yearStr] = dateValue.split('/');
const month = parseInt(monthStr);
const year = parseInt(yearStr);
if (!isValidDate(month, year)) {
fieldsToToggle.forEach(field => field.style.display = 'none');
return;
}
const inputDate = new Date(year, month - 1);
const currentDate = new Date();
const yearDiff = (currentDate.getFullYear() - inputDate.getFullYear()) +
(currentDate.getMonth() - inputDate.getMonth()) / 12;
const displayStyle = yearDiff < 2 ? 'block' : 'none';
fieldsToToggle.forEach(field => field.style.display = displayStyle);
}
function checkAddressDate() {
checkDateAndToggleFields(dateField.value, [
oldAddressField,
oldAddressDurationField
]);
}
function checkBankDate() {
checkDateAndToggleFields(bankDateField.value, [
previousBankField,
previousBankDurationField,
previousBankReasonField
]);
}
function checkEmploymentDate() {
checkDateAndToggleFields(employmentDateField.value, [
previousEmploymentNameField,
previousEmploymentDurationField
]);
}
dateField.addEventListener('keyup', (e) => {
const cursorPos = e.target.selectionStart;
checkAddressDate();
e.target.setSelectionRange(cursorPos, cursorPos);
});
bankDateField.addEventListener('keyup', (e) => {
const cursorPos = e.target.selectionStart;
checkBankDate();
e.target.setSelectionRange(cursorPos, cursorPos);
});
employmentDateField.addEventListener('keyup', (e) => {
const cursorPos = e.target.selectionStart;
checkEmploymentDate();
e.target.setSelectionRange(cursorPos, cursorPos);
});
// Initial check for both date fields
checkAddressDate();
checkBankDate();
checkEmploymentDate();
});
</script>
<?php
});
// exclude custom conditional fields from submission
add_filter('gform_pre_submission_8', 'exclude_conditional_fields');
function exclude_conditional_fields($form) {
$address_date_value = $_POST['input_119']; // Date field ID
$address_validation_result = validation_pass_based_on_date($address_date_value, []);
if( is_array($address_validation_result) && isset($address_validation_result['is_valid']) ){
unset($_POST['input_17_1']);
unset($_POST['input_17_2']);
unset($_POST['input_17_3']);
unset($_POST['input_17_4']);
unset($_POST['input_17_5']);
unset($_POST['input_17_6']);
unset($_POST['input_80']);
}
$bank_date_value = $_POST['input_120']; // Date field ID
$bank_validation_result = validation_pass_based_on_date($bank_date_value, []);
if( is_array($bank_validation_result) && isset($bank_validation_result['is_valid']) ){
unset($_POST['input_123']);
unset($_POST['input_78']);
unset($_POST['input_124']);
}
$currently_employed_field = $_POST['input_44'];
$employment_date_value = $_POST['input_121']; // Date field ID
$employment_validation_result = validation_pass_based_on_date($employment_date_value, []);
if(
strtolower(trim($currently_employed_field)) === 'no' ||
( is_array($employment_validation_result) && isset($employment_validation_result['is_valid']) ) ){
unset($_POST['input_125']);
unset($_POST['input_79']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment