symfony 1.4 在新操作中使用后验证器会引发错误

发布于 2024-10-17 15:39:59 字数 2617 浏览 5 评论 0原文

我对表单执行以下操作。在 MyForm 定义中,我添加了后验证器,因此我可以根据特定条件一次验证两个字段。当通过验证后,symfony 向我发送一个错误,并且还在数据库中的新记录中存储空值时,问题就出现了。我在这里发布我的代码...我的表单只有两个字段,用户最多从两个列表中选择每个字段来创建新记录。由于模式限制,两个字段的组合是唯一的。另外,我需要验证是否不允许使用此字段的某些组合来创建新记录。

actions.class.php:

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MyForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->form = new MyForm();
    $this->processForm($request, $this->form, true);
    $this->setTemplate('new');
  }

  protected function processForm(sfWebRequest $request, sfForm $form, $new = false)
  {
    $form->bind($request->getParameter($form->getName()),
                $request->getFiles($form->getName()));
    if ($form->isValid())
      {
        $post = $form->save();
        $this->redirect('post/' . $post->getId());
      }
  }

MyForm.class.php

class MyForm extends BaseMyForm
{
  public function configure()
  {
      $this->widgetSchema['field1'] = new sfWidgetFormChoice(array('choices' => MyUtil::$field1_vaues));
      $this->widgetSchema['field2'] = new sfWidgetFormChoice(array('choices' => MyUtil::$field2_values));

      $this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'validateBothFields'))));
   }

  public function validateBothFields($validator, $values, $arguments)
  {
    if (MyUtil::fields_relation($values['field1'], $values['field2']))
      {
        throw new sfValidatorError($validator, "Fields relation invalid!");
      }
  }
}

当我注释掉 mergePostValidator 行时,一切正常,但随后我无法验证不接受两个字段的某些组合的条件。

当按原样使用此代码时:如果组合无效,则一切正常,表单再次出现,并显示“字段关系无效!”信息。但如果组合有效,symfony 会抛出以下错误:

警告: 为 /path/to/symfony/lib/plugins/sfDoctrinePlugin/lib/form/ 中的 foreach() 提供的参数无效sfFormDoctrine.class.php 第 169 行

可捕获的致命错误: *传递给 Doctrine_Record::fromArray() 的参数 1 必须是一个数组,给定 null,在 /path/to/symfony 中调用/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php 第 150 行,并在 /ṕath/to/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php 第 1970 行定义*

警告: 第 1973 行 /path/to/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php 中的 foreach() 提供的参数无效 >

然后是一些关于无法修改响应头的警告/错误(但我自己没有修改任何内容,也许只是操作 Create 方法中的 $this->setTemplate('new') ...

我正在使用 Symfony 1.4.9,但也尝试使用 SVN 服务器(修订版 32070)的 1.4 开发分支并得到相同的结果。 阿帕奇 2.2、PHP 5.2.6

I have the following action for a form. In the MyForm definition, I added post validator, so I can validate two fields at a time by certain condition. The problem comes when, after passing validation, symfony sends me an error, and also stores null values in a new record in the database. I post my code here... My form just has two fields, and the user most select each of them from two lists to create a new record. By schema restrictions, the combination of both fields is unique. Also, I need to validate that certain combinations of this fields aren't allowed for the creation of a new record.

actions.class.php:

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MyForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->form = new MyForm();
    $this->processForm($request, $this->form, true);
    $this->setTemplate('new');
  }

  protected function processForm(sfWebRequest $request, sfForm $form, $new = false)
  {
    $form->bind($request->getParameter($form->getName()),
                $request->getFiles($form->getName()));
    if ($form->isValid())
      {
        $post = $form->save();
        $this->redirect('post/' . $post->getId());
      }
  }

MyForm.class.php

class MyForm extends BaseMyForm
{
  public function configure()
  {
      $this->widgetSchema['field1'] = new sfWidgetFormChoice(array('choices' => MyUtil::$field1_vaues));
      $this->widgetSchema['field2'] = new sfWidgetFormChoice(array('choices' => MyUtil::$field2_values));

      $this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'validateBothFields'))));
   }

  public function validateBothFields($validator, $values, $arguments)
  {
    if (MyUtil::fields_relation($values['field1'], $values['field2']))
      {
        throw new sfValidatorError($validator, "Fields relation invalid!");
      }
  }
}

When I comment out the mergePostValidator line, then all works fine, but then I cannot validate the condition of not accepting certain combination of my two fields.

And when using this code as is: if the combination is invalid, all works well, the form appears again with the 'Fields relation invalid!' message. But if the combination is valid, symfony throws the following errors:

Warning: Invalid argument supplied for foreach() in /path/to/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php on line 169

Catchable fatal error: *Argument 1 passed to Doctrine_Record::fromArray() must be an array, null given, called in /path/to/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php on line 150 and defined in /ṕath/to/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php on line 1970*

Warning: Invalid argument supplied for foreach() in /path/to/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php on line 1973

and then some warnings/errors about not being able to modify headers to response (but I am not modifying any by myself, and perhaps is just the $this->setTemplate('new') in the action Create method...

I'm using Symfony 1.4.9, but also tried with the 1.4 development branch from the SVN server (rev 32070) and got the same result.
Apache 2.2, PHP 5.2.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-10-24 15:39:59

验证器需要返回清理后的值 - 对于后验证器来说也是如此。将 return $values; 添加到函数末尾。

您的问题来自于缺少此内容,因此稍后将 null 传递给 foreach() 调用,从而导致您的第一个错误。其他人也是同样的理由。

存储空值是因为通过缺少此返回,您实际上将传递到表单的每个值清零。

A validator needs to return the cleaned value - same for the post validator. Add a return $values; to the end of your function.

Your problem comes from missing this, therefore later a null is passed to a foreach() call, causing your first error. Same reason for the others.

Null values are stored because you effectively zero out every value passed to the form by missing this return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文