OK after reading the documentation: http://four.laravel.com/docs/html#form-model-binding
I have a form that looks something like this:
{{ Form::model($profile, array('action' => 'ProfilesController@edit', $profile->user_id, 'files' => true)) }}
{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), array('class' => 'span12')) }}
{{ From::close() }}
My problem is: model binding does not work with Form::select, works great with text input. What am I doing wrong??
Thanks for your help.
I think your 3rd parameter in the select needs to be the selected value:
{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), $profile->gender) }}
I know it kinda defeats the purpose of model binding but it will actually work. Other issue of course is that now you've lost your class!
But if we have a quick look at the api:
select( string $name, array $list = array(), string $selected = null, array $options = array() )
We see that you can pass your options array as the 4th argument.
Therefore, the working code is:
{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), $profile->gender, array('class' => 'span12')) }}
kJamesy is right, the third parameter must be the selected value, but if you set it to null, the form model binding will set default value.
{{ Form::model($profile, array('action' => 'ProfilesController@edit', $profile->user_id, 'files' => true)) }}
{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), null, array('class' => 'span12')) }}
{{ From::close() }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With