Sunday, November 29, 2009

Re: validate as hour

I would just create a custom validation function:

/**
* function intRange
*
* Checks if a value is an integer within the
* range given (inclusive)
*
* @param $value to be checked
* @param $start of range
* @param $end of range
* @return bool
*/
function intRange($value,$start,$end) {
// Verify the value is an int
if (!is_int($value)) return false;

// Verify the value is within the range
if ($value<$start) return false;
if ($value>$end) return false;

// Passed all tests
return true;
}

use it in your validation array like this:

'hour' => array(
'rule' => array('intRange', 0, 23),
'message' => 'Please enter an integer between 0 and 23'
)


On Nov 26, 10:41 am, "ge...@dinnerbooking.com"
<ge...@dinnerbooking.com> wrote:
> I have an field which is an hour:
>
> "hour" => array(
>         "required" => true,
>         "allowEmpty" => false,
>         "rule" => "/^\d{1,2}+/",
> ),
>
> but how do I also specify that the number MUST be between 0 and 23 ?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: