【问题标题】:Laravel: Populating forms while still allowing Form::old()Laravel:在仍然允许 Form::old() 的同时填充表单
【发布时间】:2013-06-10 02:04:50
【问题描述】:

使用 Laravel 中的 Form 类制作的数据库数据填充表单的最佳方法是什么,同时如果有任何错误,仍然让位于 Input::old()?我好像没弄好。

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

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

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

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

我做错了什么?

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    您必须从控制器传递旧输入($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');
    

    【讨论】:

    • 但是如何在第一次加载页面时使用来自数据库查询的数据填充它?
    • 查看更新的答案。我的答案是针对 Laravel 3,Melvin 的另一个答案基本相同,但针对 Laravel 4。
    • 小提示;您使用的三进制可以缩短为:Form::text('entry', Input::old('entry') ?: $entry, array('class' =&gt; 'form-select');
    【解决方案2】:

    我通常这样做:

    // 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.. 希望这对您有用.. 干杯...

    【讨论】:

      【解决方案3】:

      最好的方法是使用表单模型绑定 (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() }}
      

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

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

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

      【讨论】:

      • 我从来不知道Form::model 命令。我显然遗漏了一些 L4 细节,但您从哪里获得视图中使用的 sample.form 值?
      • 我收回了。这是一条命名路线。
      • 谁能评论为什么需要一个空模型类?为什么不使用被访问数据库的模型类?
      • @EricCope,最好使用正在访问的数据库的实际模型类,问题是这样的,因为用户给我们的例子。但你是对的,我刚刚编辑了答案以提供更好的数据库示例。
      • 如果您希望出现Input::old 输入,请确保在控制器中包含-&gt;withInput(Input::all())
      【解决方案4】:

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

      【讨论】:

      • 这太棒了。我不知道Input::old() 提出了第二个论点!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2016-10-09
      相关资源
      最近更新 更多