带有占位符、类和输入的 Laravel 表单::旧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26058353/
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
Laravel form with placeholder, class and input::old
提问by Alberto
I am trying to get used to work with Laravel's blade.
我正在尝试习惯使用 Laravel 的刀片。
I would like to create a text input called company.
我想创建一个名为 company 的文本输入。
The input field needs to have an id and a class.
输入字段需要有一个 id 和一个类。
I also want to show a placeholder if there is no data in the database, or the data stored if already exists.
如果数据库中没有数据,或者存储的数据已经存在,我还想显示一个占位符。
Finally, I would like to keep the introduced input in case of errors.
最后,我想保留引入的输入以防出错。
I would like to use something similar at this:
我想在这里使用类似的东西:
{{ Form::text(
'company',
isset($user->company)?$user->company:array('placeholder'=>'Your company'),
array('class' => 'field required', 'id' => 'company'),
Input::old('company')
) }}
Any help would be appreciated. Thanks!
任何帮助,将不胜感激。谢谢!
回答by Jarek Tkaczyk
The easy way, using form model binding:
简单的方法,使用表单模型绑定:
{{ Form::model($user, [ ...options...]) }}
{{ Form::text(
'company', // refers to $user->company
null, // auto populated with old input in case of error OR $user->company
array('class' => 'field required', 'id' => 'company',
'placeholder' => 'Your company') // placeholder is html attribute, don't use model data here
) }}
And if you don't want form model binding, this is all you need:
如果你不想要表单模型绑定,这就是你所需要的:
{{ Form::text(
'company',
$user->company, // auto populated with old input in case of error
array('class' => 'field required', 'id' => 'company',
'placeholder' => 'Your company')
) }}
回答by Martin Bean
Laravel will handle re-populating inputs for you, so long as the key in the POST data is the same as your input's name
attribute.
Laravel 将为您处理重新填充输入,只要 POST 数据中的键与您输入的name
属性相同。
With Form::text()
, the first parameter is the field name, the second parameter is the default value you want, and the third parameter is an array of HTML attributes you want set. So, you would have:
用Form::text()
,第一个参数是字段名,第二个参数是你想要的默认值,第三个参数是你想要设置的 HTML 属性数组。所以,你会有:
{{ Form::text('company', null, array(
'class' => '',
'id' => '',
'placeholder' => '',
)) }}
Obviously replaced the class
, id
, and placeholder
values with your desired values.
显然class
,用您想要的值替换了、id
和placeholder
值。
回答by Alberto
Found it!
找到了!
It works fine for me if I do this:
如果我这样做,它对我来说很好用:
{{ Form::text(
'company',
Input::old( 'company', $user -> company ) ,
array( 'class' => 'field required', 'id' => 'company', 'placeholder' => 'Your company' )
) }}