8

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.

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?

4

4 回答 4

12

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

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

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

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

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

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

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

如果您将表单与已经有数据的表一起使用,这就是您使用它的方式:

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'));
}

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

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

你甚至不必传递你的 Input 数据,因为 Laravel 会使用先出现的来填充你的输入:

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

Laravel 还将使用 Form::open() 或 Form::model() 为你处理 csrf 令牌。

于 2013-06-15T14:24:09.110 回答
3

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

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

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

但是,在执行重定向时出现错误,您必须重新绑定旧的输入数据,以便您的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());
    }
}
于 2015-04-08T09:33:37.500 回答
2

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

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

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

Form::text('entry', Input::old('entry') ? Input::old('entry') : $entry, array('class' => 'form-select');
于 2013-06-13T15:49:46.973 回答
0

我通常这样做:

// 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')); ?>

然后在你的控制器中,你可以这样做:

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();
}

请注意,这适用于 Laravel 4 .. 希望这对您有用.. 干杯...

于 2013-06-13T16:40:39.460 回答