symfony-forms

Modifying Symfony Forms html attributes / overriding a single attribute

时间秒杀一切 提交于 2019-12-07 10:56:06
问题 I am trying to figure out how to modify html attributes on the fly with Symfony2 forms. The situation is a case where a default placeholder is used most of the time, but occasionally, the developer needs to write a custom message. My Form type looks like this: <?php namespace My\AwesomeBundle\FormType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use My\AwesomeBundle\Transformer

How to add validators on the fly in Symfony2?

为君一笑 提交于 2019-12-07 03:46:25
问题 I've a password form field ( not mapped to User password) to be used in a change password form, along with two other (mapped) fields, first and last . I've to add validators on the fly: if value for password is blank then no validation should occur. Otherwise a new MinLength and MaxLength validators should be added . Here is what i've done so far: create the repeated password field, add a CallbackValidator and return if $form->getData() is null . Then, how can i add validators for minimum and

How to fill the dropdown list dynamically in symfony? (select the cities of the region)

跟風遠走 提交于 2019-12-06 16:25:53
I explain my problem: I have to create a form where the drop-down lists are filled according to our choices in the previous ones. I have two entities: A Region may have several Cities (ManyToOne) relationship. I followed the documentation from here How to Dynamically Modify Forms Using Form Events (Dynamic Generation for Submitted Forms) . Here is the code for the entities: Region entity: class Region { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="nom", type="string", length

Assign many items to one category - OneToMany Relation

纵饮孤独 提交于 2019-12-06 16:03:13
I want to assign many items to one category. I'm using Symfony 2.6.7. I'm not at the owning side here. If I open this URI: /category/2/assign [x] Item 1 [x] Item 2 (Save Button) I have the possibility to chose many items with checkboxes. This is my db table "Item", where I want to connect the both: id | category_id It is an OneToMany relation, where One = Category and Many = Items. How can I assign the both here? It is already working, when I edit one item and than select a category to this item. Now I'm on the category side and here I want select many items to this one category. Can someone

Making a A2lix translation field required in the front end in a Symfony form

梦想与她 提交于 2019-12-06 15:56:17
Is there a way to make a A2lix translation field required and validated through the front-end in a Symfony form? I have tried adding a property of presentation and translations to my validation.yml file, but to no avail. I find that when I don't enter anything in the translation field the form doesn't submit, but nothing happens. No FE or BE error. My form: /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('translations', 'a2lix_translationsForms', array( 'form_type' => new CourseGuideTranslationType($this->dataClass . 'Translation

pagination in symfony 4 using KnpPaginatorBundle

余生颓废 提交于 2019-12-06 15:19:43
Attempted to load class " KnpPaginatorBundle " from namespace " App\Knp\Bundle\PaginatorBundle ". Did you forget a " use " statement for " Knp\Bundle\PaginatorBundle\KnpPaginatorBundle "? Just add class namespace of KnpPaginatorBundle : in config/bundles.php : <?php return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], *************************************************************** *************************************************************** Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => [

Accessing a form field from a subscriber (of a form event) in Symfony2

早过忘川 提交于 2019-12-06 13:26:35
I'm following the tutorial How to Dynamically Generate Forms Using Form Events . I'm stuck on the creation of AddNameFieldSubscriber : $subscriber = new AddNameFieldSubscriber($builder->getFormFactory()); My question is simple: how FormFactory can access and modify an arbitrary form field previously created by the $builder ? And why we are passing the FormFactory instead of the $builder itself? Assuming we have just two fields ("name" and "price") in the builder : class ProductType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $subscriber = new

How to disable html5-validation for specific buttons in symfony 2.3

﹥>﹥吖頭↗ 提交于 2019-12-06 13:05:27
I have a form with three buttons. One for adding objects, one for deleting and one for editing objects. I now want to disable html5-validation in the add-button using the new form buttons introduced in Symfony 2.3. Is this possible or do I have to create a new button type? How could I do that? All you need to do is to add the html attribute novalidate or novalidate="novalidate" to the element. You can do this same way you would add any other attribute from either the form type of the twig template. For example: {{ form_widget(form.add, { 'attr': {'novalidate': 'novalidate' }}) }} Actually,

Symfony2 Cloning entities with collections and entity field type

◇◆丶佛笑我妖孽 提交于 2019-12-06 10:00:56
I have object A which contains a collection of objects B which in turn contain a collection of objects C which again contains a collection of object D. I need to retrieve object A from DB, create a form from it, change some values in fields of the form (in the future also add and remove elements from the collections) and persist on DB a new object A with all the appropriate references to the new B, new C and new D. The object CVC below is my A. If I do: $storedCVC = $this->getDoctrine() ->getRepository('AppBundle:CVC') ->find($id); $clonedCVC = clone $storedCVC; $form = $this->createForm(new

How to add an Event Listener to a dynamically added field using Symfony Forms

三世轮回 提交于 2019-12-06 04:50:41
I am using event listeners to dynamically modify a form. I want to add another event listener to a field that was added dynamically. Im not sure how to accomplish this. public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('first_field','choice',array( 'choices'=>array('1'=>'First Choice','2'=>'Second Choice') )); $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'preSetData')); $builder->get('first_field')->addEventListener(FormEvents::POST_SUBMIT, array($this, 'postSubmit')); } public function preSetData(FormEvent $event) { $form = $event-