Hopefully someone can point me in the right direction..

I am trying to validate a "Date" field to reject dates that are not more than a month in the future.. Can you suggest how I could do this?

Thanks..

Comments

wipeout_dude’s picture

Adding to this as I thought about it.. Would also be great is I could validate that the times were between 8am and 6pm.. :)

Thanks..

g089h515r806’s picture

Date module is a vey popular field module.
Maybe we will add more support for it in the future, I do not know which the common validation rule is needed for date field.

g089h515r806’s picture

now it could be done through "Custom PHP function" validation. this article maybe helpfull for you,http://drupal.org/node/1325068 .

you need a little change to use it.

$item = $variables['item'];
$start_date = $item['value'];
$end_date =$item['value2'];
if($start_date>time()+30*24*60*60){
//does not pass validation
$flag = FALSE;
}
g089h515r806’s picture

Add date range validator for date module.Here is the document:
http://drupal.org/node/1438436

Here are some examples of date range:
{global}[2012-02-10 08:30:00, 2012-02-13 20:30:00]
{year}(02-10 08:30:00, 02-13 20:30:00]
{month}(07 08:30:00, 13 20:30:00)
{week}(1 08:30:00, 5 20:30:00)
{day}[08:30:00, 20:30:00)
{hour}[10:00, 35:00]
{minute}[00, 35]
{global}[now, +1 month]

It also support:
{global}[2015-02-10 08:30:00, 2012-02-13 20:30:00]
{year}(11-10 08:30:00, 02-13 20:30:00]
{month}(22 08:30:00, 06 20:30:00)
{week}(6 08:30:00, 0 20:30:00)
{day}[22:30:00, 08:30:00)
{hour}[50:00, 10:00]
{minute}[55, 15]

henryblyth’s picture

Version: 7.x-1.0-beta4 » 7.x-1.0-beta5

For those wondering about the difference between using [, (, ], and ) around the start and end dates, and the use of reversing the start and end values:

Before the start date, or after the end date, a square bracket [ ] means 'inclusive'. A parenthesis ( ) means 'exclusive'.

As an example:
{day}[22:30:00, 08:30:00) will not validate (due to reversing the values) if the submitted time value is inside the range of 08:30:01 to 22:30:00.

Note that 08:30:01 (one second past the required value) is exclusive to 08:30:00 whereas 22:30:00 is inclusive.

So, a submitted value that evaluates to a time of 08:30:00 is valid. However, 22:30:00 is invalid.


Reversing the values is effectively a not '!' operator upon the entire calculation. It is the last check to occur, so one's thought process should evaluate the range given, including the information above regarding inclusive/exclusive values, then 'not' the result.


Thanks g089h515r806 for this addition! Could you add this info to this project's documentation so it becomes more visible to others? (Assuming I have it correct of course!)

Cheers,
Henry Blyth

g089h515r806’s picture

Do you mean there is a logic error at {day}[22:30:00, 08:30:00)

I have not test it when the value is 22:30:00 or 08:30:00,

This is my understanding for {day}[22:30:00, 08:30:00)
valid values:
22:30:00
22:30:01
22:30:02
...
08:29:58
08:29:59

Unvalid values:
08:30:00
08:30:01
08:30:02
...
22:29:58
22:29:59

If it does not work as this, that means the there is a error for the logic.

g089h515r806’s picture

{day}(22:30:00, 08:30:00) means reverse of {day}[08:30:00,22:30:00]
{day}(22:30:00, 08:30:00] means reverse of {day}(08:30:00,22:30:00]
{day}[22:30:00, 08:30:00) means reverse of {day}[08:30:00,22:30:00)
{day}[22:30:00, 08:30:00] means reverse of {day}(08:30:00,22:30:00)

g089h515r806’s picture

{day}(22:30:00, 08:30:00) means reverse of {day}[08:30:00,22:30:00]
{day}(22:30:00, 08:30:00] means reverse of {day}(08:30:00,22:30:00]
{day}[22:30:00, 08:30:00) means reverse of {day}[08:30:00,22:30:00)
{day}[22:30:00, 08:30:00] means reverse of {day}(08:30:00,22:30:00)

g089h515r806’s picture

I have test it with latest code, the logic of the code is right.
Maybe i need write a document for this feature, it is a little difficult to understand it

henryblyth’s picture

Sorry g089h515r806, I didn't mean to cause confusion. I was explaining how I thought the logic worked - there wasn't an error.

In post #6 you explain it more succinctly. And yes, please write a document for this feature. I figured it out from my own testing, so it would be useful to others.

Thanks again,
Henry

g089h515r806’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.