internationalization - Symfony 1.4 形式,有条件地以嵌入式形式抛出 sfValidatorError

标签 internationalization symfony1 symfony-1.4 symfony-forms

我有 Page 对象:

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 Advanced Forms”指南和其他一些帖子,但我不知道我应该在 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”?

//解决方案

感谢 Jeremy,我使用了你的想法并最终得到了这个解决方案:

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

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

最佳答案

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

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

全局

当全局抛出错误时,它将显示为 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));
  }
}

关于internationalization - Symfony 1.4 形式,有条件地以嵌入式形式抛出 sfValidatorError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4470170/

相关文章:

angular - 如何读取 Angular 库中的本地 JSON 文件?

php - 为什么多词 Doctrine 搜索会导致 MySQL 使用高达 300% CPU?

php - Ubuntu symfony错误:Could not perform XLST transformation

php - 在 symfony 中执行自定义 SQL

regex -\w 是否匹配 Unicode 标准中定义的所有字母数字字符?

ruby-on-rails - Rails 3.1 共享客户端/服务器模板,支持 i18n

ruby-on-rails - Rails不允许更改语言环境

templates - 同一 symfony 应用程序中不同 View 的不同布局

php - Symfony Doctrine 查询错误

symfony1 - 如何在后端加载不同的前端模块模板以使用 symfony 1.4,doctrine 自定义模板内容