symfony-forms

Symfony 2 entity field attributes

别说谁变了你拦得住时间么 提交于 2019-12-01 07:40:48
I have in my form an entity field. This field looks like this: ->add('user', 'entity',array( 'class' => 'Elearning\SiteBundle\Entity\User', 'property' => 'name', 'multiple' => true, 'expanded' => true, 'required' => true, 'label' => 'Użytkownicy ', 'attr' => array('class' => 'userFiledCollection'), // this not working. It set class to parent div. I want to have this class in checkboxes. 'query_builder' =>function(EntityRepository $er) { return $er ->createQueryBuilder('u') ->where('u.isActive = 1'); }, ) ) All i want to do is to set an class attribute to all checkboxes rendered by this filed.

How to include in entity form type some fields from another entity?

大城市里の小女人 提交于 2019-12-01 07:35:54
I want to use fields from few entities in one form, can I do it? For example, I want to add to one form surname field from ProfileType type and name field from CountryType . This fields must be a simple string ( text ). How can I do it? Thanks! NOTE: I can't use entity type, becouse Symfony provide only checkboxes , radio buttons and selects for it, however I need to use a simple text field. To include fields from related entities in a form you embed a custom form type for each related entity. Theoretically it is possible to display and update all the data for a complex entity with many

Symfony2 form collection: How to remove entity from a collection?

半城伤御伤魂 提交于 2019-12-01 07:17:05
I try to remove entities from a collection but it doesn't work. I think I have a mistake somewhere, but I don't know where. Here the code from my updateAction : $em = $this->getDoctrine()->getEntityManager(); $entity = new Person(); if (!$entity) { throw $this->createNotFoundException('Unable to find Person entity.'); } $editForm = $this->createForm(new PersonType(), $entity); $deleteForm = $this->createDeleteForm($id); $request = $this->getRequest(); $editForm->bindRequest($request); if ($editForm->isValid()) { $entity = $editForm->getData(); $em->persist($entity); foreach($entity-

Skip validation if sibling (checkbox) field contains 'false'

此生再无相见时 提交于 2019-12-01 06:09:51
I have a form containing a checkbox and a "value field". The value field could be anything, a text box, a compound field, a collection - anything. The form could look like this, for example: field_1_label enabled [x] value [________] field_2_label enabled [x] value sub_field_1 [________] sub_field_2 [________] field_3_label enabled [x] value [________] When the "enabled" field contains true , everything works fine already. When the "enabled" field contains false , I would like to disable validation on the value field and it's child fields . So when "enabled" is un-checked, I will effectively

Define a form option allowed values depending on another option value in a FormType

十年热恋 提交于 2019-12-01 06:09:43
问题 In Symfony 2.8, in a custom form type, is it possible to make setAllowedValues return a result that depends on the value of another option? There isn't an obvious way to access an Options object in the closure as far as I can see. public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('option1'); $resolver->setRequired('option2'); $resolver->setAllowedValues('option2', function ($value) { return $based_on_set_restricted_by_option1; // <-- option2 values are

Symfony form: customize the setter that is called

倖福魔咒の 提交于 2019-12-01 05:38:21
I have a Symfony form custom type for an entity. I want to customize the code that is executed when the form is submitted, but only for a field. For example, Symfony will by default call this: $entity->setFoo($value); I want to do call instead something like: $entity->doSomething($value, true); How can I do that without affecting all other properties that are correctly mapped with the form? You can define your foo field in the form as not mapped and then add listener on the POST_SUBMIT that will call your doSomething() method: $builder->add('foo', null, array('mapped' => false)) ; $builder-

Symfony2 form collection: How to remove entity from a collection?

偶尔善良 提交于 2019-12-01 04:27:44
问题 I try to remove entities from a collection but it doesn't work. I think I have a mistake somewhere, but I don't know where. Here the code from my updateAction : $em = $this->getDoctrine()->getEntityManager(); $entity = new Person(); if (!$entity) { throw $this->createNotFoundException('Unable to find Person entity.'); } $editForm = $this->createForm(new PersonType(), $entity); $deleteForm = $this->createDeleteForm($id); $request = $this->getRequest(); $editForm->bindRequest($request); if (

Symfony 2 entity field attributes

心不动则不痛 提交于 2019-12-01 04:23:07
问题 I have in my form an entity field. This field looks like this: ->add('user', 'entity',array( 'class' => 'Elearning\SiteBundle\Entity\User', 'property' => 'name', 'multiple' => true, 'expanded' => true, 'required' => true, 'label' => 'Użytkownicy ', 'attr' => array('class' => 'userFiledCollection'), // this not working. It set class to parent div. I want to have this class in checkboxes. 'query_builder' =>function(EntityRepository $er) { return $er ->createQueryBuilder('u') ->where('u.isActive

How to include in entity form type some fields from another entity?

前提是你 提交于 2019-12-01 04:11:23
问题 I want to use fields from few entities in one form, can I do it? For example, I want to add to one form surname field from ProfileType type and name field from CountryType . This fields must be a simple string ( text ). How can I do it? Thanks! NOTE: I can't use entity type, becouse Symfony provide only checkboxes , radio buttons and selects for it, however I need to use a simple text field. 回答1: To include fields from related entities in a form you embed a custom form type for each related

Conditional validation of fields based on other field value in Symfony2

心不动则不痛 提交于 2019-12-01 00:05:25
问题 So here is the scenario: I have a radio button group. Based on their value, I should or shouldn't validate other three fields (are they blank, do they contain numbers, etc). Can I pass all these values to a constraint somehow, and compare them there? Or a callback directly in the controller is a better way to solve this? Generally, what is the best practice in this case? 回答1: I suggest you to use a callback validator. For example, in your entity class: <?php use Symfony\Component\Validator