【问题标题】:sfValidatorAnd fails when saving a i18n objectsfValidatorAnd 在保存 i18n 对象时失败
【发布时间】:2010-04-29 10:30:19
【问题描述】:

我正在使用带有 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 有点奇怪。你知道会发生什么吗?

谢谢!

【问题讨论】:

    标签: validation forms symfony1 doctrine


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 2012-06-30
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多