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

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>

Related

Livewire and CSS(After adding a new div, class select2 is deleted)

After adding a new div, select2 class is deleted.
I have a form with wire:submit.prevent="sendMessage"
<form wire:submit.prevent="sendMessage" method="POST">
The form has a select2 and it works perfectly, except one moment, when I click on a button. A page does not reload, because of wire:submit.prevent. And when message is sent, I add a new div.
#if($sent)
<div class="alert alert-success">
A message was sent successfully
</div>
<div wire:poll="reuse"></div>
#endif
I checked this code without new div and it works great, however it does not matter which element, when I just try to add something new, class select2 is deleted.
Also I have added wire:ignore, and in this case it doesn't work
<div wire:ignore>
<select wire:model="type" name="type[]" class="select2" multiple>
<option value=""></option>
#foreach($types as $type)
<option value="{{ $type->id }}">{{ $type->title }}</option>
#endforeach
</select>
</div>
I have tried a lot of different methods, and only one works perfectly. I added an ID to the where select is. Now it looks like
<div wire:ignore id="select2">
<select wire:model="type" name="type[]" class="select2" multiple>
<option value=""></option>
#foreach($types as $type)
<option value="{{ $type->id }}">{{ $type->title }}</option>
#endforeach
</select>
</div>

Using old() in Laravel

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>

Symfony2 form_widget datetime : an hour without leading zeros

formType
->add("someday", DateTimeType::class, [
"date_widget" => "single_text",
"minutes" => ["00", "20", "40"],
"required" => true,
])
template
{{ form_widget(form.someday.date) }}
{{ form_widget(form.someday.hour) }}
{{ form_widget(form.someday.minute) }}
Generated HTML (on hour part)
<select id="form_someday_time_hour" name="form[someday][time][hour]">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13" selected="selected">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
In this case, selectbox of hour with leading zeros(00, 01, 02 ... 23),
but I would like to change this selectbox without leading zeros(0, 1, 2 ... 23).
I would like to generate HTML of this format. (on hour part)
<select id="form_someday_time_hour" name="form[someday][time][hour]">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13" selected="selected">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
I tried specifying the format in the form type, but there was no change.
Will there be options for the widget on the template side?
Thank you if you know.
Try the Twig date filter and specify the hour format without leading zeros like so:
{{ form_widget(form.someday.hour|date('h')) }}
Formats are specified using the DateInterval.format. I think that should work for you. Please try it.
EDIT #2
I'm not sure why you are getting that error. Maybe it's because the date filter is in the wrong place? Can you try this:
{{ form_widget(form.someday.hour)|date('h') }}
I'm not sure if that will fix it, but please try it. If that doesn't work, maybe you can tell me how you want the "date", "hour" and "minute" to be rendered in HTML format (update your question post to show that); then maybe we can use the formatting of the DateTimeType field.
EDIT #3
Why not try this then:
$hours = new array();
for ($i = 0; $i <= 23; $i++) {
array_push($hours, strval($i));
}
...
->add("someday", DateTimeType::class, [
"date_widget" => "single_text",
"minutes" => ["00", "20", "40"],
"hours" => $hours,
"required" => true,
])
I think that will work for you.
EDIT #4
Try this as well.
->add("someday", DateTimeType::class, [
"format" => "dd H mm",
"minutes" => ["00", "20", "40"],
"required" => true,
])
Then will be shown as drop down lists. Not sure why you were using date_widget???
Unfortunately, that's impossible.
https://github.com/symfony/symfony/blob/f7d9701cdfaf9be6b40f661d0d9ec973dc4b904b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php#L74
Looking at the code, recognized added leading zeros here.
If rewrite like this, it may be the expected behavior.
- $hours[str_pad($hour, 2, '0', STR_PAD_LEFT)] = $hour;
+ $hours[$hour] = $hour;

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>
...

Using shouldBeDisabled to enable / disable controls

Is using shouldBeDisplay the way to enable / disable a control on the html or it is easier to go with jscript? I have the follow code (spacebar i believe) but it does not trigger anything.
template:
<template name="prodlist">
<select id="category-select">
<option {{ shouldBeDisabled }} selected="selected">Please Select</option>
{{#each prodlist}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
</template>
helper:
Template.registerHelper("shouldBeDisabled", function(prodlist) {
return "disabled"
});
Found the error - the disable should be placed at the select not option:
<template name="prodlist">
<select id="category-select" disabled= {{ shouldBeDisabled }}>
<option selected="selected">Please Select</option>
{{#each prodlist}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
</template>

Resources