On Wed, Apr 28, 2010 at 5:29 AM, Ed Propsner <[email protected]> wrote:
> Well I either never tried to convert to a string or I mucked something up
> (the latter of the two seems most likely) because it took the string with no
> arguments, go figure. I expected as much.
> Now here is a new question. Let's say (hypothetically) that I was so anal
> retentive that I absolutely had to do the date conversion in the model
> instead of the controller, I assume there must be an easy way?
> I can get close with the following code, but sill no cigar.
> [rule]
> 'date' => array(
> 'convertDate' => array(
> 'rule' => 'date',
> 'message' => 'Date is required',
> 'allowEmpty' => false
> )
> )
> [/rule]
> [function]
> function convertDate($field)
> {
> $date =
> $field['date']['year'].'-'.$field['date']['month'].'-'.$field['date']['day'];
> return $date;
> }
> [/function]
> I can get the converted date into the rule but it usually just displays it
> as the 'message'. How do I validate the returned value of convertDate()
> instead of field 'date' ?
Hey, I think there's no problem with your approach:
'dob' => array(
'required' => true,
'allowEmpty' => false,
'rule' => 'date',
'message' => 'Some message here.'
)
There might be something wrong on the view, or did you do validation
on controller ?
In case you're need some conversion, you could use custom
validation[1]. Here's an example that use custom validation
that trying to validate dob to disallow age under 18:
'dob' => array(
'required' => array(
'required' => true,
'allowEmpty' => false,
'rule' => 'date',
'message' => 'Required'
),
'valid' => array(
'rule' => array('vDob', 18),
'message' => 'Must be above 18 years old'
)
)
function vDob($field, $maxAge) {
$studentYear = date('Y', strtotime($field['dob']));
$currentYear = date('Y');
if ( ($currentYear-$studentYear) < $maxAge ) {
return true;
} else {
return false;
}
return true;
}
Custom validation rule must returns boolean value.
[1] http://book.cakephp.org/view/150/Custom-Validation-Rules
--
regards,
gedex
blog: http://gedex.web.id
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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en