Laravel 4 Form Text Input

20,621

Solution 1

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.

Solution 2

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

Share:
20,621
jsldnppl
Author by

jsldnppl

Updated on July 25, 2022

Comments

  • jsldnppl
    jsldnppl almost 2 years

    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

  • jsldnppl
    jsldnppl over 6 years
    Thank you Rob - never got around to saying that this fixed the issue :)