【问题标题】:How to make admingenerator ignore empty fields in symfony 2?如何让管理员忽略 symfony 2 中的空字段?
【发布时间】:2012-10-17 19:43:40
【问题描述】:

我目前正在努力使用 Symfony 2 AdminGenerator。我尝试制作一个基本的管理员用户管理表单,其中包括编辑用户数据。在那里我有字段电子邮件、密码和 isActive。问题是即使密码为空也会保存,这不是我想要的。

只有在输入任何内容时才需要考虑密码,否则应将其忽略并且不应填充此字段。

提前谢谢你,因为我在 Symfony 2 中没有找到关于这个问题的 AG 信息, 博彦。

【问题讨论】:

  • 只需将旧密码保存在临时变量中(在将请求绑定到表单之前),然后将其与“新”密码进行比较,即if ($user->getPassword() === null) { $user->setPassword($tempPass)}
  • 密码字段的表单字段类型是什么?
  • 表单域的类型为“密码”。而且@mahok它不起作用,因为我尝试在预保存事件中检查它,而我得到的只是新密码......而且我不能只保存对象,因为它已经预先填充使用新密码“”。
  • 查看我的答案,因为它不适合评论;)

标签: php symfony symfony-forms symfony-2.1 admin-generator


【解决方案1】:

这是我的想法:

class UserController extends Controller
{
    public function editAction($id) {
        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository('AcmeDemoBundle:User')->find($id);
        if (!$user) {
            throw $this->createNotFoundException();
        }
        $request = $this->getRequest();

        if ('POST' === $request->getMethod()) {
            $oldPassword = $user->getPassword();
            $form->bindRequest($request);
            if ($form->isValid()) {
                if ($user->getPassword() === '' || $user->getPassword() === null) {
                    $user->setPassword($oldPassword);
                } else {
                    // Encode password
                }
                $em->flush();
            }
        }
    }
}

在将请求绑定到表单之前,请将密码保存在临时变量中。然后检查新密码是否为空或“”或其他任何内容,您可以将其替换为旧密码或对其进行编码。我不确定您的管理生成器的外观如何,但这是解决此问题的一种方法。

您还可以查看 FOSUserBundle。 FOSUserBundle:用户使用一个属性plainPassword,当通过FOSUserBundle/UserManager更新用户时,它会被编码并替换真实密码,如果它不为空。

更新:该操作显然缺少某些部分,例如成功编辑后重定向和呈现表单...只是一个快速的技巧来说明我的意思。

【讨论】:

    【解决方案2】:

    希望这对您有所帮助,两个月后 :) 最好的方法是将实现 EventSubscriberInterface 的类绑定到表单对象中的预绑定事件。使用这种方法,您不必编辑管理生成器的控制器。

    Suscriber Listener Ignore Empty fields,链接是西班牙语,不过我可以翻译给你,基本上你创建一个实现EventSubscriberInterface接口的类,博文给你这个类:

    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->factory = $factory;
            $this->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->getData();
        $data_name = $data[$this->name];
    
        if( IgnoreBlankFieldListener::isEmpty($data_name) )
        {
           $form = $event->getForm();
           unset( $data[$this->name] );
           $event->setData($data);
           $form->remove($this->name);
        }
    }
    
    public static function getSubscribedEvents() {
    
            return array(FormEvents::PRE_BIND => 'preBind');
        }
    }
    

    然后,您将此类订阅到表单,您在 ClassType 定义的 buildForm 部分执行此操作:

    public function buildForm(FormBuilder $builder, array $options)
    {
     //initial code
     //
    $subscriber = new IgnoreBlankFieldListener($builder->getFormFactory(), 'password');
    
    $builder->addEventSubscriber($subscriber);
    
    }
    

    更多信息Symfony docs

    【讨论】:

      猜你喜欢
      • 2018-12-09
      • 2018-10-28
      • 2015-11-04
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2011-02-05
      相关资源
      最近更新 更多