当我从表单中读取数据时,“Zend \ Form \ Element \ Select”获取空值

时间:2016-10-12 09:55:21

标签: forms zend-framework2

我有一个完美的代码(将数据写入db表)而没有Zend \ Form \ Element \ Select。但是当我把Select元素放在表单中时,代码不起作用,但也没有显示任何错误消息。它根本不会将数据写入数据库。 所以我测试了一点控制器代码,结果如下:

这是控制器逻辑:

public function addAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) {
        $this->postForm->setData($request->getPost());

        if ($this->postForm->isValid()) {
            try {
                $this->postService->savePost($this->postForm->getData());

                return $this->redirect()->toRoute('blog');
            } catch (\Exception $e) {
                // Some DB Error happened, log it and let the user know
            }
        }
    }

    return new ViewModel(array(
        'form' => $this->postForm
    ));
}

首先,我测试了来自请求的数据:

public function addAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) {
          $data = $request->getPost();
          var_dump($data); 
    }

    return new ViewModel(array(
        'form' => $this->postForm
    ));
}

我得到了一个具有准确值的数组。 'category_id'是Zend \ Form \ Element \ Select。

  array (size=9)
  ... 'category_id' => string '4' (length=1) ...

然后我测试了表格:

public function addAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) {
        $this->postForm->setData($request->getPost());
        var_dump($this->postForm);

    }

    return new ViewModel(array(
        'form' => $this->postForm
    ));
}

我得到了同样准确的结果:

array (size=9)
... 'category_id' => string '4' (length=1) ...

然后我测试了表格是否有效:

public function addAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) {
        $this->postForm->setData($request->getPost());

        if ($this->postForm->isValid()) {
            echo "The form is valid";
        }  
    }

    return new ViewModel(array(
        'form' => $this->postForm
    ));
}   

是的,表格有效。 然后我再次测试了表格中的数据。

public function addAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) {
        $this->postForm->setData($request->getPost());

        if ($this->postForm->isValid()) {
            $data = $this->postForm->getData();
            var_dump($data);
        }  
    }

    return new ViewModel(array(
        'form' => $this->postForm
    ));
}

我在这里遇到了问题:

... 'category_id' => null ...

我希望我很清楚。任何帮助都非常受欢迎。

这就是我的PostFieldset的定义方式:

namespace Blog\Form;
use Zend\Form\Fieldset;
use Blog\Model\Post;
use Zend\Stdlib\Hydrator\ClassMethods;

class PostFieldset extends Fieldset
{
    public function __construct($name = null, $options = array())
    {
        parent::__construct($name, $options);

        $this->setHydrator(new ClassMethods(false));
        $this->setObject(new Post());

        $this->add(array(
            'type' => 'hidden',
            'name' => 'id'
        ));

        $this->add(array(
            'type' => 'text',
            'name' => 'content',
            'options' => array(
                'label' => 'Blog Content'
            )
        ));

        $this->add(array(
            'type' => 'text',
            'name' => 'title',
            'options' => array(
                'label' => 'Blog Title'
            )
        ));

        $this->add(array(
            'type' => 'Zend\Form\Element\Select',
            'name' => 'category_id',
            'options' => array(
                'label' => 'Category',
                'value_options' => array(
                    '1' => 'Gold Rings',
                    '2' => 'White Gold Rings',
                    '3' => 'Silver Rings',
                    '4' => 'Birthstone Rings',
                    '5' => 'Diamond Rings',

                 ),
             )
        ));
    }
}

0 个答案:

没有答案