8

我的 CRUD 应用程序中的 addAction 有问题。在控制器上,逻辑没有通过 $form->isValid() 验证,但表单没有显示任何错误消息。

我试过这个(谢谢山姆):

foreach($form->get('product')->getElements() as $el)
{
    echo $el->getName()." = ".$el->getValue()." > ".$el->getMessages()." <br/>";
}

那只显示字段的名称和值,但不显示错误消息。

我试过让表单完全空白,它会触发“值是必需的,不能为空”的错误消息,但是,我一个接一个地填写每个字段,直到我没有收到更多错误消息但表单仍然无效.

我的表单有一个产品字段集作为基本字段集和一个提交按钮。在产品字段集中,我有一个 ID 字段、一个名称字段、一个价格字段和一个品牌字段集。在我的品牌字段集中,我有一个 id 字段。像这样:

产品形式:

class ProductForm extends Form
{

    public function init()
    {
        // we want to ignore the name passed
        parent::__construct('product');
        $this->setName('product');
        $this->setAttribute('method', 'post');

        $this->add(array(
               'name' => 'product',
                'type' => 'Administrador\Form\ProductFieldset',
                'options' => array(
                        'use_as_base_fieldset' => true
                ),
        ));
        $this->add(array(
                'name' => 'submit',
                'type' => 'Submit',
                'attributes' => array(
                        'value' => 'Add',
                        'id' => 'submitbutton',
                ),
        ));
    }
}

产品字段集:

class ProductFieldset extends Fieldset implements ServiceLocatorAwareInterface
{

    protected $serviceLocator;

    function __construct($name = null)
    {
        parent::__construct('product_fieldset');

        $this->setHydrator(new ArraySerializableHydrator());
        $this->setObject(new Product());
    }

    public function init()
    {

        $this->add(array(
                'name' => 'id',
                'type' => 'Hidden',
        ));
        $this->add(array(
                'name' => 'name',
                'type' => 'Text',
                'options' => array(
                        'label' => 'Name',
                ),
        ));
        $this->add(array(
                'name' => 'price',
                'type' => 'Text',
                'options' => array(
                        'label' => 'Price',
                ),
        ));
        $this->add(array(
                'name' => 'brand',
                'type' => 'BrandFieldset',
        ));
    }


    public function setServiceLocator(ServiceLocatorInterface $sl)
    {
        $this->serviceLocator = $sl;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
}

品牌字段集:

class BrandFieldset extends Fieldset
{
    function __construct(BrandTable $brandTable)
    {
        parent::__construct('brand_fieldset');

        //$this->setHydrator(new ClassMethodsHydrator(false))->setObject(new Brand());

        $this->setHydrator(new ArraySerializableHydrator());
        $this->setObject(new Brand());

        $brandSelectOptionsArray = $brandTable->populateSelectBrand();
        $this->add(array(
                'name' => 'id',
                'type' => 'Select',
                'options' => array(
                        'label' => 'Brand',
                        'empty_option' => 'Please select a brand',
                        'value_options' => $brandSelectOptionsArray,
                ),
        ));
    }
}

这是我在 addAction 中的新 Form 语句:

$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('Administrador\Form\ProductForm');

在我的模型“产品”中,我有 inputFilters、“id”字段所需的过滤器、“name”字段所需的过滤器。对于 Brand 字段,我创建了其他 inputFilter 并将其添加到主 inputFilter:

$brandFilter->add($factory->createInput(array(
'name'     => 'id',
'required' => true,
'filters'  => array(
    array('name' => 'Int'),
),
)));
$inputFilter->add($brandFilter, 'brand');

奇怪的行为是我的 editAction 工作正常并且具有相同的逻辑。

是否有任何形式从表单中回显内部错误消息,这有助于我理解为什么表单无效。

编辑 2013-06-01

这是我的完整控制器:

class ProductController extends AbstractActionController
{

    protected $productTable;
    protected $brandTable;

    public function indexAction()
    {
        return new ViewModel(array(
            'products' => $this->getProductTable()->fetchAll(),
        ));
    }

    public function addAction()
    {
        $formManager = $this->serviceLocator->get('FormElementManager');
        $form = $formManager->get('Administrador\Form\ProductForm');
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $product = new Product();
            $product->brand = new Brand();
            $form->setInputFilter($product->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $product->exchangeArray($form->getData());
                $this->getProductTable()->saveProduct($product);

                // Redirect to list of products
                return $this->redirect()->toRoute('product');
            }
        }
        return new ViewModel(array(
            'form' => $form,
        ));
    }

    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('product', array(
                    'action' => 'add'
            ));
        }

        // Get the Product with the specified id.  An exception is thrown
        // if it cannot be found, in which case go to the index page.
        try {
            $product = $this->getProductTable()->getProduct($id);
        }
        catch (\Exception $ex) {
            return $this->redirect()->toRoute('product', array(
                    'action' => 'index'
            ));
        }
        $formManager = $this->serviceLocator->get('FormElementManager');
        $form = $formManager->get('Administrador\Form\ProductForm');
        $brand = $this->getBrandTable()->getBrand($product->brand);
        $product->brand = $brand;
        $form->bind($product);
        $form->get('submit')->setAttribute('value', 'Edit');

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

            if ($form->isValid()) {
                $this->getProductTable()->saveProduct($form->getData());

                // Redirect to list of products
                return $this->redirect()->toRoute('product');
            }
        }

        return array(
                'id' => $id,
                'form' => $form,
        );
    }

    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('product');
        }

        $request = $this->getRequest();
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                $id = (int) $request->getPost('id');
                $this->getProductTable()->deleteProduct($id);
            }

            // Redirect to list of products
            return $this->redirect()->toRoute('product');
        }

        return array(
                'id'    => $id,
                'product' => $this->getProductTable()->getProduct($id)
        );
    }


    public function getProductTable()
    {
        if (!$this->productTable) {
            $sm = $this->getServiceLocator();
            $this->productTable = $sm->get('Administrador\Model\ProductTable');
        }
        return $this->productTable;
    }

    public function getBrandTable()
    {
        if (!$this->brandTable) {
            $sm = $this->getServiceLocator();
            $this->brandTable = $sm->get('Administrador\Model\BrandTable');
        }
        return $this->brandTable;
    }
}
4

3 回答 3

1

我的情况是我通过了错误的输入过滤器。isValid返回 false,但$form->getMessages()为空。表格OrderForm有以下内容:

$form->setInputFilter(new \Application\Form\UserInputFilter($er));

当我改变UserInputFilter它的OrderInputFilter工作原理。

于 2015-07-29T00:22:57.347 回答
0

好吧,我得到了答案:D

addAction 应该是这样的:

public function addAction()
{
    $formManager = $this->serviceLocator->get('FormElementManager');
    $form = $formManager->get('Administrador\Form\ProductForm');
    $form->get('submit')->setValue('Add');

    $product = new Product();
    $product->brand = new Brand();

    $form->bind($product);  // I need to bind the product to the form to pass the isValid() validation

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

        if ($form->isValid()) {
            $product = $form->getData();
            $this->getProductTable()->saveProduct($product);

            // Redirect to list of products
            return $this->redirect()->toRoute('product');
        }
    }
    return new ViewModel(array(
        'form' => $form,
    ));
}

显然我需要将产品对象绑定并清空到表单才能通过 isValid() 验证。之后,我从 $form->getData() 中检索产品对象。

于 2013-06-03T21:09:14.133 回答
0

你也可以这样做:$form->setBindOnValidate(false);

于 2015-01-19T15:14:37.507 回答