Disable empty_value FormType Symfony2 - symfony

I want to disable the empty_value by default in my form type.
how to do this ?
->add('subject', 'choice', array(
'label' => 'Subject',
'choices' => array(
'1' => 'contact.Appreciation',
'2' => 'contact.Feedback',
'3' => 'contact.Dissatisfactions',
'4' => 'contact.My account',
'5' => 'contact.Recruitment',
'6' => 'contact.Other',
),
'attr' => array(
'class' => 'form-control'
),
'required' => false,
'empty_value' => 'contact.Subject',
))

Change required to true.
'required' => true
See documentation

There is no easy solution for this, but you can use an event listener on the PRE_BIND event.
See this for example: https://stackoverflow.com/a/14346520/606104

Related

entitytype multiple default value

i have a entitytype and i want give default value (i have the id of entity type)
->add('esame_' . $i, EntityType::class, array(
'label' => false,
'mapped' => false,
'class' => 'AppBundle:Nome_esame',
'required' => true,
'multiple' => true,
'data'=>array($id_Nome_esame1,$id_Nome_esame2) ,
'choice_label' => 'nome',
// 'disabled' => 'disabled',
'attr' => array(
'placeholder' => 'Esami',
'class' => 'max_width esame_row select_esame',
// 'class'=>'col-md-12 col-md-offset-0 col-xs-9 col-xs-offset-3 ',
)
))
My goal is give 2 or more default value to that entity
i try
'data'=>array($id_Nome_esame1,$id_Nome_esame2)
where $id_Nome_esame1, and $id_Nome_esame2 are the id value of entity type by it don't work
I think your data has to be the same type as the class (AppBundle:Nome_esame)
Try something like this (you will need to bring doctrine or the entity manager into your FormType if you didn't do that yet)
->add('esame_' . $i, EntityType::class, array(
'label' => false,
'mapped' => false,
'class' => 'AppBundle:Nome_esame',
'required' => true,
'multiple' => true,
'data' => array($this->em->getReference("AppBundle:Nome_esame", $id_Nome_esame1), $this->em->getReference("AppBundle:Nome_esame", $id_Nome_esame2)),
'choice_label' => 'nome',
// 'disabled' => 'disabled',
'attr' => array(
'placeholder' => 'Esami',
'class' => 'max_width esame_row select_esame',
// 'class'=>'col-md-12 col-md-offset-0 col-xs-9 col-xs-offset-3 ',
)
))
To get the entity manager, if you use the formType as a service, inject doctrine into it, if not, just past it via __construct.
Hope this helps,
Alexandru Cosoi

Access translated property of collection in form

I have a form with a collection. My form "PageType" with a collection look like this.
$builder->add('sections', 'collection', array(
'type' => new SectionType($this->securityContext, $this->entityManager) ,
'allow_add' => true,
'allow_delete' => true,
'label' => false,
'prototype' => true,
'prototype_name' => '__sectionPrototype__',
'options' => array(
'label' => false,
)
And my second form which represents the collection "SectionType" look like this.
->add('translations', 'a2lix_translations', array(
'fields' => array(
'title' => array(
'field_type' => 'text',
'label' => 'title',
'attr' => array('class' => 'rte sectionTitle')
),
'text' => array(
'field_type' => 'textarea',
'label' => 'hintText',
'attr' => array('class' => 'rte sectionDescription')
)
)
))
How can i access from twig the translations fields?
I think that is not possible. Maybe somebody could correct me if I'm wrong.
fields is an option item for a2lix_translations. If you would have Form object you might be able to do that, but in fact, you have FormView which represents built form.
Alternatively, you could pass that array via standard return from controller and into your twig. Does that sound good?

woocommerce Customising checkout fields

I am trying to customise the woocommerce checkout page. The below works fine:
$fields['billing']['billing_city'] = array(
'label' => __('Town / City:', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'label_class' => array('infotown'),
'clear' => true,
'placeholder' => _x('', 'placeholder')
);
Where as the following does not:
$fields['billing']['billing_state'] = array(
'label' => __('State:', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'label_class' => array('infocounty'),
'clear' => true,
'placeholder' => _x('', 'placeholder')
);
There's no difference between the 2, apart from the label, and the label class.
Any ideas?
try adding
- woocommerce language support, which is for translators
- check the add_action function in which you are adding it too
- spacing can be a problem in the wrong places also
to add language support, try so
$fields['billing']['billing_state'] = array(
'label' => __('State:', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'label_class' => array('infocounty'),
'clear' => true,
'placeholder' => _x('', 'placeholder', 'woocommerce')
);
where i added "woocommerce" next to placeholder. :)

Adding a value to a textbox generated on Symfony 2

Having this:
echo $view['form'] -> row($form["codelist"], array(
//widget
"widgetArgs" => array(
"attr" => array(
'class' => 'input-xlarge tooltipRight',
'id' => "gift_codelist"
),
"tooltip"=>"gift.tooltip.codelist",
"translation_domain" =>"brand"
),
"labelArgs" => array(
"label_attr" => array(
'class' => 'control-label',
)) ,"rowType"=>2
)
);
How do you add an initial value to that textbox?
If you want to add an actual value to the field, you can use the value attribute:
"attr" => array(
'class' => 'input-xlarge tooltipRight',
'id' => "gift_codelist",
'value' => "Your initial value"
)
But if you are using HTML5 and just want to give a hint to your users, you'd better use a placeholder:
"attr" => array(
'class' => 'input-xlarge tooltipRight',
'id' => "gift_codelist",
'placeholder' => "Your initial value"
)
http://www.w3.org/html/wg/drafts/html/master/forms.html#the-placeholder-attribute

How to make checkbox checked?

I have this field in form builder
->add('pay_method', 'choice', array(
'choices' => array(
'telnet' => 'Image',
),
'expanded' => true,
'label' => 'Способ оплаты',
))
with ony one option yet, but how can i make this checked?
Add attr index with 'checked' => 'checked':
->add('pay_method', 'choice', array(
'choices' => array(
'telnet' => 'Image',
),
'expanded' => true,
'label' => 'Способ оплаты',
'attr' => array('checked' => 'checked')
))
Set it on the object or array the form is used for.
You can set the default value by either setting it in your domain model:
private $pay_method = 'telnet';
or
$object->pay_method = 'telnet'
or by specifying the "data" option of the field:
$builder->add('pay_method', 'choice', array(
'choices' => array(
'telnet' => 'Image',
...
),
'data' => 'telnet',
'expanded' => true,
'label' => ...,
));
Ok, i did it through JavaScript

Resources