Zend framework2 Zend框架2表单

Zend framework2 Zend框架2表单,zend-framework2,zend-form2,Zend Framework2,Zend Form2,我左右为难。 我想在我的表单中,如果绑定(对象)的数据添加到表单中,取决于图像是否存在,该项是否显示在表单上 我是这样解决的,但我不知道这是否正确 $id = (int) $this->params()->fromRoute('id', 0); $coupon = $this->getEntityManager()->find('Application\Entity\Coupon', $id); $forms = $thi

我左右为难。 我想在我的表单中,如果绑定(对象)的数据添加到表单中,取决于图像是否存在,该项是否显示在表单上

我是这样解决的,但我不知道这是否正确

        $id = (int) $this->params()->fromRoute('id', 0);
        $coupon = $this->getEntityManager()->find('Application\Entity\Coupon', $id);

        $forms = $this->getServiceLocator()->get('FormElementManager');
        $form = $forms->get('CouponForm');

        $form->bind($coupon);
        $form->setBindOnValidate(false);
        $form->get('dateStart')->setValue($coupon->getDateStart()->format('Y-m-d'));
        $form->get('dateEnd')->setValue($coupon->getDateEnd()->format('Y-m-d'));

        if($coupon->getImageUrl()) {
            $form->get('image')->setAttribute('src', $coupon->getImageUrl());
        }else {
            $form->remove('image');
        }

解决这个问题会更好吗?

如果您希望更改表单本身的显示,则呈现/不呈现优惠券的逻辑很可能位于视图帮助器中

这使渲染不受控制器的影响,并保持了良好的关注点分离