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

Gravity forms date function/hook to limit date span on validation

  • SOLVED

Hello,

I am struggling to find any online resources for this.

The question prize is an innitial starter as I need it answering asap, and I'm not sure if it's actually possible. If no one can answer it in time I i will have to abandon it. If someone can help, I will be happy to add another $50 to the prize. So the total prize is $70


The question..


I am using the <strong>Date field</strong>.

In this mode:<strong>dd/mm/yyyy</strong>


And upon form validation, I need to check if the date is within a date span. This is my date span below...

18/03/2013 - 28/7/2013

I guess this can done using the validation hook php that gravity forms show you. But will require some clever skills to create the function.

If someone does try and enter a invalid date, the form must not submit and the validation message should read...

<strong>Sorry, your invoice date is invalid.</strong>


Some useful links.

[[LINK href="http://www.gravityhelp.com/documentation/page/Date"]]http://www.gravityhelp.com/documentation/page/Date[[/LINK]]

[[LINK href="http://www.gravityhelp.com/documentation/page/Using_the_Gravity_Forms_%22gform_validation%22_Hook"]]http://www.gravityhelp.com/documentation/page/Using_the_Gravity_Forms_%22gform_validation%22_Hook[[/LINK]]


If anyone can help, that would be soooo awesome.

Thanks


Full prize full will go one person only.

Answers (2)

2013-03-13

Kyle answers:

Hello, could you send the form and input IDs to help make the function as accurate as possible


Josh Cranwell comments:

Form 4
Input 39

Thanks


Kyle comments:

Try this

add_filter("gform_field_validation_4_39", "validate_date", 10, 4);
function validate_date($result, $value, $form, $field){

$input_string = strtotime( $value );

$max_date = strtotime("28 July 2013");
$min_date = strtotime("18 March 2013");

if ($input_string < $min_date OR $input_string > $max_date )
{
$result["is_valid"] = false;
$result["message"] = "Sorry, your invoice date is invalid.";
}
return $result;
}


Kyle comments:

I did not test that and may need to run some tests on how my $input_string is working, let me know if it works.


Josh Cranwell comments:

Going to test now thanks


Kyle comments:

Okay so that worked well for me, let me know if you run into any troubles implementing that


Josh Cranwell comments:

OK, I have tested and it returns strange results.

This is my form settings... http://i.imgur.com/0o7xrzW.png

Using these dates...


$max_date = strtotime("28 July 2013");
$min_date = strtotime("18 March 2013");


These are my entrie tests...

1. <strong>18/04/2013</strong> = this should validate, but it does not?

http://i.imgur.com/6p5giJR.png


2. <strong>4/04/2013</strong> = this should validate, and it does.

http://i.imgur.com/MRlWV71.png


3. <strong>19/03/2013</strong> = this should validate, but it does not?

http://i.imgur.com/xGTaFB3.png


Can you help?

Thanks
Josh


Kyle comments:

The problem is with my $input_string var, one second let me adjust


Josh Cranwell comments:

THanks


Kyle comments:

Okay so the issue is dd/mm/yyyy is pretty much the only date format that php strtotime doesn't understand haha (works with mm/dd/yyyy)

http://www.php.net/manual/en/datetime.formats.date.php

Do you need it to be in that format? I could write something that converts yours if you need to


Josh Cranwell comments:

What about just a string replace?

$invoice_date = str_replace("/", "-", $value );
$input_string = strtotime( $invoice_date );

[[LINK href="http://www.php.net/manual/en/datetime.formats.date.php"]]http://www.php.net/manual/en/datetime.formats.date.php[[/LINK]]

Also, by the way, my form is an ajax form - how does this work on an ajax form?

Thanks


Kyle comments:

Should work fine in ajax. str_replace may have worked, I had already started working on the solution before you answered :)

This should work with your format:

add_filter("gform_field_validation_4_39", "validate_date", 10, 4);
function validate_date($result, $value, $form, $field){

$ex_val = explode ("/",$value);
$new_val = $ex_val[1].'/'.$ex_val[0].'/'.$ex_val[2];

$input_string = strtotime( $new_val );
$max_date = strtotime("28 July 2013");
$min_date = strtotime("18 March 2013");

if ($input_string < $min_date OR $input_string > $max_date )
{
$result["is_valid"] = false;
$result["message"] = "Sorry, your invoice date is invalid.";
}
return $result;
}


Josh Cranwell comments:

I've tested my str_replace adjustment, and it seems to be OK - would you recommend this be OK, what did you have in mind?


Josh Cranwell comments:

Ahhh I see what you have done. Very nice.

Cool. Well I think it works fine. I will add your extra prize money now. If I have any problems I will let you know.


Thanks you very much for your help.

Josh


Kyle comments:

Sounds good, yes either one results in same string so its up to you. Feel free to pm me if you need to. You're welcome

2013-03-13

Gabriel Reguly answers:

Hi Josh,

I can surely help you, just need a lunch break.

Meanwhile, you can increase the prize ;-)

Regards,
Gabriel

Edit: I see ktrusak offered help first, so I'll wait for him to deliver the solution.


Josh Cranwell comments:

Cool thanks