symfony 1.4 using post validator in New action throws error

你。 提交于 2019-12-23 05:09:25

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5022761/symfony-1-4-using-post-validator-in-new-action-throws-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!