user9746625
user9746625

Reputation:

laravel validation doesn't work

I am taking a laravel course an in order to follow the course I have to fix this issue, I checked everything, and apparently, everything is the same as the guy shows me. But the difference is that his validation works but not mine. It has to redirect me to the register_form when the validation is invalid but it doesn't.

Route

Route::get('/', function(){
    return view('welcome');
});

Route::get('/register', 'HomeController@register_form');

Route::post('/register', 'HomeController@register');

Controller

namespace CoolBlog\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller {

    public function register_form(){
        return view('register');
    }

    public function register(Request $request) {
        $this->validate($request, [
            'username' => 'min:5|max:30',
            'email' => 'email',
            'pass' => 'min:5',
            'pass2' => 'same:pass'
        ]);
    }
}

Html Page

@extends('layout.default')

@section('title', 'Registration')

@section('content')
    <form method="post" action="/register" >
        {{csrf_field()}}
        Name: <input type="text" name="username"><br>
        E-mail: <input type="text" name="email"><br>
        Password: <input type="password" name="pass2"><br>
        Re-Password: <input type="password" name="pass2"><br>
        <input type="submit" value="Register">
    </form>

@endsection

Upvotes: 0

Views: 2191

Answers (3)

Shahrukh Khan
Shahrukh Khan

Reputation: 46

Change this line:

Password: <input type="password" name="pass2"><br>
Re-Password: <input type="password" name="pass2"><br>

and replace it with this:

Password: <input type="password" name="pass"><br>
Re-Password: <input type="password" name="pass2"><br>

and in your validation if you want any field must be required then add required in the validation

$this->validate($request, [
            'username' => 'required|min:5|max:30',
            'email' => 'required|email',
            'pass' => 'required|min:5',
            'pass2' => 'required|same:pass'
        ]);

Upvotes: 1

Girdharilal Pandey
Girdharilal Pandey

Reputation: 94

I saw that you are not returning to any route after validating the form values. So, can you please try this code format. the validation method is also updated on upper level laravel versions

function store(Request $request)
    {
        request()->validate([
            'title' => 'required',
            'body' => 'required',
        ]);
        Post::create($request->all());
        return redirect()->route('posts.index')->with('success', 'Post created successfully');

Upvotes: 0

Kiran Rai Chamling
Kiran Rai Chamling

Reputation: 498

You are using same name for password and password confirmation. Change password name to pass in your blade file and try using this validation instead

$this->validate($request, [
        'username' => 'required|min:5|max:30',
        'email' => 'required|max:255|unique:users',
        'pass' => 'required|min:6|confirmed'
    ]);

Upvotes: 0

Related Questions