【问题标题】:Symfony 1.4 form, conditional throwing sfValidatorError in embedded formSymfony 1.4 形式,有条件地以嵌入形式抛出 sfValidatorError
【发布时间】:2011-05-27 02:11:15
【问题描述】:

我有页面对象:

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;
  }

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

【问题讨论】:

    标签: internationalization symfony1 symfony-1.4 symfony-forms


    【解决方案1】:

    这应该是一个帖子验证器,因为您使用了多个值。

    当通过 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));
      }
    }
    

    【讨论】:

    • 它有效,但仅在 PageTrasnaltionForm 中有效。现在,只有当 PageForm 中的 is_course 设置为 true 时,我才必须启用此 PostValidator - 怎么做?
    • 如果 is_course 不可变,您可以在添加 post 验证器之前进行检查。如果它是表单的一部分,请始终保持启用状态,如果 is_course 为 false,则不要抛出错误。
    猜你喜欢
    • 2015-03-10
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多