0

I try to find the proper logic to add some more rules on a previous rules set (that was already defined) if some condition is met.

$form_validation_rules = v::key('password_current', v::length(6))
->key('password', v::length(6))
->key('password_confirm', v::length(6))
->equals($_POST['password'])->validate('password_confirm');

if($this->admin_role->owner === 1) {
   $form_validation_rules->key('username', v::length(3))->key('username', v::alnum());
}

In the above, the first set of rules is set and, if the $this->admin_role->owner === 1 condition is met, add some more rules. The output is, of course, Error: Call to a member function key() on bool

My question is: how can I extend an existing set of rules later on, in code (based on various logic conditions) ?

Thank you in advance!

punctweb
  • 45
  • 6

1 Answers1

0

Nevermind :) My code logic was wrong. This is the working way:

$form_validation_rules = v::key('password_current', v::length(6))
            ->key('password', v::length(6))
            ->keyValue('password_confirm', 'equals', 'password');

        if($this->admin_role->owner === 1) {
            $form_validation_rules->key('username', v::length(3))->key('username', v::alnum());
        }

In my initial version, the equals() usage was wrong. Also, the keyValue() method saves time :)

punctweb
  • 45
  • 6