symfony-forms

Symfony Form Validation Constraint Expression

孤者浪人 提交于 2019-12-21 06:07:32
问题 I have form, and need to create inline validation: $builder ->add('Count1', 'integer', [ 'data' => 1, 'constraints' => [ new NotBlank(), new NotNull(), ], ]) ->add('Count2', 'integer', [ 'constraints' => [ new NotBlank(), new NotNull(), ], ]) ->add('Count3', 'integer', [ 'data' => 0, 'constraints' => [ new NotBlank(), new NotNull(), ], ]) How white inline validation Expression for rules Count2 >=Count1 Count3 <=Count2 Count2 >= $someVariable 回答1: Other solution by using Expression Constraint

Use Sonata Field Type on normal Form Class

青春壹個敷衍的年華 提交于 2019-12-21 05:32:10
问题 I'm trying to insert custom sonata form field type on the front page, not in SonataAdmin, something like this: $form = $this->createFormBuilder($content) ->add('titleEs', 'text', array('required' => true, 'label' => 'label.title.spanish', 'attr' => array('class' => 'col-xs-12 form-control input-lg'))) ->add('contentEs', 'ckeditor', array('required' => true,'label' => 'label.content.spanish', 'attr' => array('class' => 'col-xs-12'))) ->add('titleEn', 'text', array('required' => true,'label' =>

Symfony 2.8/3.0 upgrade: how to deal with form types with variable parameters?

為{幸葍}努か 提交于 2019-12-21 05:06:53
问题 Let's say I create custom form types as services, as it is described in the Symfony documentation. But I want 2 "gender" custom types, with 2 different input parameters, which I was doing like this in Symfony 2.7: # app/config/config.yml parameters: genders1: m: Male f: Female genders2: # This makes no sense at all, but it is for the example purpose! h: Horse t: Turtle And then, I was declaring 2 services like this: <!-- src/AppBundle/Resources/config/services.xml --> <service id="app.form

Adding a 'other, please specify' option to my ChoiceType form field in Symfony

大憨熊 提交于 2019-12-21 05:02:58
问题 I'm trying to create a form field with a set of choices with an extra text input which needs to be filled out if you choose 'other': How often do you exercise? (*) I do not exercise at the moment ( ) Once a month ( ) Once a week ( ) Once a day ( ) Other, please specify: [ ] Currently, I'm using a ChoiceType where I have set my choices like this: $form->add('exercise', Type\ChoiceType::class, array( 'label' => 'How often do you exercise?', 'choices' => [ 'I do not excerise at the moment' =>

How to change form field value in symfony 2

懵懂的女人 提交于 2019-12-21 03:33:13
问题 I have a form like below: class ItemType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder // ... ->add('tags','text',array( 'required' => false, 'attr' => array('name' => 'tags'), 'mapped' => false)) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'MyBundle\ItemBundle\Entity\Item', 'cascade_validation' => true, )); } } My edit action public function editAction

How to change form field value in symfony 2

点点圈 提交于 2019-12-21 03:33:04
问题 I have a form like below: class ItemType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder // ... ->add('tags','text',array( 'required' => false, 'attr' => array('name' => 'tags'), 'mapped' => false)) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'MyBundle\ItemBundle\Entity\Item', 'cascade_validation' => true, )); } } My edit action public function editAction

How-to: Optimize Symfony's forms' performance?

核能气质少年 提交于 2019-12-21 03:16:09
问题 I have a form that is the bottleneck of my ajax-request. $order = $this->getDoctrine() ->getRepository('AcmeMyBundle:Order') ->find($id); $order = $order ? $order : new Order(); $form = $this->createForm(new OrderType(), $order); $formView = $form->createView(); return $this->render( 'AcmeMyBundle:Ajax:order_edit.html.twig', array( 'form' => $formView, ) ); For more cleaner code I deleted stopwatch statements. My OrderType has next fields: $builder ->add('status') // enum (string) ->add('paid

How to override Symfony form MoneyType input as type=“number”?

早过忘川 提交于 2019-12-19 18:22:45
问题 The Symfony MoneyType Field renders as input type="text" which allows a user to type whatever they want into the field. How can I override this to render as input type="number" so that users can only enter numeric characters? $formBuilder->add("amount", MoneyType::class, [ 'currency' => 'USD' ]); Current output: <div><label for="form_amount" class="required">Amount</label>$ <input type="text" id="form_amount" name="form[amount]" required="required" /></div> What I am trying to achieve: <div>

Symfony form: customize the setter that is called

北慕城南 提交于 2019-12-19 08:01:30
问题 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? 回答1: You can define your foo field in the form as not mapped and then add listener on the POST_SUBMIT

How to pass parameter to FormType constructor from controller

不羁岁月 提交于 2019-12-18 11:05:31
问题 In Symfony2.7 i was able to pass parameter to Form Type constructor directly from controller while creating the form, however in Symfony3 i'm not able to do it! Before in Symfony2.7 $form = $this->createForm(new NewsType("posted_by_name")); After in Symfony3 $form = $this->createForm(NewsType::class); // no idea how to pass parameter? Update: I also wanted to access it from: $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { // how to access posted_by_name here