-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easier way to add an empty option in a select element #60
Comments
Yeah, I like this idea. For now, you can do something like this: {!! Form::select('user_id', [null => 'Select a user...'] + $users->lists('name', 'id')) !!} My idea for implementing this would be to add an option to the {!! Form::select('user_id', $users->lists('name', 'id'), null, ['optional' => 'Select a user...']) !!} Thoughts? |
I second the above. This would be the easiest, simplest implementation. |
Signed-off-by: Adam Engebretson <adam@enge.me>
Ok, I pushed something to master. Do you guys wanna play with it and see what you think? |
Docs here: http://laravelcollective.com/docs/master/html#drop-down-lists Generating a Drop-Down List With an Empty Placeholderecho Form::select('size', array('L' => 'Large', 'S' => 'Small'), null, ['optional' => 'Pick a size...']); |
Works great! I do personally wonder whether "optional" is the best label for it - why not simply "placeholder"? That word is not a valid attribute for a |
You know, I thought about that... But the placeholder attribute is actually not considered valid on a select element. I could call it placeholder and remove it from the Thoughts anyone else? @tshafer? |
Well, But let's see what other people think. |
I am removing the "optional" attribute from the |
Yeah I saw - I just mean you could handle |
So what I mean is that it could make some sense being in between the other attributes in the |
I say we go with placeholder. I understand right away what this is supposed to do. |
Sounds good. I'll push the tweak now. |
Signed-off-by: Adam Engebretson <adam@enge.me>
A placeholder shouldn't submit data, fb7693c renames the placeholder paramenter, but placeholder value is being sent for select element when placeholder item is selected. |
Any update on this? I have an issue w/ moving a select element from a Laravel 5.0 project to Laravel 5.1 causing an Unsupported operand types error. Also, need to prepend multiple options such as: {!! Form::select('user_selection',['' => '- select -'] + ['other'=>'Other (unlisted)'] + $data,null,['class'=>'form-control','placeholder'=>'Choose a value...']) !!} Worked before, now it doesn't! |
Try using |
I tried your .toArray() suggestion, but I still get the same error. |
I tried on laravel 5.1
Not work |
What error did you get if any? |
@tshafer I'm having the same problem - there's no error message, but no placeholder <select name="size" placeholder="Pick a size...">
<option value="L">Large</option>
<option value="S">Small</option>
</select> Laravel 5.1 |
Could you both do me a favor, and run |
|
Looks like you have Thanks, |
Oh, just kidding. Looks like you do have |
Hi |
@adamgoose |
If |
Thanks I'll take a look |
FYI $companies is a table |
Quick question. When I use the placeholder, I end up with an empty field in my request::all() data if the user chooses that option. The problem is that the field has a foreign key, and although it can be null, it cannot be equal to an empty string. So is the way to deal with that is to add an null => 'Choose blablal' in front of my array of options, or there is something that I don't understand with the placeholder, like how to set a default value? Hope that make sense. Thanks! |
For me, doing Form::select('size', ['L' => 'Large', 'S' => 'Small'], null, ['placeholder' => 'Pick a size...']); Is there any fix for passing an empty field yet? |
https://laravelcollective.com/docs/master/html#drop-down-lists |
So there's no method of adding an extra option at the top, and not pre-selecting it? I have a bunch of data and for each row in the database there's a page with a form on it to modify it. I'd like to have the current value be the default selected option, but adding a placeholder overrides this. =/ |
@octoxan the "null" value in can be used to specify the selected option. The placeholder will just add a dummy value at the top and will be selected if the 3rd argument is null. Default selected: Specific Value Selected: *$data is the current record and size is the related field |
@robertholf Weird, I swear I tried that right before I posted and it was still selecting the placeholder. Works now though. lol |
@JorisDebonnet |
Hello, |
This is working well for me, however it is common practice to add the "disabled" attribute on the option so it is evident (dimmed) when clicking on it that the option is not acceptable. Would it be possible to add that, or would it cause problems for people who need the empty to be returned? |
Continuing from this discussion, I believe it would be worthwhile to have an easier way to include an empty top option in a select field. It can be a pain to have to prepend it to a
Collection
manually (figuring out how to preserve keys) before passing it toForm::select
, while I do believe this is a common use-case. Many dropdowns do not have anything selected by default, after all, so it would be nice if there were no hurdles to overcome to get this functionality.What could be the best way to fix this? If not through a new option on
Form::select
, perhaps with a new built-in macro?The text was updated successfully, but these errors were encountered: