symfony-forms

Symfony form query_buider and entity repository

笑着哭i 提交于 2019-11-28 17:47:48
问题 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

Symfony2 entity field type alternatives to “property” or “__toString()”?

这一生的挚爱 提交于 2019-11-28 17:36:11
Using Symfony2 entity field type one should specify property option: $builder->add('customers', 'entity', array( 'multiple' => true, 'class' => 'AcmeHelloBundle:Customer', 'property' => 'first', )); But sometimes this is not sufficient: think about two customers with the same name, so display the email (unique) would be mandatory. Another possibility is to implement __toString() into the model: class Customer { public $first, $last, $email; public function __toString() { return sprintf('%s %s (%s)', $this->first, $this->last, $this->email); } } The disadvances of the latter is that you are

Difference between ObjectManager and EntityManager in Symfony2?

断了今生、忘了曾经 提交于 2019-11-28 16:56:32
问题 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

Add error to Symfony 2 form element

筅森魡賤 提交于 2019-11-28 16:27:50
I check some validation in my controller. And I want to add error to specific element of my form on failure. My form: use Symfony\Component\Form\FormError; // ... $config = new Config(); $form = $this->createFormBuilder($config) ->add('googleMapKey', 'text', array('label' => 'Google Map key')) ->add('locationRadius', 'text', array('label' => 'Location radius (km)')) ->getForm(); // ... $form->addError(new FormError('error message')); addError() method adds error to form, not to element. How can I add an error to locationRadius element? You can do $form->get('locationRadius')->addError(new

Could not load type “file” in Symfony3

早过忘川 提交于 2019-11-28 11:34:33
问题 I am building form with multiple image upload and I can't find solution for problem that is mentioned in title. To help me with that I included VichUploaderBundle Right now I am stuck with this error and I can't find why and where is mistake in code: Could not load type "file" 500 Internal Server Error - InvalidArgumentException Does someone knows where is the problem? Here is code that I am using: ProductType class: <?php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType

Symfony2.1 using form with method GET

痴心易碎 提交于 2019-11-28 09:24:38
I need help on using Symfony2.1 forms with method=GET and a clean URL space. I am creating a "filter" which I'd like to set in the URL so that people can bookmark their links. So, very simply the code: $form = $this->createFormBuilder($defaultData) ->add('from', 'date', array('required' => false, 'widget' => 'single_text', 'format' => 'dd.MM.yyyy')) I render the form widget and all is fine. However when I submit the form, it produces very ugly GET parameters: /app_dev.php/de/event?form%5Bfrom%5D=17.11.2012 This is because the input name is of course form[from] So to clean the URL space, I made

How can I add a violation to a collection?

笑着哭i 提交于 2019-11-28 09:17:23
My form looks like this: public function buildForm(FormBuilderInterface $builder, array $options) { $factory = $builder->getFormFactory(); $builder->add('name'); $builder->add('description'); $builder->add('manufacturers', null, array( 'required' => false )); $builder->add('departments', 'collection', array( 'type' => new Department )); } I have a class validator on the entity the form represents which calls: if (!$valid) { $this->context->addViolationAtSubPath('departments', $constraint->message); } Which will only add a 'global' error to the form, not an error at the sub path. I assume this

Problems With Multiple File Upload In Symfony2

久未见 提交于 2019-11-28 06:58:07
I am making a Symfony2 application which needs to have a multiple image upload option. I have made the single file upload using the cookbook entry: How to handle File Uploads with Doctrine which works fine. I have implemented the lifecyclecallbacks for uploading and removing. Now I need to turn this into a multiple upload system. I have read a few answers from Stack Overflow as well, but nothing seems to work. Stack Overflow Question: Multiple file upload with Symfony2 multiple file upload symfony 2 I have the following code at the moment: File Entity: <?php namespace Webmuch\ProductBundle

Symfony form - Access Entity inside child entry Type in a CollectionType

五迷三道 提交于 2019-11-28 05:24:02
I'm trying to access the entity for a given embedded form in the parent CollectionType inside FormBuilder : ParentType Class ParentType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('children', CollectionType::class, array( 'entry_type' => ChildType::class ); } } ChildType class ChildType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $child = $builder->getData(); // this returns null } public function getDefaultOptions(array $options) { return array( 'data_class' => 'Vendor

Creating a 'Ajaxified' Form Field Type

余生颓废 提交于 2019-11-28 04:26:09
问题 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