How to apply the “matches” validation rule in Kohana 3.1?

谁都会走 提交于 2019-12-06 05:38:17

Your syntax is correct, but I'm going to guess and say that your DB schema does not have a 'password_confirm' column so you are trying to add a rule to a field that doesn't exist.

Regardless, the right place to perform password confirm matching validation is not in your model but as extra validation that is passed to your model in your controller when you attempt to save.

Put this in your user controller:

$user = ORM::Factory('user');

// Don't forget security, make sure you sanitize the $_POST data as needed
$user->values($_POST);

// Validate any other settings submitted
$extra_validation = Validation::factory(
    array('password' => Arr::get($_POST, 'password'),
          'password_confirm' => Arr::get($_POST, 'password_confirm'))
    );

$extra_validation->rule('password_confirm', 'matches', array(':validation', 'password_confirm', 'password'));

try 
{
    $user->save($extra_validation);
    // success
}
catch (ORM_Validation_Exception $e)
{               
   $errors = $e->errors('my_error_msgs');
   // failure
}

Also, see the Kohana 3.1 ORM Validation documentation for more information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!