简体   繁体   中英

kohana validation during update

In my application developed using Kohana, I have a form where user enters their registration information, My rules are correctly executing and the error message is displaying correctly if user ignores an important field or enters an email address in inappropriate form.

The problem is that i want to do the same validations while an his personal information at a later point of time, but i want to ignore the password fields which is usually empty when the form loads.

  • Is it really possible to make kohana ignore the blank password values while updating?

  • I am using the following code to update user data, I could see no
    validation is performed(which is defined in my Model_User ) when i use update_user() of Auth. Can somebody please throw some light on
    potential issues that hinders the execution of validation routines
    here?

if(Auth::instance()->logged_in()) {
    $values =  array();
    $now = new DateTime();
     $userobj = Auth::instance()->get_user();

    //$userobj->updatedon = $now->format('Y-m-d H:i:s'); // need research
    try {
        $userobj->update_user($_POST, array('email',
                                            'password',
                                            'firstname',
                                            'secondname',
                                            'lastname',
                                            'phone',
                                            'city_id'
        ));
        $values = Auth::instance()->get_user()->as_array();
    } catch (ORM_Validation_Exception $e) {
         $errors = $e->errors('models');
    }
    //...

This shouldn't be a problem. When registering users, make sure to use create_user() and add password specific rules to get_password_validation() .

Now the logic will only apply for registration and not for updating already existing users.

Also if you don't want to update the password , make sure to remove it from the list.

$updateColumns = array('email', 'password', ...);
if ( ! isSet($_POST['password']) || trim($_POST['password']) == "") {
    unset($updateColumns[1]);
}
$userobj->update_user($_POST, $updateColumns);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM