Symfony2 entity form field required multichoice - php

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

Related

FormBuilder multiple checkboxes required using EntityType Field

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.

Get value from form choice Symfony 1.4

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.

Symfony2 - form entity field with multiple , data not preserved after choices set

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.

Symfony2 Form field Entity with 170k of results

I have a form with a field of relationship entity.
The problem is that that entity tien a lot of records, over 170,000 and render the view form the server is saturated and no load.
What solutions are there to this?
This is the form field
->add('stream', 'genemu_jqueryselect2_entity', array(
'class' => 'AcmeBundle:Stream',
'property' => 'name',
'choice_label' => 'name',
'multiple' => false,
'required' => false,
'configs' => array(
'multiple' => false,
)
)
)
IMPORTANT
I found something.
Stream entity is related to another entity under a bidirectional one-to-one.
Doctrine is running a query for each record to grab the data from that relationship.
Is there any way to tell Doctrine to not spread relationships and take "real" data Stream entity?
If I correctly understand you, you shoud look to query_builder options what allow you to fetch Streams what sutisfy some condition. E.g.:
->add('stream', 'genemu_jqueryselect2_entity', array(
'class' => 'AcmeBundle:Stream',
'property' => 'name',
'choice_label' => 'name',
'multiple' => false,
'required' => false,
'configs' => array(
'multiple' => false,
)
'query_builder' => function (StreamRepository $repository) {
return $repository->findStreamsWhatSatisfySomeCondition();
}
)
you can use external parameters like:
'query_builder' => function (StreamRepository $repository) use ($param) {
}
Detailed info you can find in doc. Hope that will help find solution :)

set checked attribute for simple checkbox field

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

Categories