symfony : textfield how to change type? - symfony

How to change the type of Textfield to force it to 'password' so that the content of the field does not appear when entering. I am implementing easyadmin, in symfony 6, as follows:
TextField::new('password')
->hideOnIndex()
->setFormTypeOptions([
'label' => 'Mot de passe',
'attr' => ['type' => 'password'],
]),
Thank you in advance for your returns and very nice day to all.

Add ->setFormType(PasswordType::class).
TextField::new('password')
->hideOnIndex()
->setFormType(PasswordType::class)
->setFormTypeOptions([
'label' => 'Mot de passe',
'attr' => ['type' => 'password'],
]),

Related

Symfony 4 - The option "invalid_message" does not exist

I use Symfony 4.1, and I try a simple usage of form builder :
$builder
->add('name', TextType::class)
->add('phone', TelType::class)
->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'Passwords don\'t match',
'options' => array('attr' => array('class' => 'password-field')),
'required' => false,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Password confirmation'),
'mapped' => false,
))
I got the error :
The option "invalid_message" does not exist. Defined options are ....
I don't find any informations on it, I just tried to copy the example from Symfony Doc : https://symfony.com/doc/current/reference/forms/types/repeated.html
Do I need to import something ?
The invalid_message option belong to FormTypeValidatorExtension which is loaded by ValidatorExtension when the Validator component is available.
You need to install this component to enable the validator options:
composer require symfony/validator

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

How to change the size of a "select box" form

A client doesn't want to scroll in a select form so I need to increase the size of my select form.
I tried this but still doesn't work, I only see 4 inputs :
{{ form_widget(filterForm.accomodationFilter1, { attr: { class: 'fancyselect filter type' }, {'size': '10'} }) }}
Any idea how I can do that and where is the documentation ?
Thanks
Try doing it directly in the formBuilder:
->add('example', 'choice', array(
'attr' => array('class' => 'fancyselect filter type',
'style' => 'max-width:100px'),
'choices' => 'choices',
)
Or just use:
->add('example', 'choice', array(
'attr' => array('class' => 'fancyselect filter type'),
'choices' => 'choices',
'max_length' => 100,
)

Disable empty_value FormType Symfony2

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

Display data attributes in collection field Symfony 2

I have 2 fields in my form. First Name and Email.
$builder->add('firstName', 'text', [
'label' => 'First Name',
'required' => true,
'attr' => [
'data-msg-required' => 'First name is required'
],
'trim' => true])
->add('emails', 'collection', [
'type' => new RegisterEmail,
'required' => true,
'by_reference' => false,
'label' => false,
'options' => [
'attr' => [
'data-msg-required' => 'Email is required'
]
],
]);
However data-msg required is only displayed for first field but not for second one. I know I can add that directly in twig template but is there anyway I can achieve that through form class only.
You should add it to the default value of the RegisterEmail type, with setDefaults()

Resources