php - Zend Form Hydrator 类方法未正确绑定(bind)到对象实体的问题

标签 php zend-framework zend-framework2 zend-form zend-form2

我试图使用 Classmethod() Hydrator 将具有默认值(使用 getter)的实体 Contact 绑定(bind)到表单 ContactForm。

问题是,当我使用一组值调用 setData 时,Hydrator 无法合并默认值和该组值,而是仅返回该组值。请在下面找到我的代码的摘录。

<?php
// My contact form
namespace Application\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;

class Contact extends Form
{
    public function __construct($name = 'contact')
    {
        parent::__construct($name);

        $this->add(array(
            'name' => 'name',
            'options' => array(
                'label' => 'Your name',
            ),
            'type'  => 'Text',
        ));
        $this->add(array(
            'name' => 'subject',
            'options' => array(
                'label' => 'Subject',
            ),
            'type'  => 'Text',
        ));
        $this->add(new \Zend\Form\Element\Csrf('security'));
        $this->add(array(
            'name' => 'send',
            'type'  => 'Submit',
            'attributes' => array(
                'value' => 'Submit',
            ),
        ));

        // We could also define the input filter here, or
        // lazy-create it in the getInputFilter() method.
    }


    public function getInputFilter()
    {
        if (!$this->filter) {
            $inputFilter = new InputFilter();
            $inputFilter->add(array('name' => 'name', 'required' => false));
            $inputFilter->add(array('name' => 'subject', 'required' => false));
            $this->filter = $inputFilter;
        }
        return $this->filter;
    }
}

这是我的实体

class Contact
{

    protected $name;
    protected $subject;

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $subject
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;
    }

    /**
     * @return mixed
     */
    public function getSubject()
    {
        // Trying to set a default value
        if (null == $this->subject) {
            return 'default subject';
        }
        return $this->subject;
    }

}

这里我在 Controller 操作中测试它

class TestController extends AbstractActionController
{
    public function indexAction()
    {
        $data = array(
            'name' => 'myName'
        );

        $class = '\Application\Entity\Contact';
        $contact = new $class;

        $form = new \Application\Form\Contact();
        $form->setHydrator(new ClassMethods(false));
        $form->bind($contact);
        $form->setData($data);

        if ($form->isValid()) {
            var_dump($form->getData());
        }
        die("end");
    }
}

我想得到

object(Application\Entity\Contact)[---]
    protected 'name' => string 'myName' (length=6)
protected 'subject' => string 'default subject' ...

但是我得到了这个结果

object(Application\Entity\Contact)[---]
  protected 'name' => string 'myName' (length=6)
  protected 'subject' => null

知道如何让 Classmethod() 从绑定(bind)中提取 getter 值并合并 setData() 上的剩余数据吗?

最佳答案

设置默认值实际上很容易。 定义实体中的默认值:

class Contact
{
   protected $subject = 'Default Subject';

  // other properties and methods
}

此外,您还可以在表单中定义默认值:

        $this->add(array(
            'name' => 'subject',
            'options' => array(
                'label' => 'Subject',
            ),
            'attributes' => array(
                'value' => 'Default Subject'
            ),
            'type'  => 'Text',
        ));

关于php - Zend Form Hydrator 类方法未正确绑定(bind)到对象实体的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852853/

相关文章:

php - 弃用 : mysql_real_escape_string(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO

php - 如何根据主查询对表中的项目进行计数?

php - 使用 Zend Framework 的原始 SQL 查询

php - 雅虎老板 - 链接 - 困惑!

php - 学说 2 实体关系删除

doctrine-orm - 如何在Doctrine 2中测试该连接是否工作?

php - 访问根目录之外的文件

php - IE7 中 foreach 循环中的文本大小增加

zend-framework - 为什么 Google 会截断我的网址? (使用 Zend 框架)

zend-framework2 - 无法在 Zend 上的 TCP 连接 api.mysite.com 上启用加密