【问题标题】:Why is this form invalid all the time为什么这个表格一直无效
【发布时间】:2015-06-09 22:17:14
【问题描述】:

我有一个只有一个字段的表单,方法 isValid() 每次提交时都将其视为无效,我一直在提交应有的文本

//form
class CategoryForm extends Form
{
 public function __construct()
 {
    parent::__construct('category');

    $this->setAttribute('method', 'post');

    $this->add(array(
        'name' => 'name',
        'type'  => 'text',
        'options' => array('label' => 'Name',),
        'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
    ));

    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Save',
            'id' => 'submitbutton',
        ),
    ));
 }
}

我猜这可能是有问题的部分,因为它可以正常工作,所以没有错误消息。

//validation clause at controller
 $category = new Category;
 $form = new CategoryForm();
 $form->bind($category);
 $request = $this->getRequest();

    if ($request->isPost()) {
        $form->setInputFilter($category->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $em = $this->getEntityManager();
            $em->persist($category);
            $em->flush();

            $this->flashMessenger()->addSuccessMessage('category Saved');

            return $this->redirect()->toRoute('category');
        }
    }

如果我将代码更改为此,它会保留数据

public function addAction()
{
    $form = new CategoryForm();
    $form->get('submit')->setValue('Add');

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

        $category->exchangeArray($form->getData());
        $em = $this->getEntityManager();
        $em->persist($category);
        $em->flush();
        $this->flashMessenger()->addSuccessMessage('Category Saved');
        return $this->redirect()->toRoute('category');
    }
     return new ViewModel(array(
        'category' => $category,
        'form' => $form
    ));
}

【问题讨论】:

  • 没有错误消息 - 您的系统是否启用了错误报告?如果没有,请在打开 PHP 标记(例如 <?php error_reporting(E_ALL); ini_set('display_errors', 1);)之后将错误报告添加到文件顶部,然后是其余代码,以查看它是否产生任何结果。
  • 这里需要一位 zend 框架专家
  • isValid 函数检查什么?什么是有效的表格?
  • 没有 php 错误,一定和验​​证有关,不知道是什么问题
  • 就像它检查的数组状态一样,它具有价值并且它是一个字符串

标签: php forms validation zend-framework orm


【解决方案1】:

您是否真的在验证之前将提交的值传递给表单?

我通常会这样做:

if ($this->_request->isPost() && $form->isValid($this->getRequest()->getPost())) { 
...
Your save code here...

如果您这样做,请检查表单是否引发了验证错误...

var_dump($form->getMessages());

【讨论】:

  • 消息数组中的 var_dump (size=0) 为空
  • 假设显示相关代码。似乎更有可能不是您认为的问题。甚至设置了哪些验证器?我没有看到。除了required。您可以单步执行该代码吗?
  • 这就是它只有一个,它仍然说它无效,也只有一个字段。
  • 对不起。不像我1.x那样熟悉ZF2。
  • 没关系可能是我应该添加id字段,即使isvalid()为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多