symfony-forms

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

纵饮孤独 提交于 2019-12-03 09:48:21
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_status') // enum (string) ->add('purchases_price') // int ->add('discount_price') // int ->add(

Adding a field specific error from the controller in symfony2

谁说胖子不能爱 提交于 2019-12-03 09:12:38
问题 I have some complex validation going on with my symfony form, and I need to be able to assign an error to a specific field from my controller. Right now, I have global errors working like this: $error = new formerror("There is an error with the form"); $form->addError($error); But that creates a global error, not one bound to a specific field. Is there a way to throw an error on a specific field from my controller? 回答1: Thanks to some help over IRC (thanks @fkrauthan!) I came up with an

How to setup a many to many form in Symfony2

…衆ロ難τιáo~ 提交于 2019-12-03 08:13:37
I have three entities, ChannelEntity -> MatchChannelEntity <- MatchEntity, the MatchChannelEntity saves the many to many relations between the other two tables, I want a form to list all the channels using checkboxes, and if a match has one the of channels, the checkbox of that channel is selected, how can I do this ? Here is the Form type code: class MatchhType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('channels', 'entity', array('label' => 'Channels', 'class' => 'Mikay\MikiBundle\Entity\Channel', 'multiple' => true, 'expanded' =>

How to add an autocomplete field in a Symfony2 form for a collection and using Propel?

£可爱£侵袭症+ 提交于 2019-12-03 07:36:24
问题 I'm using Symfony 2.1 forms with PropelBundle and I'm trying to refactor a form that had a drop-down list of objects (to select from) to instead use a jquery autocomplete field (working with AJAX). For the dropdown list I was using the following code (which worked perfectly for the drop-down) in my form type: $builder->add('books', 'collection', array( 'type' => 'model', 'options' => array( 'class' => 'MyVendor\MyBundle\Model\Book', 'property' => 'title', ), 'allow_add' => true, 'allow_delete

Calling $builder->getData() from within a nested form always returns NULL

邮差的信 提交于 2019-12-03 06:48:00
I'm trying to get data stored in a nested form but when calling $builder->getData() I'm always getting NULL. Does anyone knows what how one should get the data inside a nested form? Here's the ParentFormType.php: class ParentFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('files', 'collection', array( 'type' => new FileType(), 'allow_add' => true, 'allow_delete' => true, 'prototype' => true, 'by_reference' => false ); } } FileType.php class FileType extends AbstractType { public function buildForm(FormBuilderInterface

Symfony2 -> Twig -> Form -> Field -> Set rendered = true

喜夏-厌秋 提交于 2019-12-03 05:38:10
i have a simple problem. I have a form with a field for example: $builder ->add('x') ->add('y') ->add('z') ; In my twig files i used multiple blocks and i want to stop render fields... I view the b.html.twig file! a.html.twig {% block body %} {% block form %} {{ form_widget(form) }} {% endblock form %} {% endblock body %} b.html.twig {% block form %} {{ form.x.set('rendered', true) | default() }} {{ parent() }} {% endblock form %} If i remove the "default()" i get the error, that the object cant be converted to a string. And actually the form renders all fields... Inclusive the x field. But

symfony2 customize form select options

≯℡__Kan透↙ 提交于 2019-12-03 05:32:43
I'm trying to do a simple form to add an activity with a name and a color. So I want to make a list with some an array of color, for now it is working I have the name of the color. I can add any attribute to my select tag: $form = $this->createFormBuilder($myclass) ->add('Colors','choice',array('label'=>'select some colors', 'multiple'=>true, 'choices'=>array(1=>'red', 2=>'blue', 3=>'green'), 'attr'=>array('style'=>'width:300px', 'customattr'=>'customdata') )); The output will be something like this: <select name="select" style="width: 300px;" multiple="multiple" customattr="customdata">

Apply class to Symfony2 Form Label

两盒软妹~` 提交于 2019-12-03 04:22:58
I'm attempting to set the class on a class on a form label using php templates. Here's my code: <?php echo $view['form']->label($form['first_name'], 'First Name', array( 'attr' => array('class' => 'control-label') )) ?> But here's my output: <label class="required" for="form_first_name">First name</label> I can find how to do it using twig, but not an example with PHP. I've got it... <?php echo $view['form']->label($form['first_name'], 'First Name', array( 'label_attr' => array('class' => 'control-label'), )) ?> Apparently "attr" is "label_attr" for labels fields. P.S. Corresponding twig code

Symfony 2 : Add a custom form element, not in an Entity

為{幸葍}努か 提交于 2019-12-03 04:18:45
I work with Symfony2 and I would like to create a registration form. I don't want to use FOSUserBundle. So, I create an Entity Account (with fields : username, password, email...) and I create the form : $account = new Account(); $form = $this->createFormBuilder($account) ->add('username', 'text', array('label' => 'Nom de compte :')) ->add('password', 'password', array('label' => 'Mot de passe :')) ->add('email', 'email', array('label' => 'Adresse email :')) ->getForm(); Now, I want to add a confirmation field for the password. But, when I try to add a field with add() method, for example

Manually change or catch the message for invalid CSRF token in Symfony2.1

让人想犯罪 __ 提交于 2019-12-03 04:00:00
I'm using Symfony2.1 . It has a builtin CSRF protection for the forms. The error message returned when the CSRF token is invalid is: " The CSRF token is invalid. Please try to resubmit the form ". I show it on the top of the form in my Twig template by using the classic call: {{ form_errors(form) }} How can I change the returned message? In alternative, a more advanced possibility is to catch this error type in order to show a lot of options/links in my Twig template. Any idea? Did you try to set in the file validators.{locale_code}.yml to set a translation for key The CSRF token is invalid.