简体   繁体   English

Laravel 4表单文本输入

[英]Laravel 4 Form Text Input

Hello I'm currently working with Laravel 4 forms. 您好我正在使用Laravel 4表格。 I'm struggling to generate a text input with a specific class without choosing a 'default value'. 我很难在没有选择“默认值”的情况下使用特定类生成文本输入。 I want to do the following: 我想做以下事情:

{{ 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

Instead of a value, supply null . 而不是值,提供null (do no supply empty string "" ) (不要提供空字符串""

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. 如果您打算使用Form Model Binding( http://laravel.com/docs/html#form-model-binding ),这将在以后派上用场,因为null将给出给定模型属性的值。

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

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

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_name is/can be bound to the first_name field 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 :) 基南:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM