How to specify select placeholder? - laravel-backpack

In one of my crud controllers I tried to do the following to add placeholder (disabled selected option) for select
$this->crud->addFields([
[
'name' => 'measurement_id',
'label' => 'Measurement',
'type' => 'select',
'entity' => 'measurement',
'attribute' => 'name',
'model' => Measurement::class,
'attributes' => [
'placeholder' => 'My placeholder',
],
],
]);
But it doesn't work. It generates the following html.
<select name="measurement_id" placeholder="My placeholder">
<option value="1">option1</option>
<option value="2">option2</option>
</select>
Is there a way it generates proper html?
<select name="measurement_id">
<option disabled selected>My placeholder</option>
<option value="1">option1</option>
<option value="2">option2</option>
</select>

Related

Symfony 5 form not submitted when a list contains a placeholder

I have a form with different lists like this :
$builder
->add('seatsNumber', ChoiceType::class, [
'placeholder' => 'choose.number.seats.placeholder',
'choices' => [
'1.seat.choice' => 1,
'2.seats.choice' => 2,
'3.seats.choice' => 3,
'4.seats.choice' => 4,
'5.seats.choice' => 5,
'6.seats.choice' => 6,
'7.seats.choice' => 7,
'8.seats.choice' => 8,
'9.seats.choice' => 9,
'10.seats.choice' => 10
],
'attr' => [
'class' => 'select'
],
'label' => 'seats.number.label',
'multiple' => false,
'required' => true
]
)
This is the generated HTML :
<select id="vehicle_seatsNumber" name="vehicle[seatsNumber]" required="required" class="select form-control" tabindex="-1">
<option value="" selected="selected">Choose the number of seats</option>
<option value="1">1 seat</option>
<option value="2">2 seats</option>
<option value="3">3 seats</option>
<option value="4">4 seats</option>
<option value="5">5 seats</option>
<option value="6">6 seats</option>
<option value="7">7 seats</option>
<option value="8">8 seats</option>
<option value="9">9 seats</option>
<option value="10">10 seats</option>
</select>
When I try to submit the form without having select a choice (placeholder is shown), the form isn't submitted and no one message is shown.
If you want to display validation error, you should consider using constraints
$builder->add('seatsNumber', ChoiceType::class, [ 'constraints' => [
new NotNull(),
new NotBlank()
] ])

Symfony 3+ : Styling the parent div of a field (div containing label + input)

At first, my objective here is to make two input fields inline, the picture is attached :
Symfony allow us to style form's field by using attr for form_widget of the field and label_attr for form_label but here i want to apply a style for the div containing the label + input that Symfony generate automatically in the twig :
$builder
->add('language', ChoiceType::class, array(
'choices' => array(
'FR' => 5,
'EN' => 1,
),
'label' => ' ',
'attr' => array('style' => 'float:left;width:40%'),
))
->add('level', ChoiceType::class, array(
'choices' => array(
'Good' => 5,
'Bad' => 1,
),
'label' => ' ',
'attr' => array('style' => 'float:left;width:40%'),
));
result : The style has been applyed to the input (select in our case) and the div parent wasn't be affected (stay with empty style):
<div id="homebundle_personnel_personnelLanguages_0">
<div> <!-- the div that i want to apply a style -->
<label for="homebundle_personnel_personnelLanguages_0_language" class="required"> </label>
<select id="homebundle_personnel_personnelLanguages_0_language" name="homebundle_personnel[personnelLanguages][0][language]" style="float:left;width:40%">
<option value="5">FR</option>
<option value="1">EN</option>
</select>
</div>
<div>
<label for="homebundle_personnel_personnelLanguages_0_level" class="required"> </label>
<select id="homebundle_personnel_personnelLanguages_0_level" name="homebundle_personnel[personnelLanguages][0][level]" style="float:left;width:40%">
<option value="5">Good</option>
<option value="1">Bad</option>
</select>
</div>
</div>
Resolved, I must treat this in the twig's level :
<div style="float:left;width:40%" >
{{ form_widget(form.language) }}
</div>
<div style="float:left;width:40%" >
{{ form_widget(form.level) }}
</div>
use:
'row_attr' => [
'class' => 'yourClass',
],

How can I add a custom attribute to a popup_menu using CGI.pm?

I use the following Perl code to generate an HTML popup menu with CGI.pm:
$html->popup_menu(
-name => "to",
-values => [#TO, $param_to],
-labels => {%TO, $param_to => $param_to,},
-default => $param_to,
-onchange => $onchange,
-class => "form-control"
);
The generated menu looks like this:
<select name="to" onchange="if (this.value=='support#abc.com' || document.theForm.supportform.value==1) document.theForm.submit();" class="form-control">
<option value=""> select recipient </option>
<option value="sales#abc.com">Sales Inquiry</option>
<option value="support#abc.com">Technical Support</option>
<option value="jobs#abc.com">Jobs # abc</option>
<option value="investor-relations#abc.com">Investor Relations</option>
<option value="webmaster#abc.com">abc Webmaster</option>
</select>
How can I add the attribute required to the <select> element?
According to the CGI documentation:
Many routines will do something useful with a named argument that it doesn't recognize.
So just add another named argument -required:
$html->popup_menu(
-name => "to",
-values => [#TO, $param_to],
-labels => {%TO, $param_to => $param_to,},
-default => $param_to,
-onchange => $onchange,
-class => "form-control",
-required => "required"
);
This will generate something like:
<select name="to" ... required="required">
However, generating HTML with CGI.pm is a pain and not very maintainable. It's better to use a templating library like Template Toolkit. Templates allow you to separate your Perl code and your HTML (mostly), so you can have something like this:
popup.tt
<select name="to" onchange="if (this.value=='support#abc.com' || document.theForm.supportform.value==1) document.theForm.submit();" class="form-control">
[% FOR option IN options %]
<option value="[% option.value %]">[% option.text %]</option>
[% END %]
</select>
my_script.cgi
use strict;
use warnings;
use CGI;
use Template;
my $tt = Template->new or die Template->error;
my $q = CGI->new;
print $q->header;
my $options = [
{ value => '', text => 'select recipient' },
{ value => 'sales#abc.com', text => 'Sales Inquiry' },
{ value => 'support#abc.com', text => 'Technical Support' },
{ value => 'jobs#abc.com', text => 'Jobs # abc' },
{ value => 'investor-relations#abc.com', text => 'Investor Relations' },
{ value => 'webmaster#abc.com', text => 'abc Webmaster' }
];
$tt->process('foo.tt', { options => $options }) or die $tt->error;
Output
<select name="to" onchange="if (this.value=='support#abc.com' || document.theForm.supportform.value==1) document.theForm.submit();" class="form-control">
<option value="">select recipient</option>
<option value="sales#abc.com">Sales Inquiry</option>
<option value="support#abc.com">Technical Support</option>
<option value="jobs#abc.com">Jobs # abc</option>
<option value="investor-relations#abc.com">Investor Relations</option>
<option value="webmaster#abc.com">abc Webmaster</option>
</select>

Select form Type only post the first option

Symfony version: Symfony 2.1.7-DEV.
I have a form with a select field, now if I select multiple options he only post the first option.
I have a form type like this:
$builder->add( 'weeks', 'entity', array(
'class' => 'VM\NameBundle\Entity\CaravanRow',
'property' => 'line',
'query_builder' => function(EntityRepository $er ) use ( $caravan ) {
return $er->createQueryBuilder('w')
->orderBy('w.dateFrom', 'ASC')
->where('w.caravan = :caravan' )
->andWhere('w.visible = 1')
->setParameter( 'caravan', $caravan );
},
'attr' => array(
'multiple' => true,
'size' => 5,
'style' => 'width: 415px;'
)
));
Now in the select form on the site looks like this:
<select id="reservation_weeks" style="width: 415px;" size="5" multiple="1" required="required" name="reservation[weeks]">
<option value="1">20 - 11-05 t/m 18-05 (2013)</option>
<option value="2">21 - 18-05 t/m 25-05 (2013)</option>
<option value="3">22 - 25-05 t/m 01-06 (2013)</option>
<option value="4">23 - 01-06 t/m 08-06 (2013)</option>
<option value="5">24 - 08-06 t/m 15-06 (2013)</option>
</select>
Should reservation[weeks] not be reservation[weeks][]?
Thnx.
you must add option multiple but not in attr array
$builder->add( 'weeks', 'entity', array(
'class' => 'VM\NameBundle\Entity\CaravanRow',
'property' => 'line',
....
'multiple' => true

Zend Framework 2 formInput or formElement ID tag not rendering

In Zend framework 2, when I use the view's formRow method like so
$this->formRow($form->get('product_name'));
it will generate HTML like this
<label for="product_name">
<span>Name</span>
<input type="text" id="product_name" name="product_name">
</label>
but if I use formInput
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls">
<?php echo $this->formInput($form->get('product_name')); ?>
</div>
</div>
$this->formInput($form->get('product_name'));
i don't get the id tag
<input type="" name="product_name">
I've tried with formElement with same results.
How can I get it to render just the input with all attributes and values?
This is how my Zend Framework 2 View looks like (simplified)
<?php echo $this->form()->openTag($form); ?>
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls"><?php echo $this->formInput($form->get('product_name')); ?></div>
</div>
<div class="control-group">
<div class="controls"><?php echo $this->formSubmit($form->get('submit')); ?></div>
</div>
<?php echo $this->form()->closeTag(); ?>
and the Zend Framework 2 Form
<?php
namespace Product\Form;
use Zend\Form\Form;
class ProductForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('product');
$this->setAttribute('method', 'post');
$this->setAttribute('class','form-horizontal');
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'submitbutton',
'class'=>'btn btn-success btn-large'
),
));
}
}
?>
Change:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
to:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
'id' => 'product_name',
),
'options' => array(
'label' => 'Name',
),
));
Actually this code:
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'product_name',
'attributes' => array(
'id' => 'product_name',
'class' => 'span3',
),
'options' => array(
'label' => 'Your label',
),
));
could be used correctly by a css renderer like Bootstrap due to fact that the $this->formRow(...) form helper will produce:
<label for="product_name">Your label</label><input type="text" name="product_name" class="span3" id="product_name" value="">
which is more readable than the original formRow output (without id neither class attributes)