使用实体之间的空映射关联验证Zend表单

时间:2013-09-02 18:11:32

标签: forms validation doctrine-orm zend-framework2 associations

首先,对不起我的英语。

我的订单实体

* @ORM\Entity
* @ORM\Table(name="orders")
class Order implements InputFilterAwareInterface
{
    ...
    /**
     * Order status
     *
     * @ORM\OneToOne(targetEntity="Admin\Entity\OrdersStatus")
     * @ORM\JoinColumn(name="status_id", referencedColumnName="id")
     **/
    private $status;
    ...
}

我订单的雕像实体

* @ORM\Entity
* @ORM\Table(name="orders_statuses")
class OrdersStatus implements InputFilterAwareInterface
{
    /**
     * @var Zend\InputFilter\InputFilter
     */
    protected $inputFilter;

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

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

我的订单生成

class OrderForm extends Form
{
    public function __construct(EntityManager $entityManager)
    {
        parent::__construct('Order');

        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'itemForm');

        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type' => 'hidden',
            ),
        ));

        $this->add(array(
            'name'    => 'status',
            'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
            'options' => array(
                'label' => 'Status',
                'object_manager' => $entityManager,
                'empty_option'   => '--- Select status ---',
                'target_class'   => 'Admin\Entity\OrdersStatus',
                'property'       => 'title'
            ),
            'attributes' => array(
                'required' => false
            )
        ));
    }
}

我的OrderController

class OrderController extends AbstractActionController
{
...
public function editAction()
{
    $em = $this->getEntityManager();
    $id = (int) $this->getEvent()->getRouteMatch()->getParam('id');

    $item = new Stock\Entity\Order;

    if ($id) {
        $item = $em->getRepository($this->entity)->find($id);
    }

    $form = new Stock\Form\OrderForm($em);
    $form->setHydrator(new DoctrineEntity($em, Stock\Entity\Order, false));
    $form->bind($item);

    $request = $this->getRequest();
    if ($request->isPost()) {
        $post = $request->getPost();
        $form->setInputFilter($item->getInputFilter());
        $form->setData($post);

        if ($form->isValid()) {
            $em->persist($item);
            $em->flush();

            return $this->redirect()->toRoute('stock/order', array('action' => 'index'));
        }
    }
...
}

当我尝试在不选择订单状态的情况下验证表单时,发生了以下错误:“值是必需的,不能为空”。

当我使用此输入过滤器时:

public function getInputFilter()
{
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();
        $inputFilter->add($factory->createInput(array(
            'name'     => 'client',
            'required' => false,
        )));

        $inputFilter->add($factory->createInput(array(
            'name'        => 'status',
            'allow_empty' => true,
            'required'    => false,
            'filters'     => array(
                array('name' => 'Null'),
            ),
        )));
    }
}

我得到了这个: Admin \ Entity \ OrdersStatus

的查询缺少标识符ID

没有Null过滤器:

  

执行'SELECT t0.id AS id1,t0.title时发生异常   AS title2,t0.description AS description3 FROM orders_statuses t0   在哪里t0.id =?'与params [“”]:

     

SQLSTATE [22P02]:文本表示无效:7错误:输入无效   整数的语法:“”

在订单状态中允许空值的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我认为麻烦在你的订单实体中,试着改变:

  • @ORM \ OneToOne(targetEntity ="管理\实体\ OrdersStatus&#34)

到此:

  • @ORM \多对一(targetEntity ="管理\实体\ OrdersStatus&#34)

我还会删除"数组('名称' =>' Null'),"来自状态inputFilter的过滤器,因为required = FALSE