Yes, you can validate *any* field in a model, even if it is not a part of
the model, just make sure that the rules in the model match the names of the
input fields. I use this functionality for both password and email
verification, as the user must enter them twice, my model validates and
makes sure that they match.
Instead of allowEmpty, try a minLength rule instead. Here's an example.
'password_new' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum 8 characters')
Also, for your question on matching fields, you can add a function to the
end of your model(or your App_Model if you want it available to every model)
Here's mine:
/*function to make sure that 2 entered fields are identical*/
function identicalFields( $field=array(), $compare_field=null )
{
foreach( $field as $key => $value )
{
$vi = $value;
$v2 = $this->data[$this->name][$compare_field];
if ($vi !== $v2)
{
return false;
}
else
{
continue;
}
}
return true;
}
and you can call this function like so:
'confirm_email' => array(
'rule' => array('identicalFields', 'email'),
'message' => 'E-Mail addresses must match')
Hope this helps!
In the name of Life, Liberty, and the pursuit of my sanity.
Siebren Bakker(Aevum Decessus)
On Wed, Jul 16, 2008 at 12:19, august.gresens <[EMAIL PROTECTED]> wrote:
>
> Hello
>
> I'm setting up a user registration form with a 'password_new' and
> 'password_confirm' fields.
>
> These fields are not in my database (and not official fields in my
> User model).
>
> Can I validate these fields using the validation rules in my User
> model? (I was under the impression from some other posts that this was
> possible).
>
> Here are the form calls in my view (register.ctp):
>
> <?php echo $form->password('password_new', array('label' => false)); ?
> >
> <?php echo $form->password('password_confirm', array('label' =>
> false)); ?>
>
> The authentication is set up like so in my User model:
>
>
> var $validate = array(
>
> 'password_new' => array(
> 'rule' => VALID_NOT_EMPTY,
> 'required' => true,
> 'allowEmpty' => false,
> 'message' => 'Passwords need to be a mimimum 8
> characters long'
> ),
>
>
> 'password_confirm' => array(
> 'rule' => VALID_NOT_EMPTY,
> 'required' => true,
> 'allowEmpty' => false,
> 'message' => 'Passwords need to be a mimimum 8
> characters long'
> ),
> )
>
>
> Yet, if I submit a form with both of these fields blank, no error
> message is generated.
>
> I also need to compare the values of these fields. Is there a way to
> do this in the controller or can it also be done through the model
> validation?
>
> Thanks,
>
> August
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---