symfony-forms

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

怎甘沉沦 提交于 2019-12-03 16:07:05
问题 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

Overriding the User Admin Form

烈酒焚心 提交于 2019-12-03 15:57:49
I'm trying to override the SonataUser/Admin/Model/UserAdmin 's configureFormFields() because I need to remove some default fields from the admin form. So I have copied the file vendor/bundles/Sonata/UserBundle/Admin/Model/UserAdmin.php in my bundle app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php and modified it. Then declared it as a service: # app/application/Sonata/UserBundle/Resources/config/services.yml services: application_user.registration.form.type: class: Application\Sonata\UserBundle\Admin\Model\UserAdmin arguments: [%sonata_user.model.user.class%] tags: - { name: form

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

回眸只為那壹抹淺笑 提交于 2019-12-03 15:56:44
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.type.gender1" class="AppBundle\Form\Type\GenderType"> <argument>%genders1%</argument> <tag name="form

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

偶尔善良 提交于 2019-12-03 14:33:23
问题 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

Grouping checkboxes in Symfony2

十年热恋 提交于 2019-12-03 13:44:43
问题 It seems that Symfony2 Form component does not handle this common case. Below is what I want in my html The code looks like : ->add('code', 'choice', array( 'choices' => array( 'Food' => array('pizza', 'burger', 'icecream'), 'Music' => array('poney', 'little', 'pocket'), ), 'multiple' => true, 'expanded' => true, 'required' => true )); Which gives in reality the wrong output : It's wierd because the case with expanded => false is correctly handled How to handle that case please ? 回答1: Ok so

Symfony2 : accessing entity fields in Twig with an entity field type

我怕爱的太早我们不能终老 提交于 2019-12-03 11:51:10
问题 Here is my FormType : public function buildForm(FormBuilder $builder, array $options) { $builder ->add('user', 'entity', array( 'class' => 'UserBundle:User', 'expanded' => true, 'property' => 'name', )); } Is there a way to access user's fields in the view (Twig) ? I'd like to do something like this : {% for u in form.user %} {{ form_widget(u) }} {{ form_label(u) }} {% if u.moneyLeft > 0 %} <span>{{ u.name }} : {{ u.moneyLeft }} €</span> {% endif %} {% endfor %} ... where moneyLeft and name

How can I check whether the supplied CSRF token is invalid in Symfony2?

此生再无相见时 提交于 2019-12-03 11:39:27
I have created a Symfony2 form and bound it to the Request. I need to explicitly ensure whether the CSRF token is valid/invalid before proceeding with the rest of the form. $form['_token']->isValid() throws OutOfBoundsException with message "Child _token does not exist." I can still verify that the rendered form contains _token field. In case that CSRF value is invalid, $form->isValid() returns false. What am I missing here? Update 1: Controller (partial): private function buildTestForm() { $form = $this->createFormBuilder() ->add('name','text') ->getForm(); return $form; } /** * @Route("/test

How to change form field value in symfony 2

馋奶兔 提交于 2019-12-03 10:49:27
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(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository(

Symfony2 - form_start function customise in twig

半腔热情 提交于 2019-12-03 10:45:58
问题 Form helpers form_start and form_end are useful in twig: {{ form_start(form) }} {{ form_end(form) }} I can customise some parameters like the method or the action . But I need to customise others parameters like the class or add the form-enctype . Can I do it? Should i set up it into the FormType.php ? Since now I simply try to add my customised value to the twig function like below: {{ form_start(form, {'class': 'myclass', 'action': 'myaction'}) }} // fields... {{ form_end(form, {'render

How to 'validate' a Symfony form in steps - instead of calling $form->isValid()

一个人想着一个人 提交于 2019-12-03 10:20:43
问题 I am using Symfony 1.3.6 on Ubuntu. I have a form with a lot of fields on it - rather than showing all the fields in one go (which may intimidate the user), I want to break up the form into stages, so that a user can fill in only the fields displayed, at each step/stage (kinda like a wizard). In order to do that, I need to write custom methods for the form, e.g.: validateStep1(); validateStep2(); ... validate StepN(); Where in each of the functions above, I only validate a subset of the