【问题标题】:Define default values for Laravel form fields为 Laravel 表单字段定义默认值
【发布时间】:2013-07-04 19:46:58
【问题描述】:

要预填充表单字段,我们可以在 create.blade.php 中为表单字段添加“值”:

{{ Form::text('title', 'Some default title') }}

有没有办法在其他地方完成这项任务(可能在模型或控制器中?)。我想在创建和编辑视图中为表单字段提供相同的代码。谢谢!

【问题讨论】:

  • 为什么不将表单代码放入单独的局部视图中,然后将其包含在两个视图中,创建和编辑视图?
  • 嗨安德烈科!我打算尝试使用 if 语句(如果模式是创建,则使用默认值,或者如果模式是编辑,则使用 null)的表单字段的部分或变量。我还想在其他地方设置它,例如:为新项目设置类别(与“当前”类别相同)。
  • 您可能想研究使用 ViewModel 的“模式”。一个解释here 注意:为了这个概念,不要尝试在你的Laravel项目中使用FuelPHP。 Here's Zend 中的一个例子。
  • fideloper,谢谢你的链接。我承认事情经常奏效,但我不明白为什么。我是新手,但我也可以责怪“魔术”,它隐藏了复杂性,同时对理解很重要。 :)

标签: php laravel default-value


【解决方案1】:

好的,我们到了……我在示例中使用了 Laravel 的表单模型绑定。 (我使用用户模型/数据库表)。 如果你对这个话题不清楚,看看这个http://laravel.com/docs/html#form-model-binding

// Controller

class UsersController extends BaseController
{

    ...

    // Method to show 'create' form & initialize 'blank' user's object
    public function create()
    {
        $user = new User;
        return View::make('users.form', compact('user'));
    }

    // This method should store data sent form form (for new user)
    public function store()
    {
        print_r(Input::all());
    }

    // Retrieve user's data from DB by given ID & show 'edit' form   
    public function edit($id)
    {
        $user = User::find($id);
        return View::make('users.form', compact('user'));
    }

    // Here you should process form data for user that exists already.
    // Modify/convert some input data maybe, save it then...
    public function update($id)
    {
        $user = User::find($id);
        print_r($user->toArray());
    }

    ...

}

控制器提供的视图文件来了。

// The view file - self describing I think
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    @if(!$user->id)
    {{ Form::model($user, ['route' => 'admin.users.store']) }}
    @else
    {{ Form::model($user, ['route' => ['admin.users.update', $user->id], 'method' => 'put']) }}
    @endif
        {{ Form::text('firstName') }}

        {{ Form::text('lastName') }}

        {{ Form::submit() }}
    {{ Form::close() }}
</body>
</html>

【讨论】:

  • 单个视图文件比我想尝试的要好得多,更易于维护(两个视图文件和一个通用表单字段文件:));伟大的!!!谢谢!我需要的唯一更改是 if 语句 ---> @if(!isset($user->id))
【解决方案2】:

是的,让我们考虑以下示例:

查看:

{{ Form::text('title', $title) }}

控制器:

$title = 'Some default title';
if($article) {
    $title = $article->title;
}
return View::make('user.login')->with('title', $title)

如果$article 不等于false,那么您将有一个带有Some default title 或$article 标题的文本输入

【讨论】:

  • 谢谢,维克多!将 $title 变量添加到视图,然后附加 'with' 部分: ->with('title', 'Some default ttle') 有效。如果我有相同的视图代码进行编辑,它会抱怨缺少标题变量,所以我添加:->with('title', null)
  • 当我选择 Andreyco 的答案时,不知何故这被取消选择为接受的答案。两者都为我的“有没有办法”的问题提供了有用的答案:) 谢谢大家!
【解决方案3】:

您需要做的就是在刀片模板中包含一个条件。

假设您的数据库表有一个字段 myfield,您希望将其默认为 mydefault。

只需在创建和编辑视图调用的局部视图中包含以下内容:

@if(isset($myfield))
{{ Form::input('text','myfield') }}
@else
{{ Form::input('text','myfield','mydefault') }}
@endif

你不需要其他任何东西。

【讨论】:

    【解决方案4】:

    如果你的意思是占位符,你可以这样做

    {{ Form::password('password', array('placeholder' => 'password'))}}
    

    【讨论】:

    • 谢谢 mahami,这与占位符无关。
    【解决方案5】:

    可能更容易(Laravel 5):

    {{ Form::text('title', isset($yourModel) ? null : 'Some default title') }}
    

    这是假设您将表单用作部分。如果表单的模型存在(您正在编辑或修补记录),它应该填充该值,否则它应该向您显示您希望的默认值。

    【讨论】:

      【解决方案6】:

      当您使用架构构建器时(在 Migrations 或其他地方):

      Schema::create( 
            'posts', function($table) {
                $table->string('title', 30)->default('New post'); 
            }
      );
      

      【讨论】:

      • 嗨 Ivan,它适用于数据库默认设置,它不会更改模型或其他模块中的任何内容。
      【解决方案7】:

      如果您想有条件地执行此操作,解决此问题的另一种方法可能是执行检查。如果此检查通过,请设置默认值(如下以$nom 为例)。否则,通过将其显式设置为 null 将其留空。

      {{ Form::text('title', (isset($nom)) ? $nom : null) }}
      

      【讨论】:

      • 虽然只允许使用代码回答,但请考虑编辑您的回答以包含说明。这有助于答案适用于更多情况并帮助其他人学习。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多