Kenny Nataniel
Kenny Nataniel

Reputation: 39

Laravel validation error

Why my validation is not working, but if i commend the validation, the code will successfully running. [Here is my Controller][1]

function changepassword(Request $req){

    $this->validate(
        $req,
        [
            'currpassword' => 'required | min:5',
            'newpassword' => 'required | min:5',
            'confirmnewpassword' => 'required| same:newpassword |min:5' 
        ]);

    // Validasinya gajalan

    $id = $req->input('id');
    $data = User::find($id);

    $currpassword = $req->input('currpassword');
    $newpassword = $req->input('newpassword');
    $confirmnewpassword = $req->input('confirmnewpassword');

    $currpass = $req->input('currpassword'); // password yang lama

    $currpasshash = Hash::make('currpassword');

    if (Hash::check($currpass , $data->password)) { 
        $data->password = Hash::make($newpassword);
        $data->save();
        return redirect('profile'); 

    }
    else{
        return redirect('changepassword');
    }


}

Upvotes: 2

Views: 599

Answers (3)

Jay Chauhan
Jay Chauhan

Reputation: 334

You can try this code.

function changepassword(Request $req) {
    $validatedData = $request->validate([
        'currpassword' => 'required|min:5',
        'newpassword' => 'required|min:5',
        'confirmnewpassword' => 'required|same:newpassword|min:5' 
    ]);
}

Displaying The Validation Errors

<h1>Create Post</h1>

@if ($errors->any())
   <div class="alert alert-danger">
      <ul>
           @foreach ($errors->all() as $error)
               <li>{{ $error }}</li>
           @endforeach
      </ul>
   </div>
@endif

For more information visit Validation in Laravel

I hope this will help you...

Upvotes: 0

Manojkumar B
Manojkumar B

Reputation: 216

There is a space in between the field named newpassword and pip | symbol and laravel identifies the field name as "newpassword " instead of "newpassword" which results always in error.

Remove the space and try:

'confirmnewpassword' => 'required | same:newpassword| min:5' 

Upvotes: 1

Sazzadur Rahman
Sazzadur Rahman

Reputation: 2930

You have passed wrong parameter to your validate method, try this,

$this->validate(
    $req->all(), [
        'currpassword' => 'required | min:5',
        'newpassword' => 'required | min:5',
        'confirmnewpassword' => 'required| same:newpassword |min:5' 
    ]);

Upvotes: 2

Related Questions