用户组的奏鸣曲验证不起作用

问题描述 投票:0回答:4

我将sonata 2.8与fos用户捆绑包一起使用,我扩展了用户实体和admin,它唤醒了除验证用户组之外的所有内容。

我可能会出现内联错误,如果用户没有选择几乎一个组,可能会阻止发送数据?

我已经尝试过

->add('groups', 'sonata_type_model', array(
                    'expanded' => true,
                    'multiple' => true,
                    'required'    => true
                ))

但无法撤消

问题出现在与多对多或多对多的关系的每个字段中,例如,我有2个实体paese,zone,带有

// paeseAdmin.php

$formMapper
        ->with('Zone')
            ->add('name', 'text' , array('label'=>"Paese"))
            ->add('zone','sonata_type_collection',array('by_reference' => false, 'required' => true,), array( 'edit' => 'inline','inline' => 'table', 'class' => 'test',))

        ->end() 
    ;

// paese  entity
/**
* @var Zona
* @ORM\OneToMany(targetEntity="Zona", mappedBy="paesi" ,  orphanRemoval=true, cascade={"persist"})
* @ORM\OrderBy({"nome" = "ASC"})
*/
private $zone;



// zona entity
/**
 * @var Paese
 * @ORM\ManyToOne(targetEntity="Paese", inversedBy="zone" )
 * @ORM\OrderBy({"nome" = "ASC"})
 * @ORM\JoinColumn(name="paese_id", referencedColumnName="id")
*/
private $paesi;

任何想法?谢谢

sonata-admin sonata
4个回答
2
投票

[如果您使用的是Sonata Admin捆绑包,则可以使用validate继承的功能来进行高级验证(例如集合计数)

例如:

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\CoreBundle\Validator\ErrorElement;

class UserAdmin extends AbstractAdmin
{

    /// ...

    public function validate(ErrorElement $errorElement, $object)
    {
        if (!$object->getGroups()->count()) {
            $errorElement
                ->with('groups')
                ->addViolation('You should select at least one group')
                ->end();
        }

        parent::validate($errorElement, $object);
    }
}


0
投票

您有'required' => false,然后有'required' => true,您需要删除多余的一个

摘自奏鸣曲类型集合的奏鸣曲文档

将'required'选项设置为true不会导致要求'至少一个'子实体。将“ required”选项设置为false也会导致所有嵌套表单字段也不再必需。

[将required => true添加到sonata_type_collection字段时,仅允许在子窗体中具有必填字段。


0
投票

因此您必须在实体用户中添加这些行

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;


    /**
     * @Assert\Callback(methods={"isGroupValid"})
     */
     public function isGroupValid(ExecutionContextInterface $context)
    {
        if ($this->groups->count()==0) {
            $context
                 ->buildViolation('choisir au minimum un group!', array(), null)
                 ->atPath('groups')
                 ->addViolation()`enter code here`;
        }
    }

0
投票

在我的奏鸣曲版本中,对我没有任何帮助。

sonata-project / admin-bundle 3.51.0

symfony好友/用户捆绑v2.1.2

无论如何都不会调用validate方法。

所以我最终写了一个验证:

#File: src/Application/Sonata/UserBundle/Resources/config/validation.xml

    <property name="groups">
        <constraint name="Count">
            <option name="min">1</option>
            <option name="minMessage">Please choose one group.</option>
        </constraint>
    </property>
© www.soinside.com 2019 - 2024. All rights reserved.