Domanda

I'm having troubles updating a record. I have a page where users can register, this works flawlessly and has 4 fields: email, username, password and confirm password. This is a simple registration page and I dont want to turn off the visitors by presenting a lot of stuff to be filled out like full name and country, so these 2 fields can be updated on their "update profile" page. The profile page is separated into areas, the main area is a single form where only these 2 fields can be updated so no username, email, password fields here - only fullname and country.

Controller Update Profile Code

$user = User::find($id);

    $user->fullname = Input::get('fullname');
    $user->country = Input::get('country');


    if (!$user->save())
    {

        return Redirect::to('edit-profile')->withInput()->withErrors($user->errors());  

    } else {


        return Redirect::to('edit-profile')->withMessage('Profile successfully updated!');

    }       

My User Model rules. I'm using Ardent:

public static $rules = array(
  'username' => 'required|between:3,20|unique:users|alpha_dash',
  'email' => 'required|email|unique:users',
  'password' => 'required|min:5|confirmed',
  'password_confirmation' => 'min:5',
  'fullname' => 'between:5,50',
  'country' => 'between:3,50'
);

Problem here is that it returns an error message saying "Passwords do not match.". So it seems like Laravel is adding the password field in the query and also tries to validate if the passwords match. I do not want to create a separate model or a separate rules for this. How can I solve this?

È stato utile?

Soluzione

To make it work when you do not display a password you can test if you are displaying it, then make it a required field, in the controller:

if ($user->exists){
    $user::$rules['password'] = (Input::get('password')) ? 'required|min:5|confirmed' : '';
    $user::$rules['password_confirmation'] = (Input::get('password')) ? 'required' : '';
}
$user->save();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top