2010-08-17 84 views
1

使用Symfony 1.4的表單,如何在嵌入式表單的驗證器中拋出sfValidatorError?如何在嵌入式表單的驗證器(sfForm)中拋出錯誤

我父窗體調用以下:

public function configure(){ 
    $this->embedForm('page', $pageLinkForm); 
} 

我的嵌入形式:

public function configure(){ 
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array(
     'callback' => array($this, 'validateLink') 
    ))); 
} 

public function validateLink($validator, $values) { 
    if (!empty($values['link']) && !empty($values['outside_link'])) 
     throw new sfValidatorError($validator, 'Only specify either an internal link or an external link, but not both.'); 

} 

的後驗證運行validateLink會拋出sfValidatorError但它並不顯示爲一個全球性的錯誤,並且形式isValid(),但它不應該是。

爲什麼忽略錯誤?我怎樣才能讓它不被忽略?

+0

嘗試debbuging這個文件:http://trac.symfony-project.org/browser/branches/1.4/lib/validator/sfValidatorSchema.class.php(如果可以的話,用XDebug一步一步調試,否則用var_dump())來檢查: - 錯誤sfValidatorError is caugh 驗證 - 由此產生的errorSchema被引發199行 - 它在父窗體上的第159行再次被捕獲 並且請發佈您父窗體的validatorSchema的var_dump() – greg0ire 2010-08-17 19:31:36

回答

0
在sf1.1

我不喜歡這樣寫道:

public function bind(array $taintedValues = null, array $taintedFiles = null) 
{ 
    sfLoader::loadHelpers(array('I18N')); 
    parent::bind($taintedValues, $taintedFiles); 
    if($taintedValues["password"]) 
    { 
    if(!$taintedValues["pwd_verify"]) 
    { 
     $this->getErrorSchema()->addError(new sfValidatorError(new sfValidatorSchema(), __('Please reenter the new password.')), 'password'); 
    } 
    } 
} 

我希望它可以幫助你。

2

恕我直言,這是更好地拋出一個sfValidatorSchemaError,像這樣:

$error = new sfValidatorError($validator, 'invalid', array('value' => $field_name)); 
throw new sfValidatorErrorSchema($validator, array($field_name => $error)); 

如果你想拋出一個嵌入表單中的錯誤,只是嵌入sfValidatorSchemaError:

//define container 
$errorSchema = new sfValidatorErrorSchema($validator); 

//embedded field error 
$error = new sfValidatorError($validator, 'invalid', array('value' => $field_name)); 
$errorSchema->addError($error, $field_name); 

//associate $errorSchema to your embedded field 
throw new sfValidatorErrorSchema($validator, array('page' => $errorSchema));