简体   繁体   中英

Laravel 4 Form Text Input

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:

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

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

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.

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

I found it better to use the Input::old('first_name') for your default value instead of just "", like:

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

Edit: Yes, the third option is an array of input options, such as text field id, size, etc. or any other HTML attribute.

Keenan :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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