【问题标题】:Zend Framework 2 formZend Framework 2 形式
【发布时间】:2013-11-19 00:51:31
【问题描述】:

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

我是这样解决的,不知道对不对。

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

能更好地解决吗?

【问题讨论】:

    标签: zend-framework2 zend-form2


    【解决方案1】:

    如果您希望更改表单本身的显示,则呈现/不呈现优惠券的逻辑很可能存在于 View Helper 中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      相关资源
      最近更新 更多