表单中的字段表现为" required&#34 ;,尽管它们不是

时间:2015-03-29 17:31:45

标签: php validation zend-framework2

我正在尝试在我的应用中为实体( Place )设置编辑表单。为此,我在控制器中编写了表单( PlaceForm ),fieldset( PlaceFieldset ),.phtml页面和函数。由于我是zf2的新手,所以我专注于他们主页上的tutorlias。我已经看到,验证是由框架提供的,但在我的情况下,没有必要。字段集中添加的字段都没有必需的属性,但是当我在所有空字段下提交表单时,我收到消息"值是必需的,并且不能为空"虽然它实际上不是必需的。我不想在这个问题中创建一个代码墙,所以我将从控制器的函数开始。如果有人想帮助我解决这个问题,请告诉我,我会提前更新代码的任何其他部分(表单,字段集等)。

...
public function editPlaceAction() {
    $id = $this->params()->fromRoute('id');
    $uiServiceProvider = $this->getServiceLocator()->get('FamilyTree\Service\UiServiceProvider');
    $placeArray = $uiServiceProvider->fetchSinglePlaceById($id);

    $place = new Places($placeArray);
    $form = new PlaceForm();
    $form->bind($place);

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
            var_dump($place);
        }
    }

    $view = new ViewModel(array(
        'form' => $form,
        'title'=>"Edit place"
    ));
    $view->setTemplate("family-tree/family-tree-maintenance/editPlace");

    return $view;
}
...

在这里你可以看到我的Fieldset看起来像

<?php

namespace Places\Form;

use Zend\Form\Fieldset;

class PlaceFieldset extends Fieldset {

public function __construct() {
parent::__construct('place');

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

    $this->add(array(
        'name' => 'name',
        'type' => 'Text',
        'options' => array(
            'label' => 'Name',
        ),
    ));

    $this->add(array(
        'name' => 'admUnit1',
        'type' => 'Text',
        'options' => array(
            'label' => 'AdmUnit1',
        ),
    ));

    }
}

0 个答案:

没有答案