laravel验证不起作用

时间:2018-05-08 09:04:57

标签: php laravel

我正在参加一个laravel课程,以便按照我必须解决这个问题的课程,我检查了一切,显然,一切都和那个人给我看的一样。但不同的是,他的验证工作但不是我的。当验证无效时,它必须将我重定向到register_form,但它没有。

路线

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

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

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

控制器

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

3 个答案:

答案 0 :(得分:0)

更改此行

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

并将其替换为

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

并且在您的验证中,如果您想要任何字段,则必须在验证中添加require

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

答案 1 :(得分:0)

您使用相同的名称进行密码和密码确认。更改密码名称以传递刀片文件并尝试使用此验证

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

答案 2 :(得分:0)

我看到您在验证表单值后没有返回任何路由。那么,请你试试这个代码格式。 验证方法也在上层laravel版本

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