Laravel:在仍然允许 Form::old() 的同时填充表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17091271/
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 07:57:47  来源:igfitidea点击:

Laravel: Populating forms while still allowing Form::old()

phplaravellaravel-4

提问by enchance

What's the best way to populate forms with database data made using the Form class in Laravel while still giving way to Input::old()if there are any errors? I can't seem to get it right.

使用 Laravel 中的 Form 类制作的数据库数据填充表单的最佳方法是什么,同时仍然让位于Input::old()是否有任何错误?我似乎无法正确理解。

My current setup looks something like this

我目前的设置看起来像这样

public function getSampleform() {
    // Load database data here

    return View::make('sampleform');
}

public function postSampleform() {
    // Save to database again then redirect to success page

    return Redirect::to('success');
}

I usually echo my fields in the View this way:

我通常以这种方式在视图中回显我的字段:

<?php echo Form::text('entry', Input::old('entry'), array('class' => 'form-select'); ?>

What am I doing wrong?

我究竟做错了什么?

回答by Antonio Carlos Ribeiro

The best way to do this is using Form Model Binding (http://four.laravel.com/docs/html#form-model-binding):

最好的方法是使用表单模型绑定(http://four.laravel.com/docs/html#form-model-binding):

Use an existing model or create an 'empty' model class:

使用现有模型或创建“空”模型类:

class NoTable extends Eloquent { 
    protected  $guarded = array();
}

Find your model or instantiate your empty class and fill it with data:

找到您的模型或实例化您的空类并用数据填充它:

public function getSampleform() {
    // Load database data here

    $model = new NoTable;
    $model->fill(['name' => 'antonio', 'amount' => 10]);

    return View::make('sampleform')->with(compact('model'));
}

If you'll use your form with a table that you already have data on it, this is how you uset it:

如果您将表单与已经有数据的表格一起使用,您可以这样使用它:

public function getSampleform() {

    // Locate the model and store it in a variable:
    $model = User::find(1);

    // Then you just pass it to your view:   
    return View::make('sampleform')->with(compact('model'));
}

To have your form populated, use Form Model Binding, this is an example in Blade:

要填充表单,请使用表单模型绑定,这是 Blade 中的示例:

{{ Form::model($model, array('route' => array('sample.form')) ) }}
    {{ Form::text('name') }}
    {{ Form::text('amount') }}
{{ Form::close() }}

You don't even have to pass your Input data, because Laravel will populate your inputs using what comes first:

你甚至不必传递你的输入数据,因为 Laravel 将使用最先出现的内容填充你的输入:

1 - Session Flash Data (Old Input)
2 - Explicitly Passed Value (wich may be null or not)
3 - Model Attribute Data

And Laravel will also take care of csrf token for you, using Form::open() or Form::model().

Laravel 也会为你处理 csrf 令牌,使用 Form::open() 或 Form::model()。

回答by Shawn Erquhart

You have to pass old input from the controller ($entryshould contain your database entry):

您必须从控制器传递旧输入($entry应包含您的数据库条目):

return View::make('sampleform')->with('entry', $entry)->with_input();

And then in the view, use an inline if statement to load the input if present, or else load from database:

然后在视图中,使用内联 if 语句加载输入(如果存在),否则从数据库加载:

Form::text('entry', Input::old('entry') ? Input::old('entry') : $entry, array('class' => 'form-select');

回答by jeteon

The old()helper in Laravel (at least in 5.0) allows for a default value so that if some default value $entryis defined, then if you do this:

old()Laravel 中的助手(至少在 5.0 中)允许使用默认值,以便如果$entry定义了某些默认值,那么如果您这样做:

<?php 
    echo Form::text('entry', Input::old('entry', $entry), array('class' => 'form-select'); 
?>

The helper will first try to look up the old form value and failing that will use the value $entry. This also avoids having to use a ternary operator in your code.

助手将首先尝试查找旧表单值,如果失败,将使用该值$entry。这也避免了必须在代码中使用三元运算符。

However, when doing the redirect and there is an error you have to re-bind the old input data so that your postSampleform()method would look like:

但是,在进行重定向并出现错误时,您必须重新绑定旧的输入数据,以便您的postSampleform()方法如下所示:

public function postSampleform() {

    // Save to database again then redirect to success page
    if ($success)
    {
        return Redirect::to('success');
    }
    else
    {
        return Redirect::to('sampleform')->withInput(Request::all());
    }
}

回答by Melvin

I usually do it this way:

我通常这样做:

// Check first if there is data from database else blank
$entry = (isset($data->entry)) ? $data->entry : '';
<?php echo Form::text('entry', isset(Input::old('entry')) ? Input::old('entry') : $entry, array('class'=>'form-select')); ?>

Then in your controller, you can do this:

然后在您的控制器中,您可以执行以下操作:

public function getSampleform() {
    // Load database data
    $data = "Database data here";
    return View::make('sampleform', compact('data'));
}

public function postSampleform() {
    // validate

    // if validation fails
    // redirect back and pass old inputs
    return Redirect::to('getSampleform')->withInput();
}

Take note that this is for Laravel 4.. Hope this works for you.. Cheers...

请注意,这是针对 Laravel 4.. 希望这对你有用.. 干杯......