3

我有页面对象:

Page:
  actAs:
    Timestampable: ~
    I18n:
      fields: [name,content,attachment,course_name, course_description ]
      actAs:
        Sluggable: { fields: [name], uniqueBy: [lang, name], canUpdate: true }
  columns:
  ...
    is_course:            { type: boolean }
    course_name:          { type: string(255) }
    course_description:   { type: string(500) }

以及带有嵌入式 i18n 表单的 PageForm:

//PageForm.class.php 
public function configure()
{
...  
    $this->embedI18n(array('pl','en'));
    $this->widgetSchema->setLabel('en', 'Angielski');
    $this->widgetSchema->setLabel('pl', 'Polski');
}

当 is_course 设置为 false 时,不需要字段 course_name 和 course_description。但是,如果启用了 is_course,验证应该会抛出需要 course_name 和 course_description 的错误。

我已阅读“Symfony 高级表单”指南和其他一些帖子,但我不知道应该在 PageForm 中使用 sfValidatorCallback 还是在 PageTranslationForm 中使用 PostValidator?我尝试以这种方式使用 sfValidatorCallback:

//PageForm.class.php
public function configure()
{
...
       $this->validatorSchema->setPostValidator(
          new sfValidatorCallback(array('callback' => array($this,'checkCourses')))
          );
}

public function checkCourses($validator, $values)
{
    if($values['is_course'])
    {
      if($values['pl']['course_name'] && !$values['pl']['course_description'])
      {
          $error = new sfValidatorError($validator,'Required filed');
          throw new sfValidatorErrorSchema($validator, array( _what_name_ => $error));
      }
    }
    return $values;
}

但我不知道如何在 $values['pl']['course_description'] 中抛出错误,因为 Symfony API 说 _what_name_ 应该是一个错误数组。

我真的很困惑在 symfony 中验证表单的过程中是什么。

//编辑

我在 PageTranslationForm 中做了一些更改,现在看起来像这样... //PageTranslationform.class.php

 public function configure()
 {
 //......

  $this->validatorSchema->setPostValidator(
          new sfValidatorCallback(array('callback' => array($this,'checkCourses')))
   );

 //.....
 }

public function checkCourses($validator, $values)
{
    if($values['course_name'] && !$values['course_description'])
    {
        $error = new sfValidatorError($validator,'Required');
        throw new sfValidatorErrorSchema($validator, array( 'course_description' => $error));
    }
    elseif(!$values['course_name'] && $values['course_description'])
    {
        $error = new sfValidatorError($validator,'Required');
        throw new sfValidatorErrorSchema($validator, array( 'course_name' => $error));

    }
    return $values;
}

它几乎可以工作,但是......只有在 PageForm is_course 设置为“true”时才应该启用这个验证器。如何在 PageTranslationForm 的 checkCourses 函数中从 PageForm 访问字段“is_course”?

//解决方案

谢谢杰里米,我用了你的想法,终于得到了这个解决方案:

//PageForm.class.php
public function configure()
{
    $this->embedI18n(array('pl','en'));
    $this->widgetSchema->setLabel('en', 'Angielski');
    $this->widgetSchema->setLabel('pl', 'Polski');

//.....
  if($this->getObject()->getIsCourse())
  {
      foreach($this->getEmbeddedForms()  as $key => $form)
      {
          $this->validatorSchema[$key]->setPostValidator(
             new sfValidatorCallback(array('callback' => array($form,'checkCourses')))
          );
      }
  }
}

//PageTranslationForm.class.php
//configure etc

  public function checkCourses($validator, $values)
  {
       $errorSchema =  new sfValidatorErrorSchema($validator);


        if($values['course_name'] && !$values['course_description'])
        {
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_description');
        }
        elseif(!$values['course_name'] && $values['course_description'])
        {
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_name');
        }
        elseif(!$values['course_name'] && !$values['course_description'])
        {
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_name');
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_description');
        }


        if (count($errorSchema))
        {
          throw new sfValidatorErrorSchema($validator, $errorSchema);
        }

      return $values;
  }

感谢您的建议,它工作得很好,我希望它会有所帮助:)

4

1 回答 1

2

这应该是一个后验证器,因为您正在使用多个值。

通过 post 验证器进行验证时,您可以通过两种不同的方式抛出错误:

全球范围内

当全局抛出错误时,它将显示为sfForm::renderGlobalErrors. 要全局抛出,只需在回调中抛出错误:

public function checkCourses($validator, $values)
{
  if ($invalid)
  {
    throw new sfValidatorError($validator, 'Required.'); //global messages should be more specific than this
  }
}

本地

要在本地呈现错误,请按照您的做法抛出带有数组的模式。数组的键将确定呈现错误的字段。这可能是你想要的。

public function checkCourses($validator, $values)
{
  if ($invalid)
  {
    $error = new sfValidatorError($validator,'Required filed');
    throw new sfValidatorErrorSchema($validator, array('course_description' => $error));
  }
}
于 2010-12-17T14:30:24.280 回答