symfony-forms

Symfony ChoiceType $choices - labels and values swapped

◇◆丶佛笑我妖孽 提交于 2019-12-10 17:45:16
问题 Symfony 2.8.2 According to the Symfony docs "The choices option is an array, where the array key is the item's label and the array value is the item's value" http://symfony.com/doc/2.8/reference/forms/types/choice.html#choices But with the following form I'm seeing the exact opposite: $filterForm = $this->createFormBuilder() ->add('vendorName', ChoiceType::class, array( 'expanded' => true, 'multiple' => true, 'choices' => array('label' => 'value') // <-- HERE )) ->add('filter', SubmitType:

Symfony2 Forms BooleanToStringTransformer Issue

↘锁芯ラ 提交于 2019-12-10 16:43:19
问题 I have a boolean field that I've put in a form as a choice field (yes or no). I would get 0 or 1 without data transformer. I added a view BooleanToStringTransformer (which seemed reasonable) : $builder ->add( $builder->create('myBooleanField', 'choice', array( 'choices' => array(true => 'Yes', false => 'No'), )) ->addViewTransformer(new BooleanToStringTransformer('1')) ) And when I try to display the form, I get the error "Expected a Boolean.". My field is set to false before creating the

Symfony2 set Entity property after form submit/validation

南楼画角 提交于 2019-12-10 14:40:23
问题 I am submitting a symfony2 form, and I would like to set a certain Entity property to false if the email field for that entity was not filled and that property was submitted as 'true'. I do this now by: $myForm = $this->createForm(new FormType(), $myEntity); $myForm->handleRequest($request); if ($myForm->isValid()) { if (!$myEntity->getEmail()) { $myEntity->setProperty(false); } } I would now expect the checkbox corresponding to the property to be uncheck when the form is displayed after

How can I add css classes to specific symfony2 form choices?

强颜欢笑 提交于 2019-12-10 12:44:21
问题 I could do this with Javascript, but I was wondering if I could add a css class to specific symfony2 form choices (not the choice field itself, but the individual choices). For example I want to apply different css styles to individual 'option' tags inside a 'select'. I could only find a way to add a class to the tag. Thanks in advance. 回答1: You can override the layout of specific widgets in your form, which means you can override the way the select renders and put in custom code to check

Symfony2 Form Event PreSetData Subscriber

末鹿安然 提交于 2019-12-10 09:27:12
问题 In my Application the user can create Custom Fields for some entities and then set the values for this custom fields for each entity object when i display a form. The implementation is like this: 1º) I created a Interface for the forms, and the forms that i want implement this Interface. 2º) I created a form extension for all forms: app_core_form_builder.form_extension: class: App\Core\Bundle\FormBuilderBundle\Form\FormExtension arguments: ["@service_container", "@doctrine.orm.entity_manager"

symfony2 form choice and mongodb

耗尽温柔 提交于 2019-12-10 02:37:39
问题 When using Entity we can load choices from Entity via form field type entity $builder->add('group', 'entity', array( 'class' => 'Fist\CoreBundle\Document\Group', )); but when using mongodb document what is the best way to inject select options from Document? in my case load all Groups from Group document. Is there better way to do that than passing to form constructor? Thank you all 回答1: The Doctrine MongoDB ODM equivalent of EntityType is Symfony\Bundle\DoctrineMongoDBBundle\Form\Type

Symfony 2 - rearrange form fields

此生再无相见时 提交于 2019-12-09 17:14:19
问题 In our Symfony2 project we have a very complex structure for forms with embedded forms.... Now we got the requirement to bring the output of the form in an specific order. And here is the problem: We use the form_widget(form) and now we are looking for a solution in the object (e.g. via annotations) or the formbuilder to move a specific field to the end of the form. in symfony 1.4 it was the widget-movefield() function, i guess... Thx... 回答1: You can re-order the fields using this bundle:

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

三世轮回 提交于 2019-12-09 05:46:08
问题 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 ); }

Symfony 2 - separate form logic, show form errors after redirect

北战南征 提交于 2019-12-08 15:19:01
问题 I want to separate form validation logic: public function contactAction() { $form = $this->createForm(new ContactType()); $request = $this->get('request'); if ($request->isMethod('POST')) { $form->submit($request); if ($form->isValid()) { $mailer = $this->get('mailer'); // .. setup a message and send it return $this->redirect($this->generateUrl('_demo')); } } return array('form' => $form->createView()); } I want to translate into 2 separate actions: public function contactAction() { $form =

Backend sfGuardUser module

爷,独闯天下 提交于 2019-12-08 11:22:45
问题 I use symfony 1.4.11 with Doctrine I have sfGuardUser module in backend. I have sfGuardUserProfile table. sfGuardUserProfile: connection: doctrine tableName: sf_guard_user_profile columns: id: { type: integer(4), primary: true, autoincrement: true } user_id: { type: integer(4), notnull: true } salutation: { type: string(10), notnull: true } first_name: { type: string(30), notnull: true } last_name: { type: string(30), notnull: true } country: { type: string(255), notnull: true } postcode: {