0

我有一个难题。如果将绑定(对象)的数据添加到表单中,我想在我的表单中,这取决于图像是否存在,项目是否显示在表单上。

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

        $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');
        }

能不能更好的解决?

4

1 回答 1

0

In the event you're looking to change the display of the form itself, the logic for rendering/not rendering the coupon should most likely live in a View Helper.

This keeps the rendering free from the controller and maintains a nice separation of concerns.

于 2014-06-24T17:06:41.627 回答