字段集验证数据

Fieldset validation data

我在 ZF2 中遇到了字段集问题,我向您展示了我的问题。

这是我的表格(由AngularJS制作,不是ZF2制作的),您可以在其中输入名称,并且select如果您允许哪个页面或哪个动作(页面由动作组成).

下图部分显示了我发送到 ZF2 的内容:

这是我的 CustomRole class 的数据模型:

class CustomRole
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Users\CustomRolePagePermission", mappedBy="customRole")
     */
    protected $pagePermissions;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Users\CustomRoleActionPermission", mappedBy="customRole")
     */
    protected $actionPermissions;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Users\GlobalRole")
     */
    protected $globalRole;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Cie", inversedBy="customRoles")
     * @ORM\JoinColumn(name="cie", referencedColumnName="id_cie")
     */
    protected $cie;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Users\User", mappedBy="customRole")
     */
    protected $users;
...

这里是我的 class CustomRolePagePermission(与 CustomRoleActionPermission 几乎相同):

class CustomRolePagePermission extends PagePermission
{

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="App\Entity\Users\CustomRole", inversedBy="pagePermissions")
     * @ORM\JoinColumn(name="custom_role_id", referencedColumnName="id", nullable=false)
     */
    protected $customRole;
...

然后抽象class PagePermission :

abstract class PagePermission
{

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="App\Entity\Page")
     * @ORM\JoinColumn(name="page_id", referencedColumnName="id", nullable=false)
     */
    protected $page;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $permission;
...

现在CustomRole对应的Fieldsetclass(我在每个实体上都做了):

class CustomRoleFieldset extends Fieldset implements InputFilterProviderInterface {

    protected $serviceLocator;

    public function __construct(EntityManager $entityManager) {
        parent::__construct('role');

        $this->setHydrator(new DoctrineHydrator($entityManager, 'App\Entity\Users\CustomRole'))
            ->setObject(new CustomRole());

        $this->add(array('name' => 'name'));

        $customRolePagePermissionFieldset = new CustomRolePagePermissionFieldset($entityManager);
        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'pagePermission',
            'options' => array(
                'target_element' => $customRolePagePermissionFieldset
            ),
        ));

        $customRoleActionPermissionFieldset = new CustomRoleActionPermissionFieldset($entityManager);
        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'actionPermission',
            'options' => array(
                'target_element' => $customRoleActionPermissionFieldset
            ),
        ));
    }

    public function getInputFilterSpecification() {
        return array(
            'name' => array('required' => true),
            'pagePermission' => array('required' => true),
            'actionPermission' => array('required' => true),
        );
    }
}
...

这里是我的字段集 CustomRolePagePermissionFieldset :

class CustomRolePagePermissionFieldset extends Fieldset implements InputFilterProviderInterface {

    protected $serviceManager;

    public function __construct(EntityManager $entityManager) {
        parent::__construct();

        $this->setHydrator(new DoctrineHydrator($entityManager, 'App\Entity\Users\CustomRolePagePermission'))
            ->setObject(new CustomRolePagePermission());

        $this->add(array('name' => 'permission'));
    }
...

然后,我的控制器:

...
$customRoleForm = new CustomRoleForm($em);
$customRole = new CustomRole();
$formData = $request->getPost();
$customRoleForm->bind($customRole);
$customRoleForm->setData($formData);

if ($customRoleForm->isValid()) {
    $customRole->setCie($cie);
    $customRole->setGlobalRole($globalRole);
    $em->persist($customRole);
    $em->flush();
    return $this->ok($customRole->getId());
}
...

问题

当我发送表单时,创建了 CustomRole,但之前检查的页面和操作没有与创建的 CustomRole 链接,就好像我从未检查过任何复选框一样。

我不明白为什么它没有效果,你有什么想法吗?

提前致谢! :)

您的 collection 应该是 "pagePermissions" 以便水龙头调用 setPagePermissions。