Laravel 5-表单上的验证错误未重新输入用户输入 [英] Laravel 5 - Validation error on form doesn't re-enter user input

查看:73
本文介绍了Laravel 5-表单上的验证错误未重新输入用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在引发验证错误的表单上输入数据,不会重新输入用户的先前输入.

Entering data on a form that throws a validation error, doesn't re-input the user's previous input.

控制器

public function store(ClinicFormRequest $request)
    {

        $user = new \App\User;
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = Hash::make(str_random(10));
        $user->save();

        $user->user_id;

        $clinic = new \App\Clinic;
        $clinic->name = $request->clinicname;
        $clinic->telephone = $request->telephone;
        $clinic->save();

        $clinic->users()->attach(user_id);

        return Redirect::route('clinic.index')->with('message', 'Clinic created');

    }

诊所表格要求:

class ClinicFormRequest extends Request {

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
        'clinicname' => 'required',
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'telephone' => 'required',
        'address' => 'required',
        'city' => 'required',
        'postcode' => 'required'
        ];
    }

}

Create.blade.php

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

{!! Form::model(new App\Clinic, ['route' => ['clinic.store'], "class" => "form-horizontal"]) !!}

<fieldset>

    <!-- Form Name -->
    <legend>Add your clinic</legend>

    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="clinicname">Clinic Name</label>  
        <div class="col-md-4">
            <input id="clinicname" name="clinicname" type="text" placeholder="" class="form-control input-md" required="">
            <span class="help-block">Your clinic's name</span>  
        </div>
    </div>

        <!-- Button -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="register">Register</label>
            <div class="col-md-4">
                <button id="register" name="register" class="btn btn-success">Register your clinic</button>
            </div>
        </div>

    </fieldset>

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

Routes.php

Route::resource('clinic', 'ClinicController');

任何帮助将不胜感激.非常感谢.

Any help would be greatly appreciated. Many thanks.

推荐答案

您需要从旧输入中获取值,就像这样:

You need to grab the value from the old input, like so:

<!-- Text input-->
<div class="form-group">
    <label class="col-md-4 control-label" for="clinicname">Clinic Name</label>  
    <div class="col-md-4">
        <input id="clinicname" name="clinicname" type="text" placeholder="" class="form-control input-md" required="" value="{{ old('clinicname') }}">
        <span class="help-block">Your clinic's name</span>  
    </div>
</div>

您还应该考虑使用 illuminate/html 软件包,这样可以自动进行检索过程给您的旧输入.

You should also consider to use the illuminate/html package, this automates the process of retrieving old input for you.

这篇关于Laravel 5-表单上的验证错误未重新输入用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆