symfony-forms

Symfony form query_buider and entity repository

半腔热情 提交于 2019-11-29 21:54:41
I'm trying to create a form with data in collection type depending on the user being logged. I'm following this chapter of the Symfony cookbook . Everything works fine when the query_builder option is a closure where I get my data from DQL. As the data need to be fetched from different location in code, I would prefer to define the query in the Repository class. Here is the function in my repository : public function findOwnedBy($user) { $query = $this->getEntityManager()->createQuery("SELECT l FROM MyBundle:Article a JOIN a.owndBy u WHERE u.id = :userId"); $query->setParameters(array("userId"

Pass a variable to a Symfony Form

自闭症网瘾萝莉.ら 提交于 2019-11-29 21:26:18
问题 I am building a web application using Symfony 1.4 and Doctrine for a school and I want to make a very simple form to add a course to a student. The main problem I have is that in the drop down list I only want to show the courses in which the student is currently not enrolled. I already have a function in the model (in Student.class.php) which returns all the courses in which the student is not enrolled but the problem is I don't know how to pass the student to the configure() of the form. I

Difference between ObjectManager and EntityManager in Symfony2?

拟墨画扇 提交于 2019-11-29 21:12:23
What's the difference between Doctrine\Common\Persistence\ObjectManager and Doctrine\ORM\EntityManager when using it in a custom form type? I can get the respository using both $this->em->getRepository() and $this->om->getRepository() . class MyFormType extends \Symfony\Component\Form\AbstractType { /** * @var Doctrine\ORM\EntityManager */ protected $em; public function __construct(Doctrine\ORM\EntityManager $em) { $this->em = $em; } } Instead of: class MyFormType extends \Symfony\Component\Form\AbstractType { /** * @var Doctrine\Common\Persistence\ObjectManager */ protected $om; public

Creating a 'Ajaxified' Form Field Type

て烟熏妆下的殇ゞ 提交于 2019-11-29 11:03:43
In my application I've got a couple of form fields with many options. The problem I experienced is similar to this question : getting and parsing all options at every page load is expensive (Twig renders all options over and over again while no client side caching possible). That problem made me create a way to send the options via AJAX to the browser. Fairly simple approach: Get all options (key-value) via AJAX (for example by getting /countries.json) and cache if possible. (in this case it's not very likely country names change very often) Use selectize, select2 or a similar plugin to insert

How do I add an unbound field to a form in Symfony which is otherwise bound to an entity?

ε祈祈猫儿з 提交于 2019-11-29 06:38:20
问题 Maybe I'm missing the obvious but how do I (or can I) add an extra "unbound" field to a Symfony form that is otherwise bound to an entity? Let's say I have an entity with fields first_name and last_name . I do the typical thing in my form class buildForm method. $builder ->add('first_name') ->add('last_name') ; and this in my controller: $editForm = $this->createForm(new MyType(), $entity); That works nicely but I'd like to add another text box, let's call it "extra", and receive the value in

Inherit form or add type to each form

笑着哭i 提交于 2019-11-29 06:38:04
I am searching for an easy way to add a bundle of fields to each form. I have found a way to extend the AbstractType and use the buildForm method to add more fields. When creating the form I give the name of my new type ( How to Create a Custom Form Field Type ). In my opinion it is an easy way, but it is restricted to one type per form. Is there a better way to achieve anything like that? I have read the cookbook of symfony, but I have only found stuff how to extend an existing form not how to create an own form "template" with my fields. Have you tried using inheritance? This is really

Symfony 2 Create a entity form field with 2 properties

喜你入骨 提交于 2019-11-29 05:38:10
I am using symfony2 and have a form to save the relation of one user to some rules. These rules are setted by the admin user of the company. In this form, after I selected a user to update, I have to select wich rule this user have permission. The problem is that I may have more then one rule with the same name (it's another entity) but the values are different. So, when I build the selectbox I must show the name and the value like: Quantity of items - 10 Quantity of items - 20 Value of the item - 200 Value of the item - 500 But now I just can show without the "- $value" using the code bellow:

Symfony3: is it possible to change the name of a form?

雨燕双飞 提交于 2019-11-29 03:30:40
With Symfony 2.7 , you could customize a form's name in your EntityType class with the method getName() This is now deprecated. Is there another way to do that with Symfony 3.0 ? I have custom prototype entry_rows for collections that I would need to use in different forms. Since the name of the rows is based on the form's name, I would need to change the later in order to use them with a different form. Matteo You should implements the getBlockPrefix method instead of getName as described in the migration guide here . As example: /** * Returns the prefix of the template block name for this

Specify different validation groups for each item of a collection in Symfony 2?

妖精的绣舞 提交于 2019-11-29 01:06:53
问题 [Documentation about collection] When embedding forms (collection type) is possible to specify validation groups for each item, based on the current item? It seems not working ATM. The TaskType form adding a collection of tags: // src/Acme/TaskBundle/Form/Type/TaskType.php // ... public function buildForm(FormBuilderInterface $builder, array $options) { // ... $builder->add('tags', 'collection', array( // ... 'by_reference' => false, )); } For example we have two tags (tag 1 and tag 2) and a

Use a conditional statement when creating a form

拟墨画扇 提交于 2019-11-28 18:03:28
I would like to use a conditional statement when creating a form in Symfony. I am using a choice widget in general case. If the user selects the option " Other ", I would like to display an additional text box widget . I suppose this can be done in javascript, but how can I still persist the data from 2 widgets into the same property in my entity? I have this so far: $builder->add('menu', 'choice', array( 'choices' => array('Option 1' => 'Option 1', 'Other' => 'Other'), 'required' => false, )); //How to add text box if choice == Other ???? I was planing to use a DataTransfomer, but on 2