Home / Admin / Gravity Forms: Cambridge Caregivers Gravity Forms Snippets
Duplicate Snippet

Embed Snippet on Your Site

Gravity Forms: Cambridge Caregivers Gravity Forms Snippets

Credit Card Expiration Field on Cambridge Caregivers Engagement Form year dropdown set to current year and +10 years from now.

Code Preview
php
<?php
/**
 * Credit Card Expiration Field on Cambridge Caregivers Engagement Form year dropdown set to current year and +10 years from now
 */
add_filter('gform_date_min_year', function ($min_year, $form, $field) {
  return $form['id'] == 1 && $field->id == 172 ? date('Y') : $min_year;
}, 10, 3);
add_filter('gform_date_max_year', function ($max_date, $form, $field) {
  $new_date = date_create('now +10 year');
  return $form['id'] == 1 && $field->id == 172 ? $new_date->format('Y') : $max_date;
}, 10, 3);
/**
 * Proposed Start Date on Cambridge Caregivers Engagement Form year dropdown set to -1 and +5 years from now
 */
add_filter('gform_date_min_year', function ($min_year, $form, $field) {
  $new_date = date_create('now -1 year');
  return $form['id'] == 1 && $field->id == 37 ? $new_date->format('Y') : $min_year;
}, 10, 3);
add_filter('gform_date_max_year', function ($max_date, $form, $field) {
  $new_date = date_create('now +5 year');
  return $form['id'] == 1 && $field->id == 37 ? $new_date->format('Y') : $max_date;
}, 10, 3);
add_filter('gform_date_max_year', function ($max_date, $form, $field) {
  $new_date = date_create('now -18 year');
  return $form['id'] == 1 && $field->id == 14 ? $new_date->format('Y') : $max_date;
}, 10, 3);
/**
 * Populate current date
 * 
 * The following example populates a date field with the current date. It assumes that the
 * date field is configured to “Allow field to be populated dynamically” and that the
 * parameter name is set to “current_date”
 */
add_filter('gform_field_value_current_date', 'populate_current_date');
function populate_current_date($value) {
  $local_timestamp = GFCommon::get_local_timestamp(time());
  return date_i18n('m/d/Y', $local_timestamp, true);
}
/**
 * Make Fields Read Only with "gf_readonly" css class.
 * Targeting a Paragraph Text field
 * update '1' to the ID of your form
 */
add_filter('gform_pre_render_1', 'add_readonly_script');
function add_readonly_script($form) {
?>
  
    jQuery(document).on('gform_post_render', function() {
      /* apply only to a textarea with a class of gf_readonly */
      jQuery(".gf_readonly textarea").attr("readonly", "readonly");
      /* apply only to a input with a class of gf_readonly */
      jQuery(".gf_readonly input").attr("readonly", "readonly");
      /* apply only to a checkbox input within a field with class of gf_readonly */
      jQuery(".gf_readonly input[type=checkbox]").attr("onclick", "return false;");
      /* apply only to a radio button input within a field with a class of gf_readonly */
      jQuery(".gf_readonly input[type=radio]").attr("onclick", "return false;");
    });
  
<?php
  return $form;
}
add_action(&#039;gform_enqueue_scripts_6&#039;, &#039;enqueue_custom_script&#039;, 10, 2);
function enqueue_custom_script($form, $is_ajax) {
  if ($is_ajax) {
    wp_enqueue_script(&#039;custom_script&#039;, &#039;https://cdnjs.cloudflare.com/ajax/libs/cleave.js/1.6.0/cleave.min.js&#039;);
  }
}

Comments

Add a Comment