Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使admingenerator忽略symfony 2中的空字段?_Php_Symfony_Symfony Forms_Symfony 2.1_Admin Generator - Fatal编程技术网

Php 如何使admingenerator忽略symfony 2中的空字段?

Php 如何使admingenerator忽略symfony 2中的空字段?,php,symfony,symfony-forms,symfony-2.1,admin-generator,Php,Symfony,Symfony Forms,Symfony 2.1,Admin Generator,我目前正在与Symfony 2 AdminGenerator进行斗争。我尝试制作一个基本的管理员用户管理表单,其中包括编辑用户数据。在那里我有现场电子邮件,密码和isActive。问题是密码即使是空的也会被保存,这不是我想要它做的 我需要密码只有在输入任何内容时才被考虑,否则它应该被忽略,并且不应该填充此字段 提前谢谢,因为我在Symfony 2中没有找到关于AG的关于此问题的信息, 博扬。以下是我的想法: class UserController extends Controller {

我目前正在与Symfony 2 AdminGenerator进行斗争。我尝试制作一个基本的管理员用户管理表单,其中包括编辑用户数据。在那里我有现场电子邮件,密码和isActive。问题是密码即使是空的也会被保存,这不是我想要它做的

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

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

以下是我的想法:

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();
            }
        }
    }
}
将请求绑定到表单之前,请将密码保存在临时变量中。然后检查新密码是否为null或“”或其他任何内容,或者将其替换为旧密码,或者对其进行编码。我不确定你的管理生成器看起来如何,但这将是一个解决这个问题的方法

你也可以研究FOSUserBundle。FOSUserBundle:user使用属性plainPassword,当通过FOSUserBundle/UserManager更新用户时,该属性被编码并替换真实密码(如果它不是空的话)


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

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

,该链接是西班牙语的,但我可以翻译为您,基本上您创建了一个实现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');
    }
}
然后,将该类注册到表单,在类类型定义的buildForm部分执行此操作:

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

$builder->addEventSubscriber($subscriber);

}

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