Laravel 5验证错误消息未显示

时间:2016-08-30 14:18:35

标签: php validation laravel-5 laravel-5.2

抱歉,我是laravel 5的初学者,我尝试验证注册表单,但它没有显示错误消息。 知道错误没有显示的原因。请帮忙。

非常感谢你。

这是我的代码

查看

                        <form role="form" method="post" action="{{ url('/backoffice/register') }}">
                      <div class="form-group{{ $errors->has('fullname') ? ' has-error' : '' }}">
                        <label for="name">Full Name</label>
                        <input  type="text" name="fullname" class="form-control" id="fullname" value="{{ old('fullname') ?: '' }}" placeholder="Enter your fullname">
                        @if ($errors->has('fullname'))
                            <span class="help-block">{{ $errors->first('fullname') }}</span>
                        @endif
                      </div>
                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="control-label">Your email address</label>
                            <input type="text" name="email" class="form-control" id="email" value="{{ old('email') ?: '' }}" placeholder="Enter your email">
                            @if ($errors->has('email'))
                                <span class="help-block">{{ $errors->first('email') }}</span>
                            @endif
                        </div>
                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="control-label">Choose a password</label>
                            <input type="password" name="password" class="form-control" id="password" placeholder="Enter your password">
                            @if ($errors->has('password'))
                                <span class="help-block">{{ $errors->first('password') }}</span>
                            @endif
                        </div>
                        <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
                            <label class="control-label">Confirm Password</label>
                            <input type="password" class="form-control" name="password_confirmation" placeholder="Re-Enter your password">

                            @if ($errors->has('password_confirmation'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password_confirmation') }}</strong>
                                </span>
                            @endif
                        </div>
                        <input type="hidden" name="activated" value="0">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <button type="submit" class="btn btn-warning btn-sm">Submit</button>
                        <button type="reset" class="btn btn-default btn-sm">Reset</button>
                    </form>

控制器

public function postRegister(Request $request) {

    $this->validate($request, [
        'email' => 'required|email|max:255',
        'password'  =>'required|alphaNum|Between:4,8|Confirmed',
        'password_confirmation'=>'required|alphaNum|Between:4,8',
        'fullname' => 'required|max:255',
        'activated' => 'required',
    ]);

    Admin::create([
        'email' => $request->input('email'),
        'password' => $request->input('password'),
        'fullname' => $request->input('fullname'),
        'activated' => $request->input('activated')
    ]);

    return redirect('backoffice/register')->with('info', 'register successfully!');

}

路线

Route::get('backoffice/register', 'AdminAuthController@getRegister');
Route::post('/backoffice/register', 'AdminAuthController@postRegister');

1 个答案:

答案 0 :(得分:1)

您的刀片中是否显示错误?我是说这个

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

如果您在控制器中验证数据,您也可以这样做

    public function postRegister(Request $request)
    {
       $validator = Validator::make($request->all(), [
        'title' => 'required|',
        'body' => 'required',
        // your validation here...
    ]);

    if ($validator->fails()) {
       return redirect('youFormPage')->withErrors($validator)->withInput();
     }
   }

documentation

了解详情