Using old() in Laravel - laravel-blade

I have below code
<option value="{{ old('city',$user->city) }}" selected></option>
How to know old() is empty here ?

You can check the value, if value matches mark selected.
<option value="London" {{ old('city', $user->city) == 'London' ? 'selected' : '' }}></option>

Related

Is it possible to have checkbox in dropdown menu in wtform flask?

Please someone help me on having checkboxes in the dropdown list in wtforms flask and i need to select more than one check box and send data to form.Here is my code
#WTform
class ReporttestForm(FlaskForm):
pname = TextField("Platform Name",validators=[DataRequired()])
proname = TextField('Project Name', validators=[DataRequired()])
collab = SelectField ('Category', choices=[('1', '1'), ('2', '2'), (3', '3'),('4','4')])
submit = SubmitField('GENERATE')
#HTML code
<form method="POST" enctype=multipart/form-data action="">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
<div>
{{ form.pname.label }}
{{ form.pname}}
{{ form.proname.label}}
{{ form.proname }} <br><br>
<select id="collab" multiple="multiple">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
{{ form.submit}}
</div>

Handlebars: Using Parent Variable as a Parameter to a Custom Helper Wrapped in an Each Block

Check out ../billerId below. If I put {{ billerId }} before the {{# each }} block, I can see that billerId is there and defined.
I want to user billerId in my custom helper to see if the option's value should be pre-selected.
<select class="searchbar-control col-6 form-control" id="searchbar-select-biller" style="display:none">
<option value=''>Select a biller...</option>
{{# each billers }}
<option value='{{ _id }}' {{ ifEqual _id ../billerId "selected" "" }} >{{ name }}</option>
{{/ each }}
</select>
Here's the helper code:
ifEqual: function (obj, value, trueString, falseString) {
return ( (obj===value) ? trueString : falseString );
},
I have tried various syntaxes: billerId, ../billerId, ../../billerId, {{ billerId }}.
No luck.
Not exactly an answer, but on researching, I don't think Handlebars allows this type of usage.
Instead, I put the billerId in a hidden span and referenced the span by its id from within javascript. I assigned "selected" from within the script.

Symfony2 form_widget on select option

I need to give a value to my select, but i don't find how add a attr selected to my option.
i would like something like that :
{{ form_widget(form.zone,{ 'option' : {'selected' : 7 }}) }}
for selected my option with the value '7', its possible ?
Sorry for my bad english.
Thanks for your answer.
I have already try this, my select had this value, but option with this value is not selected.
<select id="zone" value="7">
<option value="0">International</option>
<option value="7">Oceanie</option>
<option value="1">Union Europeenne</option>
...
and for this work i think i need :
<option selected='selected' value="7">Oceanie</option>
Try something like this:
{{ form_widget(form.zone,{ 'attr' : {'value' : 7 }}) }}

Best way to output a list of months in a Twig template?

What is the best way to output a list of months in a Twig template? For example creating a month drop down in a date of birth set of fields. The format I am after is:
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
You can use a simple loop of 12 to create a date string for each month, then run that through Twig's date filter like below:
{% for month in 1..12 %}
{% set date = month ~ "/1/2016" %}
<option value="{{ date|date("m") }}">{{ date|date("F") }}</option>
{% endfor %}
This results in the requested format:
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
The format can easily be changed using php's date format options.
TwigFiddle of example here.
The form component actually do it, you just need to set format:
$builder->add('birth_date', DateType::class, ['format' => 'ddMMMMyyyy']);
The output is:
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
...

how to place image tags in a select imput bar

I have a select box below that works perfectly; I however want to place a little colour box in each of the option groups. I tried using a span tag within it but it does not seem to work.
I don't want to use anchor tags or images. I would prefer if possible just to have some kind of container tag.
I have enclosed my code below.
<select name="search-legend">
<option value=""></option>
<option value="1"<?= $this->ReturnSearchValue == "1" ? 'selected' : '' ?>> <span id="redbox"> </span> value1 </option>
<option value="2"<?= $this->ReturnSearchValue == "2" ? 'selected' : '' ?>> <span id="bluebox"></span> value2 </option>
<option value="3"<?= $this->ReturnSearchValue == "3" ? 'selected' : '' ?>> <span id="greenbox"></span> value3 </option>
</select>
the css
#redbox {
background: red;
}
You can't do it. The HTML spec. does not allow html markup inside an <option> element. My browser just ignored it. I think if you really want this you need to be thinking about a jQuery plugin or something. Maybe Google for 'jQuery plugin select with images'.
Inside the <option> is HTML not supported. You can style the <option>
HTML
<select class="search-legend">
<option value=""></option>
<option value="" class="redbox"></option>
<option value="" class="greenbox"></option>
<option value="" class="bluebox"></option>
</select>
CSS
.search-legend option.redbox{
background:red;
}
.search-legend option.greenbox{
background:green;
}
.search-legend option.bluebox{
background:blue;
}
Below code will work in Firefox. But for those browser you have to use some JS libraries for ex: jQuery UI.
<select name="search-legend">
<option value=""></option>
<option style="background-image:url(img1.png);" value="1"<?= $this->ReturnSearchValue == "1" ? 'selected' : '' ?>>value1 </option>
<option style="background-image:url(img2.png);" value="1"<?= $this->ReturnSearchValue == "2" ? 'selected' : '' ?>>value2 </option>
<option style="background-image:url(img2.png);" value="1"<?= $this->ReturnSearchValue == "3" ? 'selected' : '' ?>> value3 </option>
</select>

Resources