2

Classmethod() ハイドレーターを使用して、エンティティ Contact をデフォルト値 (ゲッターを使用) でフォーム 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;
    }

}

ここでは、コントローラーアクションでテストします

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() がバインドから getter 値を抽出し、残りのデータを setData() にマージする方法はありますか?

4

1 に答える 1

2

それは実際には静かで簡単にデフォルト値を設定できます。エンティティでデフォルト値を定義します。

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',
        ));
于 2014-02-18T12:57:55.740 に答える