I have the following code, which I try to pre-select the 1st and 2nd option of the checkbox when rendered. However this doesn't work. Can anyone help me why?
$builder->add('preferredContactTypes', 'entity', array('class' => 'AppMainBundle:ContactType', 'property' => 'type', 'multiple' => true, 'expanded' => true, 'mapped' => false, 'data' => array(1,2)))
the entity data field expects objects to be passed in not integers,
see this line:
'data'=>$em->getReference("TestGeneralBundle:Sucursal",3)
from this question:
Symfony2 Setting a default choice field selection
Related
I am having troubles with the collection type. I can't make it save the first input.
here is my form:
->add('urls', CollectionType::class, [
'entry_type' => UrlType::class,
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false,
'label' => 'my label',
'label_attr' => ['class' => '...'],
'required' => false
])
I slightly adjusted my collection_widget and url_widget. My main concern is that the fist input in the collection has id="form_urls" name="form[urls]" as its attributes. And the following ones (I made them appear by clicking on the button +) has right attributes id="form_urls_1" name="form[urls][1]", id="form_urls_2" name="form[urls][2]".
All that results in inability to save the first input (it just ignores it). How can I solve the problem?
How can I get required multiple checkboxes with the EntityType Field instead of a ChoiceType Field in Symfony3? Actually, I'm using:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
->add('typesAdresses' , EntityType::class , array(
'class' => 'EKUserBundle:TypeAdresse',
'required' => true,
'expanded' => true,
'multiple' => true,
));
This will output multiple checkboxes but not as required.
In my form it must be required.
Checkboxes behaviour is different and you might get around it using the choice_attr option:
$builder
->add('typesAdresses' , EntityType::class , array(
'class' => TypeAddresse::class,
'expanded' => true,
'multiple' => true,
'choice_attr' => function($val, $key, $index) {
return array('required' => true);
},
))
;
However: I assume what you wish to achieve is “at least 1 checkbox checked in a group of checkboxes”. This is a rather different issue on its own and is more thoroughly explained in Using the HTML5 “required” attribute for a group of checkboxes?
So you'll probably have to approach this with some JavaScript and leave out the required attributes in your FormType.
how I can get value from form choice widget of submitted form?
Now it returns only integer index of selected option, but I need a value.
Below is my code:
$this->setWidget('emails',new sfWidgetFormChoice([
'label' => __('Emails'),
'expanded' => true,
'multiple' => true,
'choices' => ['email#gmail.com', 'email2#gmail.com'],
]));
$this->setValidators([
'emails' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('emails')), 'multiple' => true],
['required' => __('Required')]),
The choice widget can take a choices parameter with an associative array to define both the value and the displayed text of each option.
'choices' => array('email#gmail.com' => 'email#gmail.com', 'email2#gmail.com' => 'email2#gmail.com'),
This will submit the email as the value, however now you have potential security issues. Make sure you are enabling CSRF protection and properly validating the input at a minimum.
I'm using form to edit some data.
one of the form field:
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true
));
everthing works fine - i have some preselected cities in accordance with the database.
Now i want to limit choices to cities form specific country.
So I get collection of cities and set 'choices' option:
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true,
'choices' => $citiesCollection
));
The choice list is limited but no cities are selected.
i try to set this preselected cities using 'data' option but this also does not work
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true,
'choices' => $citiesCollection
'data' => $citiesSelected
));
trying different approaches, passing ArrayCollection, array, array of keys but nothing works...
It is even possible?
The working solution - set 'query_builder" option instead of 'choices'
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true,
'query_builder' => $citiesQueryBuilder
));
now rendered form has selected cities
I think that assigning the $citiesSelected array to the cities field of the base entity assigned to the form will make the trick.
i have form in Symfony2. One of field is entity type:
->add('kind', 'entity', array(
'class' => 'TestBundle:Kind',
'expanded' => true,
'multiple' => true,
'required' => true
)
)
I want extort thta user choose one of kind. But form allowed if user don't check anything. How can I fix this ?
Add constraints to this field in your entity. The 'required' => true is just a client side validation, if you have an old browser, your form will always submit, because HTML5 validations only works on browsers that support HTML5
The entity field works exactly in the same way than the "Select tag, Checkboxes or Radio Buttons".
This means you can use the same $options than the other type fields.
If you want to select just one kind from user with radio buttons use:
->add('kind', 'entity', array(
'class' => 'TestBundle:Kind',
'expanded' => true,
'multiple' => false
)
)
Or if you want to select just one kind from user with a select tag use:
->add('kind', 'entity', array(
'class' => 'TestBundle:Kind',
'expanded' => false,
'multiple' => false,
'required' => true
)
)