【问题标题】:Laravel 5 validate error message not showingLaravel 5 验证错误消息未显示
【发布时间】:2017-01-06 21:20:07
【问题描述】:

对不起,我是 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');

【问题讨论】:

    标签: php validation laravel-5 laravel-5.2


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 2022-06-20
      • 2020-04-28
      • 2021-12-19
      • 2018-06-15
      相关资源
      最近更新 更多