Ask your WordPress questions! Pay money and get answers fast! (more info)

Time span limit function for gravity forms time field

  • REFUND REQUESTED

I am trying to create a validation hook for the time field, but I am following the [[LINK href="http://www.gravityhelp.com/documentation/page/Gform_field_validation"]]documentation[[/LINK]] and tje hook that I created is not working but it is so simple that it is baffling me to as why it is not working.

The validation hook is for the time select field. And this is in 12 hour mode.

As the documentation explains, the $value is what the user inputs. Using strtotime to convert the $value time given into an integer which I then compare.

add_filter("gform_field_validation_2_6", "validate_date", 10, 4);
function validate_date($result, $value, $form, $field){
$input_time = strtotime( $value );
$max_time = strtotime("05:00 pm");
$min_time = strtotime("09:00 am");
if ($input_time < $min_time OR $input_time > $max_time ) {
$result["is_valid"] = false;
$result["message"] = "Please select a time between 9:00 am and 5:00 pm";
}
return $result;
}


But what ever date I put in when testing it seems to always return the validation same message.

If you can help that would be great thanks.
Josh

Answers (3)

2013-09-17

Navjot Singh answers:

N/A. Read it in a hurry. Ignore.


Navjot Singh comments:

According to other gravity form docs, the actual variable is $validation_result and not $result. Check http://www.gravityhelp.com/documentation/page/Using_the_Gravity_Forms_%22gform_validation%22_Hook , https://gist.github.com/BronsonQuick/2203307 and http://wp.tutsplus.com/tutorials/plugins/robust-forms-with-gravity-forms/


Josh Cranwell comments:

Thanks for your help tho managed to get a good response from gf themselves...

// REQUEST CALL BACK TIME
add_filter("gform_field_validation_2_6", "validate_date", 10, 4);
function validate_date($result, $value, $form, $field){

$time = implode( ':', $value );
$full_time = substr_replace( $time, " ", -3, 1 );
$time_in_24_hour_format = date( 'H:i', strtotime( $full_time ) );

$input_time = strtotime( $time_in_24_hour_format );
$max_time = strtotime( '17:00' );
$min_time = strtotime( '09:00' );

if ($input_time < $min_time OR $input_time > $max_time ) {
$result["is_valid"] = false;
$result["message"] = "Please select a time between 9:00 am and 5:00 pm";
}
return $result;

}

2013-09-17

Liam Bailey answers:

Your code is fine. The most likely explanation is that you are not targetting the right element or form _field_validation_$form_$field - is there any chance you can give us a link to where the form is displayed.


Josh Cranwell comments:

Thanks for your help tho managed to get a good response from gf themselves...

// REQUEST CALL BACK TIME
add_filter("gform_field_validation_2_6", "validate_date", 10, 4);
function validate_date($result, $value, $form, $field){

$time = implode( ':', $value );
$full_time = substr_replace( $time, " ", -3, 1 );
$time_in_24_hour_format = date( 'H:i', strtotime( $full_time ) );

$input_time = strtotime( $time_in_24_hour_format );
$max_time = strtotime( '17:00' );
$min_time = strtotime( '09:00' );

if ($input_time < $min_time OR $input_time > $max_time ) {
$result["is_valid"] = false;
$result["message"] = "Please select a time between 9:00 am and 5:00 pm";
}
return $result;

}

2013-09-17

Eric P. answers:

So, if I understand your question, you're always getting the "Please select a time between 9:00 am and 5:00 pm" message, even when you enter a time between 9am and 5pm.

Here's a suggestion.

Change the line
$result["message"] = "Please select a time between 9:00 am and 5:00 pm";

to this:
$result["message"] = "Please select a time between 9:00 am and 5:00 pm. You entered \"$value\".;


That might give you some clue as to what is wrong. You might be validating the wrong field. You might be converting string to time when it's already a time value. If you make that change, you'll see more what is wrong with the value, causing the message to show.


Editing this since you've closed the question. I'll put up a discourse with more info as well.

I'm responding to:

<blockquote>Thanks but found answer another way thanks for you help tho, you were on right track as I tried this but could only get array to show in the string. It's really annoying cause I would like to know how to print_r these vars on submit.
</blockquote>
OK. If you had reported that to me when it happened, I would have made a suggestion to work around that. When you get "array" or "object" or some other non-helpful information from putting a variable into a string, you can use the print_r() function to put a human readable rendition of the variable into the string.

The print_r() function has a second optional parameter, and when it's set to "TRUE", it returns the human readable text instead of printing it. So this code would have shown the full contents of the array you were seeing with my initial suggestion:

$result["message"] = "Please select a time between 9:00 am and 5:00 pm. You entered '".print_r($value,TRUE)."'.";


Josh Cranwell comments:

Thanks but found answer another way thanks for you help tho, you were on right track as I tried this but could only get array to show in the string. It's really annoying cause I would like to know how to print_r these vars on submit.

This is what I used...

// REQUEST CALL BACK TIME
add_filter("gform_field_validation_2_6", "validate_date", 10, 4);
function validate_date($result, $value, $form, $field){

$time = implode( ':', $value );
$full_time = substr_replace( $time, " ", -3, 1 );
$time_in_24_hour_format = date( 'H:i', strtotime( $full_time ) );

$input_time = strtotime( $time_in_24_hour_format );
$max_time = strtotime( '17:00' );
$min_time = strtotime( '09:00' );

if ($input_time < $min_time OR $input_time > $max_time ) {
$result["is_valid"] = false;
$result["message"] = "Please select a time between 9:00 am and 5:00 pm";
}
return $result;

}