Sorry, I am new at symfony2, and I mean disable some validation rules on 
create/update.

This is my *Entity*, I set for field *password* validation rule *NotBlank*, 
and this is true only for create, but on update I want disable this rule, 
how I can do this?, Or maybe I should do this in *Form* class by set *
required* option, and remove *NotBlank* rule in *Entity*?
------------------------------------------------------------------
<?php
//*Entity* class
class User implements UserInterface
{
    /**
     * @var string
     * @ORM\Column()
     * @Assert\NotBlank()
     */
    private $password;
------------------------------------------------------------------



Now I check if item id not null, and password empty, I fill field password 
old data, and it pass validates, but I dont like this solution.
------------------------------------------------------------------
<?php
//*Form* class
class User extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('password', 'password', array('required' => false));

        $clientData = null;
        if ($options['data']->getId() !== null) {
            $builder->addEventListener(FormEvents::PRE_BIND, function(Event 
$event) use(&$clientData) {
                $clientData = $event->getData();

                if (!empty($clientData['password'])) {
                    $event->getForm()->getData()->setNeedEncode(true);
                }

            });

            $builder->addEventListener(FormEvents::BIND_CLIENT_DATA, 
function(Event $event) use(&$clientData) {
                if (empty($clientData['password'])) {
                    $clientData['password'] = 
$event->getForm()->getData()->getPassword();
                }

                $event->setData($clientData);
            });
        }
    }
}
------------------------------------------------------------------

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to