dongqiao6445 2016-09-23 19:43
浏览 51

Laravel html表单不验证表单输入

I am trying to set up a contact form on a one page site using laravel, I can't seem to be able to get the form to validate the user input and show any errors that the form might have.

email.blade.php:

<ul>
    @foreach($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
</ul>
{!! Form::open(['route' => 'mail', 'method' => 'post', 'role' => 'form', 'id' => 'footer-form']) !!}
<div class="form-group has-feedback">
    {!! Form::label('first_name', null, ['class' => 'sr-only']) !!}
    {!! Form::text('first_name', null, ['class' => 'form-control', 'placeholder' => 'First Name']) !!}
    <i class="fa fa-user form-control-feedback"></i>
    @if($errors->has('first_name'))
        {{ $errors->first('first_name') }}
    @endif
</div>
<div class="form-group has-feedback">
    {!! Form::label('last_name', null, ['class' => 'sr-only']) !!}
    {!! Form::text('last_name', null, ['class' => 'form-control', 'placeholder' => 'Last Name']) !!}
    <i class="fa fa-user form-control-feedback"></i>
    @if($errors->has('last_name'))
        {{ $errors->first('last_name') }}
    @endif
</div>
<div class="form-group has-feedback">
    {!! Form::label('email', null, ['class' => 'sr-only']) !!}
    {!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Email address']) !!}
    <i class="fa fa-envelope form-control-feedback"></i>
    @if($errors->has('email'))
        {{ $errors->first('email') }}
    @endif
</div>
<div class="form-group has-feedback">
    {!! Form::label('textarea', null, ['class' => 'sr-only']) !!}
    {!! Form::textarea('textarea', null, ['class' => 'form-control', 'rows' => 8, 'placeholder' => 'Message']) !!}
    <i class="fa fa-pencil form-control-feedback"></i>
    @if($errors->has('textarea'))
        {{ $errors->first('textarea') }}
    @endif
</div>

{!! Form::submit('Send', ['class' => 'btn btn-default']) !!}

{!! Form::close() !!}

Route: web.php:

Route::get('/ensignhospital', [
    'as' => 'home',
    'uses' => 'HomeController@home'
]);


Route::group(['before' => 'guest'], function () {
    /*
     * CSRF Protection
     *
     * */
    Route::group(['before' => 'csrf'], function () {

        Route::post('/ensignhospital', [
            'as' => 'mail',
            'uses' => 'HomeController@postSendMail'
        ]);
    });


});

controller to handle for request:

class HomeController extends Controller {

    public function home(){
        return View('welcome');
    }



    public function postSendMail(ContactFormRequest $request){

        if($request->fails()){

            return Redirect::route('')
                ->withErrors()
                ->withInput();

        }else{

            return View('passed');
        }


    }

}

The form request validator class:

class ContactFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
            'first_name'    => 'required',
            'last_name'     => 'required',
            'email'         => 'required|email',
            'message'       => 'required'
        ];
    }
}

Problem the form does not validate when I don't enter a valid input. I need the form to validate user input and remain at the form position on the web page.

please note:

I used a .htaccess file to get the site which is on my computer to display the site root as localhost/mylaravelproject rather than the usual localhost/mylaravelprojec/public that one will normally see on a fresh install of laravel.

  • 写回答

1条回答 默认 最新

  • duanke2012 2016-09-23 19:53
    关注

    From the documentation here: https://laravel.com/docs/5.3/validation#form-request-validation

    So, how are the validation rules evaluated? All you need to do is type-hint
    the request on your controller method. The incoming form request is validated 
    before the controller method is called, meaning you do not need to clutter 
    your controller with any validation logic.
    

    Laravel will never actually remain on the form. It will instead redirect, validate and then redirect back, with the errors. You do not need the check in your code if($request->fails()).

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路