2010-04-29 75 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; 

    } 
} 
相關問題