Alberto
Alberto

Reputation: 928

Laravel form with placeholder, class and input::old

I am trying to get used to work with Laravel's blade.

I would like to create a text input called company.

The input field needs to have an id and a class.

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!

Upvotes: 1

Views: 15394

Answers (3)

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81167

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

Upvotes: 4

Alberto
Alberto

Reputation: 928

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

Upvotes: 1

Martin Bean
Martin Bean

Reputation: 39389

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.

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('company', null, array(
    'class' => '',
    'id' => '',
    'placeholder' => '',
)) }} 

Obviously replaced the class, id, and placeholder values with your desired values.

Upvotes: 2

Related Questions