【问题标题】:Custom error message not displaying properly自定义错误消息未正确显示
【发布时间】:2016-07-18 15:28:07
【问题描述】:

我正在尝试从我的 CustomForm.class.php 中获取一条自定义错误消息,该消息位于 app/module/modulename/lib 中,但不知何故我得到了另一个错误消息(可能是 Base 的默认值??)...就像无法覆盖原消息错误...

BaseClientForm.class.php

abstract class BaseClientForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'phonenumber'           => new sfWidgetFormInputText()
    ));

    $this->setValidators(array(

      'phonenumber'           => new sfValidatorString(array('max_length' => 20))
    ));

    $this->widgetSchema->setNameFormat('client[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Client';
  }

}

在ClientForm.class.php中:

class ClientForm extends BaseClientForm {

    public function configure() {

        $this->validatorSchema['phonenumber'] = new sfValidatorAnd(array(new sfValidatorString(array('min_length' => 9, 'max_length' => 9, 'required' => true),array('min_length' => 'Mínimo 9 caracteres', 'max_length' => 'Máximo 9 caracteres', 'required' => 'Obligatorio')), new sfValidatorRegex(array('pattern' => '([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])','must_match' => true))));

    }
}

在我的模板中:

<?php if($form['phonenumber']->hasError()): ?>
 <p class="error"><?php echo $form['phonenumber']->getError()->getMessage() ?></p>
<?php endif ?>

结果:

Required

如果我刚刚将其更改为“Obligio”,为什么它会显示“必需”?有什么解决办法吗?

【问题讨论】:

  • 其他定义的验证(min_length、max_length)是否正常工作?
  • 它们看起来像这样:“Minimo 9 caracteresRequired”

标签: php html symfony-1.4


【解决方案1】:

所以,调试框架我意识到你必须将required 消息传递给你的sfValidatorAnd

$this->validatorSchema['phonenumber'] = new sfValidatorAnd(array(
            new sfValidatorString(array('min_length' => 9, 'max_length' => 9), array('min_length' => 'Mínimo 9 caracteres', 'max_length' => 'Máximo 9 caracteres')), 
            new sfValidatorRegex(array('pattern' => '([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])','must_match' => true))
        ), array(), array('required' => 'My required message'));

此外,默认情况下,您的所有字段都是required,因此无需显式声明字段required

【讨论】:

  • 仍然不起作用...抱歉,出现在其他验证器中的消息是“消息无效”(例如:“Minimo 9 caracteres Invalid”),而不是我说的“必需”之前
  • 当我测试它时它对我有用。你收到我的代码的原始消息了吗?
  • 是的,它在自定义消息的结尾处不断出现“无效”:(
  • 你能用我的解决方案与我分享你的全部实施吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 2023-01-29
  • 2018-02-08
  • 1970-01-01
相关资源
最近更新 更多