Laravel 返回带有旧输入的页面以进行验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28296562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:48:49  来源:igfitidea点击:

Laravel Back to page with old input for validation

phpvalidationlaravellaravel-4laravel-validation

提问by Biz Dev B

For the Update Profile Page

对于更新配置文件页面

I use the route as

我使用路线作为

Route::get('editdriver/{data}', 'DriverController@EditDriver');

And in the controller after validation i use,

在我使用验证后的控制器中,

return Redirect::to('editdriver/'.$data)->withInput()->withErrors($validation->messages());

So, the url will be

所以,网址将是

http://localhost/project/editdriver/1

http://localhost/project/editdriver/1

If i empty the value which is required in the ruleand press submit the form,

如果我清空所需的值rule并按提交表单,

It shows the old data with the validation message.

它显示带有验证消息的旧数据。

What i need is, It should not show the old value, which is from the db.

我需要的是,它不应该显示来自数据库的旧值。

I even tried.,

我什至尝试过,

return Redirect::back()->withErrors($validation->messages());

How can i do this ??

我怎样才能做到这一点 ??

Update :

更新 :

In the controller i have

在控制器中我有

public function EditDriver($data=NULL)
    {
        $editvehicle=$data;
        $DriverDetails = DriverModel::where('DriverId', $editvehicle)->get()->toArray();

        return View::make('home/editdriver')->with('DriverDetails', $DriverDetails);
    }

In the view i have

在我看来

$("#Firstname").val("<?php echo $DriverDetails[0]['Firstname']?>");

And displaying from jquery to html as

并从 jquery 显示为 html

{{ Form::text('Firstname',null, array('id'=> 'Firstname')) }}

回答by Laurence

I have no idea why you are using jQuery to fill in your form - that makes no sense and you should remove that code.

我不知道你为什么使用 jQuery 来填写你的表单 - 这没有意义,你应该删除该代码。

All you need is this one line of code:

您只需要这一行代码:

{{ Form::text('Firstname', Input::old('Firstname', $DriverDetails[0]['Firstname']), array('id'=> 'Firstname')) }}

Using Input::old($DriverDetails[0]['Firstname'])will prefill the inital form with your database value - but will then fill in the form with any 'inputted' values if there is a validation error

UsingInput::old($DriverDetails[0]['Firstname'])将使用您的数据库值预填充初始表单 - 但如果存在验证错误,则会使用任何“输入”值填充表单

And to confirm - your original return statement looks correct:

并确认 - 您的原始退货声明看起来正确:

return Redirect::to('editdriver/'.$data)->withInput()->withErrors($validation->messages());

回答by Jirennor

I think you wanna use the Form::model method of Laravel. This allows you to populate the form with data from your database in an easy way and give you also the possibility to input old form data.

我想你想使用 Laravel 的 Form::model 方法。这允许您以简单的方式使用数据库中的数据填充表单,并且还可以输入旧的表单数据。

{{ Form::model($User, array('url' => 'management/edit/' . $User->id, 'role' => 'form')) }}
    <div class="form-group">
        <label>Username</label>
        {{ Form::text('username', Input::old('username'), array('class' => 'form-control', 'placeholder' => 'Username', 'name' => 'username')) }}
    </div>
    <div class="form-group">
        <label>Password</label>
        {{ Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password', 'name' => 'password')) }}
    </div>
    {{ Form::submit('Edit', array('class' => 'btn btn-default')) }}
{{ Form::close() }}

In your public function you can do this:

在您的公共功能中,您可以这样做:

public function showEdit($ID)
{
    //Return View With User Information
    return View::make('management.edit')
        ->with('User', Management::find($ID));
}

This will pass back the information of the user you wanna edit. And the Form::model will match this automatically with the names of your Form::text. I think that your database fields should match the names of the Form::text.

这将传回您要编辑的用户的信息。Form::model 会自动匹配你的 Form::text 的名字。我认为您的数据库字段应该与 Form::text 的名称相匹配。

And then when you post your edit to the server you can do this:

然后当您将编辑发布到服务器时,您可以执行以下操作:

public function postEdit($ID)
{
    // Get Input
    $PostData = array(
        'username'  => Input::get('username'),
        'password'  => Input::get('password')
    );

    // Declare Validation Rules.
    $ValRules = array(
        'username'  => 'required|unique:management,username,' . $ID,
        'password'  => 'required'
    );

    // Declare Validation Messages
    $ValMessages = array(
        'username.required'     => 'The field Username is required.</br>',
        'username.unique'       => 'The field Username must be unique.</br>',
        'password.required'     => 'The field Password is required.</br>'
    );

    // Validate Input
    $ValResult = Validator::make($PostData, $ValRules, $ValMessages);

    // Check Validate
    if ($ValResult->passes())
    {
        // Update User
        $UserUpdate = Management::find($ID);
        $UserUpdate->username = Input::get('username');
        $UserUpdate->password = Input::get('password');
        $UserUpdate->save();

        // Succes, Redirect To User
        return Redirect::to('management')
            ->with('Success', 'The management users is modified.');
    }
    else
    {
        // Grab Messages From Validator
        $ValErrors = $ValResult->messages();

        // Error, Redirect To User Edit
        return Redirect::to('management/edit/' . $ID)
            ->withErrors($ValErrors)
            ->withInput(Input::except('password'));
    }
}

See the ->withInput to return the information the user enterd. The Form::model will then look in the Session Flash Data and put the old input on the right places. Notice that Session Flash Data will take precendence over the models value ($User).

请参阅 ->withInput 以返回用户输入的信息。然后 Form::model 将查看会话 Flash 数据并将旧输入放在正确的位置。请注意,会话闪存数据将优先于模型值 ($User)。

Laravel Form Model Binding: http://laravel.com/docs/4.2/html#form-model-binding

Laravel 表单模型绑定:http://laravel.com/docs/4.2/html#form-model-binding

Hope this helps!

希望这可以帮助!