php 为 Laravel 表单字段定义默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17510355/
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
Define default values for Laravel form fields
提问by my2c
To pre-populate form field, we can add 'value' to form field in create.blade.php:
要预填充表单字段,我们可以在 create.blade.php 中的表单字段中添加“值”:
{{ Form::text('title', 'Some default title') }}
Is there a way to do that task elsewhere (maybe in model or controller?). I'd like to have code for form fields identical in create & edit view. Thanks!
有没有办法在其他地方(可能在模型或控制器中?)执行该任务。我希望在创建和编辑视图中具有相同的表单字段代码。谢谢!
回答by Andreyco
Okay, so here we are... I used Laravel's form model binding in the example. (I work with User model/db table). If this topic is not clear for you, take a look at this http://laravel.com/docs/html#form-model-binding
好的,我们到了……我在示例中使用了 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());
}
...
}
And here come the view file served by controller.
控制器提供的视图文件来了。
// 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>
回答by Victor Bjelkholm
Yes, let's consider the following example:
是的,让我们考虑以下示例:
View:
看法:
{{ Form::text('title', $title) }}
Controller:
控制器:
$title = 'Some default title';
if($article) {
$title = $article->title;
}
return View::make('user.login')->with('title', $title)
Then you will have a text-input with either Some default title
or the title from $article, if $article
isn't equal to false
然后你将有一个文本输入,或者Some default title
来自 $article 的标题,如果$article
不等于false
回答by Relaxing In Cyprus
All you need to do is include a conditional in your blade template.
您需要做的就是在您的刀片模板中包含一个条件。
Lets assume your database table has a field myfield, which you want to default to mydefault.
假设您的数据库表有一个字段 myfield,您希望将其默认为 mydefault。
Just include the following in a partial view which is called by the create and edit views:
只需在创建和编辑视图调用的局部视图中包含以下内容:
@if(isset($myfield))
{{ Form::input('text','myfield') }}
@else
{{ Form::input('text','myfield','mydefault') }}
@endif
You don't have to anything else.
你不必做任何其他事情。
回答by mahami
if you mean placeholder you can do this
如果你的意思是占位符,你可以这样做
{{ Form::password('password', array('placeholder' => 'password'))}}
回答by Jafo
Probably easier (Laravel 5):
可能更容易(Laravel 5):
{{ Form::text('title', isset($yourModel) ? null : 'Some default title') }}
That is to assume you are using the form as a partial. It should populate the value if the model for the form exists (you are editing or patching record) else it should show you the default you wish.
那是假设您将表单用作部分。如果表单的模型存在(您正在编辑或修补记录),它应该填充该值,否则它应该显示您希望的默认值。
回答by Marc TSIVANYO
If you want to do this conditionally, an alternative method of solving this could be to perform a check instead. If this check passes, set the default (as is done below with $nom
as an example). Otherwise, leave it empty by setting it to null
explicitly.
如果您想有条件地执行此操作,解决此问题的另一种方法可能是执行检查。如果此检查通过,请设置默认值(如下面$nom
的示例所示)。否则,通过将其null
显式设置为空。
{{ Form::text('title', (isset($nom)) ? $nom : null) }}
回答by Ivan Castellanos
When you are using the Schema builder (in Migrationsor somewhere else):
当您使用架构构建器时(在迁移或其他地方):
Schema::create(
'posts', function($table) {
$table->string('title', 30)->default('New post');
}
);