Laravel 5.2 - assign class to option in select box - forms

how one can assign class="some_class" to option in select box?
i would like to have
<select class="my_class" name="example">
<option selected="selected" value="">Choose something ...</option>
<option value="1" class="some_class">Hello</option>
<option value="2" class="some_class">World</option>
</select>
But I don't have any idea how to do it.. my form looks like:
{{ Form::select('example', array('1' => 'Hello', '2' => 'World'), null, array('class' => 'my_class', 'placeholder' => 'Choose position ...')) }}
How can I add "some_class" attribute to OPTION menu ?

The Form builder doesn't support class attributes for option tags. You will either need to build the select manually, or use JavaScript / jQuery to add the class to the option tags.

You can use my_class similar to this:
.my_class select option{
font-size: 20pt;
color:red;
}
This will affect only <option> elements of your <select> element.
Also, you can use JS to do this.

Related

Laravel dynamic select box return index instead of value from collection

I am trying to make a select box that return value from $collection, but it gives me index of the selection instead of value.
screenshot:output of my code
screenshot:browser source code view
<div class="form-group">
{!! Form::label('Supervisor') !!}<br />
{!! Form::select('supervisor_id', $lecturers, null,
array('placeholder' => '----------------','class' => 'form-control')) !!}
</div>
and when it's show in browser source code view:
<option value="">----------------</option>
<option value="0">1111111111</option>
<option value="1">1212121212</option>
Anyone who know the syntax of laravel form to make this dynamic form gives value of $lecturers please help.
Ok here we go.
First in your controller, when you are getting the lecturers list, make sure you pluck just the name and the id...in this order:
$lecturers = Lecturer::pluck('name', 'id');
Then in your view:
{{Form::select('lecturer_id', $lecturers, null, ['class' => 'form-control'])}}
Thats all.
The array of lecturers, that you plucked will be divided into "id" and "values", hence the order of plucking is important.
Now, in your form source, you will see that the option value will be lecturers ids like "1, 2, 3" etc while the "selectable" options shown to users will be the names of the lecturers.
Like this:
<select class="form-control" name="lecturers_id"><option value="1">John Doe</option><option value="2">Jane Doe</option></select>
Try these and lets know your result.

Laravel Form select disable first option

I have this code on my view
{!! Form::select('room', $rooms, Input::old('room'), array('class' => 'form-control')) !!}
and this on my controller
$rooms = array('' => 'Select Room') + $rooms;
I wanted the first option to be selected and disabled which is the "Select Room". I tried to put it also in array and it didn't work.
If you want to disable only one option from select list then you can do this in html like this:
<select>
<option value="volvo" disabled>Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi">Audi</option>
</select>
This will disable first option disabled.
For reff. http://www.w3schools.com/tags/att_option_disabled.asp

ValidForm Builder: Documentation for meta-parameter

Almost anything that can be added to the form seems to accept a meta parameter. Unfortunately I haven't found any documentation for those. Is there any list of possible options? Where they work, what they do?
[note: question is specific about ValidFormBuilder and it is encouraged by the developers to ask Questions here this way]
metaparams I so far have stumbled across (to be continued.. maybe..)
ATTRIBUTES
almost anything will turn into an attribute. With the prefix field it will become an attribute of the input, otherwise of the wrapper.
Example:
$form->addField(
'test',
'test',
ValidForm::VFORM_STRING,
array( ),
array( ),
array (
'class' => 'testClass',
'fieldClass' => 'testFieldClass',
'data-test' => 'someTestData',
'fielddata-test' => 'moreTestData',
'useless' => 'pileOfJunk',
'fielduseless' => 'jetAnotherPileOfJunk'
)
);
results in:
div class="testClass vf__optional" useless="pileOfJunk" data-test="someTestData">
<label for="test">test</label>
<input id="test" class="vf__string vf__text testFieldClass" type="text"
useless="jetAnotherPileOfJunk" data-test="moreTestData" name="test" value="">
</div>
SPECIAL
some seem to have special functionality though:
start & end
can sometimes define ranges. E.g.:
$form->addField(
'rangeex',
'Rangeex',
ValidForm::VFORM_SELECT_LIST,
array(),
array(),
array(
"start" => 1,
"end" => 3
)
);
results in:
<div class="vf__optional">
<label for="rangeex">Rangeex</label>
<select id="rangeex" class="vf__one vf__select" name="rangeex">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
tip
??prop. tooltip.. didn't test..
default / hint
set a default value.. when using hint, the hint itself doesn't pass validation for req. fields. (not sure if / where that actually works, neither default nor hint add a default value to a select for example, though hint can still be used to make the validation fail if an option with the same value is selected)

Laravel - Select defaulting to using Optgroup

I want to use a dropdown list in my view in laravel. In my controller I have the following.
$awards = array(
'gold' => 'gold',
'silver' => 'silver',
'bronze' => 'bronze',
'no-award' => 'No Award'
);
$entry->awards = $awards;
I pass it to the view as follows
$this->layout = View::make('entries/edit', array('entry' => $entry));
Finally in the view
<div class="form-group">
{{ Form::label('awards', 'Award:', array('class' => 'control-label '))}}
{{ Form::select('awards', array(entry->$awards))}}
</div>
The issue Im running into is in the source it looks like this...
<div class="form-group">
<label for="awards" class="control-label ">Award:</label>
<select id="awards" name="awards">
<optgroup label="0">
<option value="gold">gold</option>
<option value="silver">silver</option>
<option value="bronze">bronze</option>
<option value="no-award">No Award</option>
</optgroup>
</select>
</div>
Which looks good except I don't know how to avoid having it put in an optgroup, and I'd like to keep the array to my controller. Thanks.
You are wrapping your awards inside an extra unnecessary array - creating the optgroup[0]
Change
{{ Form::select('awards', array(entry->$awards)) }}
to
{{ Form::select('awards', entry->$awards) }}

Converting checkbox to select (perl)

Before:
<input type='checkbox' name='killerFeature' id='killerFeature'
<%= param('killerFeature') ? ' checked' : ''%>
/>
Now:
<select name="killerFeature" id="killerFeature" class="select">
<option value="1">Enable</option>
<option value="0">Disable</option>
</select>
How do I insert the same checked (should be 'selected' now I guess?) condition in the select now?
This is a perl app, built using Mojolicious web framework.
Many thanks for your help!
Yes, the condition should be selected (selected="selected") but i belive you already figured that out from your other post :)
<select name="killerFeature" id="killerFeature" class="select">
<option value="1" selected="selected">Enable</option>
<option value="0">Disable</option>
</select>
Also from what i saw there inst a way to create the select like you can for the input as in the below example:
<%= input 'test', type => 'text' %>
So it would be something like:
<%== param('killerFeature') eq $my_var ? ' selected="selected"' : ''; %>
Ofc you would need to replace the above to your current variables and field names.
GL:)