2010-04-29 114 views
1

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

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

如果我只有一个验证器(任何两个),它的工作原理:

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

同样的与其他两个领域发生。所以sfValidatorAnd和i18n有些奇怪。你知道会发生什么吗?

谢谢!

回答

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; 

    } 
}