Tagged: datepicker
- This topic has 4 replies, 3 voices, and was last updated 7 years, 5 months ago by
Jason.
-
AuthorPosts
-
-
September 8, 2014 at 12:49 pm #2385
afspnationalMemberIs there a way to create validation for a datepicker that guarantees the date selected is a minimum of x number of days out? For instance, I’d like to use the datepicker to have clients provide a due date, but would also adhere to a minimum timeframe (say 14 days).
Thanks,
-
September 8, 2014 at 1:25 pm #2386
JasonKeymasterCheck this out on how to create custom validations: https://piklist.com/user-guide/
You can also see an example of a date validation I made in my PiklistHelper class (free to use): https://gist.github.com/JasonTheAdams/4a9fc4b2ad0104ab219f (search for “check_date_range).
What you’ll need to do is use the strtotime() function with something like strtotime(‘-2 weeks’), then compare the user date to that date.
Hope this helps! 🙂
-
September 8, 2014 at 4:15 pm #2397
afspnationalMemberHi Jason,
Thanks for the tip. I had found your gist before but had no luck in implementing it. Below is the code I’ve cobbled together so far. Can you let me know if I’m on the right track?
piklist('field', array( 'type' => 'datepicker' ,'scope' => 'post_meta' ,'field' => 'due_date' ,'label' => 'Due Date' ,'description' => 'Choose the due date for the project.' ,'attributes' => array( 'class' => 'text' ) ,'options' => array( 'dateFormat' => 'M d, yy' ,'firstDay' => '0' ) ,'validate' => array( array( 'type' => 'valid_date' ) ) )); add_filter('piklist_validation_rules', 'check_date'); function check_date() { $validation_rules = array( 'valid_date' => array( 'callback' => 'validate_date' ) ); return $validation_rules; } function validate_date($value, $field, $arguments) { $dates = $values[0]; $due_date = strtotime($dates['due_date']); $min_date = strtotime('-2 weeks'); if ( $min_date >= $due_date ) return __('Due date must be at least 2 weeks in advance.'); }
-
-
September 8, 2014 at 2:51 pm #2394
MarcusMemberYou may also want to control the days allowable to be clicked by setting the minDate and maxDate such as this:
$("#txtFromDate").datepicker({ minDate: "-15D", maxDate: "+15D" });That will make it so they can only select the 15 days previous or later.
Marcus
-
September 9, 2014 at 9:56 am #2402
JasonKeymasterHi afspnational,
Couple of things:
First, you can’t put the callback function in the same file as the Piklist fields. Honestly, I’m not sure why this is, but I know it to be true. If you move the “piklist_validation_rules” hook and function to your functions.php (or something like it), then that much will work.
Second, the way validations work is that if the callback returns a string it means that the validation failed and it displays the string you provide (which you’re doing). But, if the validation is to pass, the callback must return true.
Other than that, it looks pretty good! Hope this helps! 🙂
P.S. You’re more than welcome to leave a comment on the PiklistHelper gist for help. This sort of validation would make a perfect addition to the list of validations. Perhaps the best thing would be to move it to a Github repository so I can include a README for instructions..
-
-
AuthorPosts
- You must be logged in to reply to this topic.