Created
March 4, 2014 14:59
-
-
Save gicolek/9347980 to your computer and use it in GitHub Desktop.
Full Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'gform_field_input', 'gf_field_input', 10, 5 ); | |
wp_enqueue_script( 'jquery-ui-datepicker' ); | |
/** | |
* Overwrite gravity forms input field | |
* | |
* @hook gform_field_input | |
*/ | |
function gf_field_input($input, $field, $value, $lead_id, $form_id) { | |
// we don't want to change the admin look behavior of the form field | |
if ( IS_ADMIN ) | |
return $input; | |
// lets create custom form html in case it had the css class assigned | |
if ( $field['cssClass'] === 'gf-date-picker' ) { | |
// get the global post values for use with the get_field function | |
global $post; | |
$id = $post->ID; | |
// have we set any values for the location and date field | |
if ( get_field( 'loc_date', $id ) ) { | |
// lets star the output buffering | |
ob_start(); | |
gf_generate_datepicker( $field['id'], $value ); | |
// lets get the output | |
$input = ob_get_clean(); | |
} | |
} | |
return $input; | |
} | |
/** | |
* Generate datepicker custom field | |
* | |
* @param int $id | |
* @param string $value | |
*/ | |
function gf_generate_datepicker($id, $value) { | |
// the value here is used for the pagination | |
?> | |
<input type="text" name="input_<?php echo $id; ?>" class="datepicker" value="<?php echo $value; ?>" /> | |
<?php | |
} | |
add_action( 'wp_head', 'gf_trigger_picker' ); | |
function gf_trigger_picker() { | |
global $post; | |
$id = $post->ID; | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
<?php if ( get_field( 'loc_date', $id ) ): ?> | |
availableDates = function(date) { | |
// create the proper format for comparison | |
var dmy = date.getFullYear() + '' + ('0' + (date.getMonth() + 1)).slice(-2) + '' + ('0' + date.getDate()).slice(-2); | |
// check what date is available | |
if (dmy === '<?php echo get_field( 'loc_date', $id ); ?>') { | |
return [true, "available", "Available"]; | |
} else { | |
return [false, "", "unAvailable"]; | |
} | |
}; | |
$(".datepicker").datepicker( | |
{ | |
// lets make sure that the dates are limited to the values of our ACF field | |
beforeShowDay: availableDates | |
} | |
); | |
<?php endif; ?> | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'gform_pre_submission_1', 'gf_form_pre_submission' ); | |
/** | |
* Presubmission form handler | |
* | |
* @hook gform_pre_submission | |
*/ | |
function gf_form_pre_submission($form) { | |
// iterate over the fields to obtain specific field's id | |
foreach ( $form['fields'] as $field ) { | |
// check if the date picker class has been added | |
if ( $field['cssClass'] == 'gf-date-picker' ) { | |
// get the field id | |
$id = $field['id']; | |
// do whatever you want with the values here (change the date format, etc.) | |
$_POST['input_' . $id] = $_POST['input_' . $id]; | |
} | |
} | |
} | |
add_filter( "gform_field_validation_1", 'gf_custom_validation', 10, 4 ); | |
/** | |
* Presubmission form handler | |
* | |
* @hook gform_pre_submission | |
*/ | |
/** | |
* Custom GF validation function used for pagination and required fields | |
* | |
* @return string | |
*/ | |
function gf_custom_validation($result, $value, $form, $field) { | |
// lets validate only our custom field | |
if ( $field['cssClass'] === 'gf-date-picker' ) { | |
// let's obtain separate strings for each part of the date | |
$value = array_map( 'intval', explode( '/', $value ) ); | |
// if the field is not valid then return the user | |
if ( !checkdate( $value[0], $value[1], $value[2] ) ) { | |
// add error message to the end user | |
$result["is_valid"] = false; | |
$result["message"] = "Wrong date, please check the format and make sure it's a valid one. "; | |
} else { | |
$result["is_valid"] = true; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment