vendredi 26 février 2016

Laravel 5 - Skip password confirmation on Update

I created some simple CMS with Laravel 5.2 and in the backend users can create & edit users. There is a simple form for that:

@section('content')
{!! Form::model($user, [
    'method' => $user->exists ? 'put':'post',
    'route' => $user->exists ? ['backend.users.update', $user->id] : ['backend.users.store'],
]) !!}

    <div class="form-group">
        {!! Form::label('name') !!}
        {!! Form::text('name', null, ['class'=>'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('email') !!}
        {!! Form::text('email', null, ['class'=>'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('password') !!}
        {!! Form::password('password', null, ['class'=>'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('password_confirmation') !!}
        {!! Form::password('password_confirmation', null, ['class'=>'form-control']) !!}
    </div>

    {!! Form::submit($user->exists ? 'Save User' : 'Create New User', ['class'=>'btn btn-primary']) !!}

{!! Form::close() !!}
@endsection

When you create a new user - password must be confirmed.

But when you edit some user - the problem is that you always have to confirm password. For example, if I just change 'Name' - I still have to confirm password.

Here is my Requests\UpdateUserRequest.php:

public function rules()
{
    return [
        'name'=>['required'],
        'email'=>['required', 'email', 'unique:users,email,'.$this->route('users')],
        'password' => ['required_with:password_confirmation', 'confirmed']
    ];
}

and here is UserController.php:

public function edit($id)
{
    $user = $this->users->findOrFail($id);
    return view('backend.users.form', compact('user'));
}

public function update(Requests\UpdateUserRequest $request, $id)
{
    $user = $this->users->findOrFail($id);
    $user->fill($request->only('name', 'email', 'password'))->save();
    return redirect(route('backend.users.edit', $user->id))->with('status', 'User has been updated');
}

When you open the form for editing, it looks like this:

Edit User Form

As you can see - when I open edit form each field of the form has been filled except for Password Confirmation.

How can I solve this problem (If I don't change the password but other fields - I don't need to confirm password)?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1n1tMDC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire