Use case:

I'm creating a birthday field for an application and the users must be under a certain age. I need to have validation for "required" but also further validation for "less than X" and/or "greater than X".

This is mostly a placeholder for the patch that I'm going to need to submit to do this and to get your input KarenS, if you have any, on whether you like this idea or if it needs to happen outside of the module.

Comments

karens’s picture

Not sure if it is best in or out. I think it depends on how generic the code can be. I'm certainly open to reviewing code to add this in.

karens’s picture

Version: 4.7.x-1.x-dev » 5.x-1.x-dev

No new features will be going into 4.7. Moving this to 5.x.

greggles’s picture

fwiw (to anyone else looking for this) I enforce this via hook_nodeapi in a custom module.

This seems nice to have, but it would clutter the interface and I'm not sure how hard/easy it would be to code.

moshe weitzman’s picture

Title: Extended validation » Support maximum and minimum values for dates

CCK already has max/min value checking for certain field types. Seems like this should fit in without any new UI.

karens’s picture

Version: 5.x-1.x-dev » 7.x-1.x-dev

Moving this to HEAD. No new features will be added to this version, but it could be considered for the 5.2 version now in HEAD.

sun’s picture

http://drupal.org/node/99095 has been marked as duplicate of this.

Coyote’s picture

Really need this feature. Ideally, it would have to be something dynamic, not a static date, since it would have to not just check for a static minimum or maximum date, but would have to calculate based on the difference between the current date and the input date.

thepanz’s picture

+1 for this feature, I'm going to implement it for my next project.. should I start with 1.x or 2.x version?
I'd like to know if version 2.x will be stable/compatible for production sites soon or I have to stay with 1.x version? :)

Regards, and compliments to KarenS for the great work!

karens’s picture

I'm hoping to release the 5.2 version very soon. There are just a few more things to clean up, but I'm out of town for the next two weeks with limited time to work on it, so it may not be until after that.

This feature won't be in the initial release, but we can re-visit it after a release. Patches welcome, of course (make patches against 5.2).

arlinsandbulte’s picture

Status: Active » Needs review

There is a module called validation API that might do this:
http://drupal.org/project/validation_api

I think artscoop successfully used it for this purpose. #579194: Date CCK : User can enter date not in range

I will mark as needs review... hoping that someone can verify that Validation API indeed works well for this purpose...

arlinsandbulte’s picture

dhalgren has another method to do this using rules:
ON event "Content is going to be saved"
IF Unchanged content is xxxx
AND Numeric comparison is true
([node:field_datelink-yyyy]<2004)

DO Show a configurable message on the site
(date must be greater than 31/12/03)
and
Page redirect to node/ass/xxxx
(Immediately issue the page redirect)

Marked that issue a duplicate of this one.
#563276: date field validation

I think the validation api might be a better method, though.

artscoop’s picture

Hello,
indeed, it worked for a date field.
If you need it on a registration form, the form name is user_register. The date field name is usually field_[0], the value is $value[0]['date'].

robby.smith’s picture

subscribing

YK85’s picture

can anyone help with this for version 6.x?

robby.smith’s picture

having this feature in 6.x would be very helpful

joachim’s picture

Status: Needs review » Active

On D6, here's the code to put in hook_nodeapi:

  if ($op == 'validate') {
    $field_name = 'field_performance_date'; // your CCK field name here.
    $now = date_format(date_now(), DATE_FORMAT_DATETIME);
    $node_date = $node->{$field_name}[0]['value'];
    if ($node_date < $now) {
      form_set_error($field_name, 'Quit living in the past!');
    }
  }

or wrapped up as a helper function:

/**
 * Helper function to validate CCK date fields as either in the past or future.
 *
 * Call this from your hook_nodeapi() implementation.
 *
 * @param $node
 *  The $node parameter from hook_nodeapi().
 * @param $field_name
 *  The machine name of the CCK date field.
 * @param $required_future
 *  (optional) Omit this if you require the date to be in the future;
 *  pass in FALSE if you require it to be in the past.
 */
function _date_validate_node_date_now($node, $field_name, $required_future = TRUE) {
  $now = date_format(date_now(), DATE_FORMAT_DATETIME);
  $node_date = $node->{$field_name}[0]['value'];
  if ($required_future) {
    if ($node_date < $now) {
      form_set_error($field_name, 'Quit living in the past!');
    }
  }
  else {
    if ($node_date > $now) {
      form_set_error($field_name, 'Stop jumping the gun!');
    }
  }
}
arlinsandbulte’s picture

Status: Active » Closed (fixed)

Above is some custom code that can be used to solve this, I think.

More importantly (for D7 at least), a new Field Validation module has been released that looks nice:
http://drupal.org/project/field_validation....
There is a module for that!

So, I am closing this.

jmolinas’s picture

where do i put this code?

thanks

g089h515r806’s picture

If you use Drupal 7, try field validation module, the sub module "date validation" exactly solve this issue.
http://drupal.org/project/field_validation

document for date range validation:
http://drupal.org/node/1438436

alb’s picture

@joachim or who know solution
can explained better how use the code in
hook_nodeapi or wrapped up as a helper function

is necessary to create a module?
which are the steps?

Paul B’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Issue summary: View changes
Status: Closed (fixed) » Active

Validating the date range is rather basic functionality, I should not have to use the field_validation module for that.