Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hope this helps you, two months later :) Best way of doing this is binding a class that implements the EventSubscriberInterface to the pre-bind event in the form object. Using this methods you don't have to edit the controller of the admin generator.</p> <p><a href="http://www.rjguevara.com/2013/02/symfony-2-ignorar-campos-vacios-en.html" rel="nofollow">Suscriber Listener Ignore Empty fields</a>, the link is in spanish, but I can translated to you, basically you create a class that implements the EventSubscriberInterface interface, the blog post gives you the class:</p> <pre><code>namespace Acme\TestBundle\Admin; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class IgnoreBlankFieldListener implements EventSubscriberInterface { private $factory; private $name; public function __construct(FormFactoryInterface $factory, $name) { $this-&gt;factory = $factory; $this-&gt;name = $name; } public static function isNotEmpty($var ) { if(is_array($var)) { $notempty = false; foreach( $var as $v ) { $notempty = $notempty || !IgnoreBlankFieldListener::isNotEmpty($v); } return $notempty; } else return !empty ($var); } public static function isEmpty( $var ) { return IgnoreBlankFieldListener::isNotEmpty($var); } public function preBind(FormEvent $event){ $data = $event-&gt;getData(); $data_name = $data[$this-&gt;name]; if( IgnoreBlankFieldListener::isEmpty($data_name) ) { $form = $event-&gt;getForm(); unset( $data[$this-&gt;name] ); $event-&gt;setData($data); $form-&gt;remove($this-&gt;name); } } public static function getSubscribedEvents() { return array(FormEvents::PRE_BIND =&gt; 'preBind'); } } </code></pre> <p>Then, you suscribe this class to the form, you do this in the buildForm part of the ClassType definition:</p> <pre><code>public function buildForm(FormBuilder $builder, array $options) { //initial code // $subscriber = new IgnoreBlankFieldListener($builder-&gt;getFormFactory(), 'password'); $builder-&gt;addEventSubscriber($subscriber); } </code></pre> <p>More info in <a href="http://symfony.com/doc/2.0/cookbook/form/dynamic_form_modification.html" rel="nofollow">Symfony docs</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload