symfony 1.4 在新操作中使用后验证器会引发错误 [英] symfony 1.4 using post validator in New action throws error

查看:25
本文介绍了symfony 1.4 在新操作中使用后验证器会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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 行时,一切正常,但是我无法验证不接受我的两个字段的某些组合的条件.

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.

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

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:

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

可捕获的致命错误: *传递给 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 行*

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*

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

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

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...

我使用的是 Symfony 1.4.9,但也尝试了来自 SVN 服务器(rev 32070)的 1.4 开发分支并得到了相同的结果.Apache 2.2,PHP 5.2.6

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

推荐答案

验证器需要返回清理后的值 - 后验证器也是如此.在函数末尾添加 return $values;.

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

您的问题是由于错过了这一点,因此稍后将 null 传递给 foreach() 调用,从而导致您的第一个错误.其他人同样的原因.

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.

这篇关于symfony 1.4 在新操作中使用后验证器会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆