Laravel 4 表单文本输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19683380/
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 4 Form Text Input
提问by jsldnppl
Hello I'm currently working with Laravel 4 forms. I'm struggling to generate a text input with a specific class without choosing a 'default value'. I want to do the following:
您好,我目前正在使用 Laravel 4 表单。我正在努力生成具有特定类的文本输入而不选择“默认值”。我想做以下事情:
{{ Form::text('first_name', array('class' => 'first_name')) }}
However I get this error (htmlentities() expects parameter 1 to be string, array given
.) unless I add a default value:
但是,htmlentities() expects parameter 1 to be string, array given
除非我添加默认值,否则我会收到此错误 ( .):
{{ Form::text('first_name', 'Some Value', array('class' => 'first_name')) }}
The default value then populates the field and needs to be deleted before entering a new value. So it can't even be used like a place holder.
然后默认值会填充该字段,需要在输入新值之前将其删除。所以它甚至不能用作占位符。
Thank you in advance,
先感谢您,
Dan
担
回答by Rob Gordijn
Instead of a value, supply null
. (do no supply empty string ""
)
代替价值,供应null
。(不提供空字符串""
)
This will come in handy in the future if you are going to work with Form Model Binding (http://laravel.com/docs/html#form-model-binding) because null
will give the value of the given model attribute.
如果您打算使用表单模型绑定 ( http://laravel.com/docs/html#form-model-binding),这将在未来派上用场,因为null
它将给出给定模型属性的值。
回答by devo
You can pass an empty value ""
like,
您可以传递一个空值,""
例如,
{{ Form::text('first_name', '', array('class' => 'first_name')) }}
Because Laravel 4's HTML Form Builder
API will accept first parameter as name
, second parameter as value
which is null
by default and the third parameter as options array
which is an empty array
by default.
因为Laravel 4的HTML Form Builder
API将接受第一个参数为name
,第二个参数value
是null
默认和第三参数作为options array
其是empty array
通过默认。
So basically you can build text input by passing only name like,
所以基本上你可以通过只传递名称来构建文本输入,
{{ Form::text('first_name') }}
And if you are planning to pass options which is the third argument, you must pass second argument also.
如果您打算传递第三个参数的选项,则还必须传递第二个参数。
See API Doc here http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#235-246
在此处查看 API 文档http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#235-246
回答by keenan
I found it better to use the Input::old('first_name')
for your default value instead of just "", like:
我发现最好使用Input::old('first_name')
默认值而不是“”,例如:
{{ Form::text('first_name', Input::old('first_name')) }}
{{ Form::text('first_name', Input::old('first_name')) }}
This way, if you go back to the form with invalid data and pass the form input, it will reload the old input that the user previously inputted. In this case, first_nameis/can be bound to the first_namefield in your database table as well.
这样,如果您返回带有无效数据的表单并传递表单输入,它将重新加载用户之前输入的旧输入。在这种情况下,first_name也可以绑定到数据库表中的first_name字段。
Edit: Yes, the third option is an array of input options, such as text field id, size, etc. or any other HTML attribute.
编辑:是的,第三个选项是一组输入选项,例如文本字段 id、大小等或任何其他 HTML 属性。
Keenan :)
基南 :)