保存i18n对象时sfValidatorAnd失败

时间:2010-04-29 10:30:19

标签: validation forms symfony1 doctrine

我正在使用带有Doctrine的Symfony 1.2。我有一个i18n行为的对象。如果我有以下验证器,则在保存表单时失败,并显示错误“SQLSTATE [23000]:完整性约束违规:1048列'id'不能为空”(它在保存对象之前尝试保存object_translation)

$this->validatorSchema['phone'] = new sfValidatorAnd(array(
  new sfValidatorPhone(),
  new sfValidatorString(array('max_length' => 50)),
), array('required'=> false));

如果我只有一个验证器(两者都有),它可以工作:

$this->validatorSchema['phone'] = new sfValidatorPhone();

另外两个领域也是如此。所以sfValidatorAnd和i18n有些奇怪。你知道会发生什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,但我仍然不知道为什么原始代码不起作用。我所做的是拥有一个独特的定制验证器:

$this->validatorSchema['phone'] = new sfValidatorPhone(array('max_length' => 50, 'required' => false));

并且sfValidatorPhone扩展了sfValidatorString:

<?php

class sfValidatorPhone extends sfValidatorString
{
  protected function configure($options = array(), $messages = array())
  {
    $this->addOption('required');

    $this->addMessage('required', 'The phone is required.');

    $this->addMessage('international', sfContext::getInstance()->getI18n()->__('The phone must have the international country code (+/00)'));

    parent::configure($options, $messages);
  }

  protected function doClean($value)
  {
    $phone = preg_replace('/[^0-9|+]/','',$value);

    $phone = parent::doClean($phone);

    ...code...

    return $phone;

  }
}