I have done someting like this. I also submitted a behavior to the
Bakery (has not been published yet), but it's called
ConditionValidation.
I can pass the behavior here. In the same case as yours, I have in my
form a check box "Reset password" ( a dummy field 'Work.rstpass', when
it's checked the password and confirm password must be mandatory. When
I add a user the Work.rstpass is an hidden field set to 1.

The behavior  (saved in behaviors/condition_validation.php):

<?php
  /*
   * conditional behavior, allow to turn on/off validations based on
condition
   */

class ConditionalValidationBehavior extends ModelBehavior
{
  /* store the current validation, useful if case of multiple 'save'
in same action
   * see setConditionValidation
   */

  var $savedValidation = null;

  /* initialize the settings */

        function setup(&$model, $params = array())
        {
                 $this->settings = $params;
        }

        /* in case we need to restore the validation */

  function afterSave(&$model)
  {
     if($this->savedValidation)
        $model->validate = $this->savedValidation;
  }

  // set the model $validate array

  function setConditionalValidation(&$model, $restore = false)
  {
         if(empty($this->settings))
            return;

     if($restore)
        $this->savedValidation = $model->validate;

           /* the data in condition must be coded as $data['Model']['field']
(do not use $this->data) */

     foreach($this->settings as $condition)
     {
        if(isset($condition['condition']) && !
empty($condition['condition']))
           $f = create_function('$data', 'return '.
$condition['condition'].' ? true : false;');
        else
           $f = create_function('$data', 'return true;');

        /* reference to this->data passed to the function to evaluate
the condition,
           if 'condition' is not present, the condition will be
assumed as true
        */

          if($f(&$model->data))
          {
           $option = array_merge(array('remove' => array(), 'validate'
=> array()), $condition);

           /* remove the fields in $validate */

           if(!empty($option['remove']))
           {
              foreach($option['remove'] as $rmfield)
                  unset($model->validate[$rmfield]);
           }

           /* add some fields to validate */

           if(!empty($option['validate']))
           {
              foreach($option['validate'] as $key => $addfield)
                  $model->validate[$key] = $addfield;
           }
          }
     }

         return;
  }
}
?>

In order to have it working (because the beforeValidate is not a call
back for behaviors), you have 2 ways to do it :

1) In app_model :

        // handle the conditionValidation behavior

        function beforeValidate()
        {
     if(isset($this->actsAs['ConditionalValidation']))
        $this->setConditionalValidation();

     return parent::beforeValidate();
        }

2) In your model (ie : User model for example)

        function beforeValidate()
        {
        $this->setConditionalValidation();
         return true;
        }

My User model looks like this :

class User extends AppModel {

        var $name = 'User';
        var $validate = array(
                                     'center_id' => VALID_NOT_EMPTY,
                         'username'  => array(array('required' =>
VALID_NOT_EMPTY,
                                                    'rule' =>
array('custom', '/^[0-9a-z]+$/i'),
                                                    'message' =>
'alphaonly'),
                                              array('rule' =>
'uniqueUserCheck',
                                                    'message' =>
'usernotunique')),
                         'inipassword'  => array('rule' =>
array('checkPassword', 'P'),
                                                 'message' =>
'passwordsyntax'),
                         'confirmpassword' => array('rule' =>
array('checkPassword', 'C'),
                                              'message' =>
'passwordmismatch'),
                         'email'     => array(array('allowEmpty' =>
true,  'rule' => 'email'))
        );

  var $actsAs  = array('ConditionalValidation' =>
array(array('condition' => '$data[\'Work\'][\'rstpass\'] != 1',
 
'remove'    => array('inipassword', 'confirmpassword'),
 
'validate'  => array('name' => VALID_NOT_EMPTY))
                                                       )
                       );

Basically, the condition is evaluated beforeValidate ..if true all the
"remove" are removed from the validate array, and all "validate are
"added" to the validate array.
I use "inipassword" instead of password, because of the Auth component
hashing everything that has "password" as a name. I needed not hashed
because I verify the strength of password ...

Hope this helps.

On Aug 15, 3:44 pm, Charlie <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I saw this post but couldn't reply on it 
> anymore:http://groups.google.com/group/cake-php/browse_thread/thread/c7c1f546...
> and I was wondering if someone has any idea how to do this.
>
> I have my validation rules based on user registration. But if I want
> to have a form with resetting a user password it takes the validation
> rules which is logical. But I want to have a more slightly different
> validation for that password reset form (both are in the same model).
> f.e. While you register you can't leave the field username empty, but
> when you want to reset your password you can.
>
> Has anyone an idea how to solve this?
>
> Thanx!
> - Charlie


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to