2011-01-19 42 views
0

出於某種原因,我的表單中的字段未被驗證。也許有人可以指出我的代碼中的任何錯誤或缺失的項目。我試圖按照這裏的例子:http://www.symfony-project.org/cookbook/1_2/en/conditional-validator如何在symfony 1.4中正確設置條件驗證器?

任何幫助表示讚賞!

我有以下窗體類:

<?php 

/** 
* dnc_btns form. 
* 
* @package NMPlus 
* @subpackage form 
* @author  Your name here 
* @version SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ 
*/ 
class dnc_btnsForm extends Basednc_btnsForm 
{ 
    public function configure() 
    { 
    $this->useFields(array('btn', 'btn_low', 'btn_high', 'company', 'source')); 


    $this->setValidator('company', new sfValidatorPass()); 
    $this->setValidator('source', new sfValidatorPass()); 
    $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(array('callback' => array($this, 'checkBtnType'))) 
     ); 

    } 

    public function checkBtnType($validator, $values) 
     { 
       if (!empty($values['btn'])) 
        { 
         $this->setValidator('btn', new sfValidatorBtn()); 
         $this->setValidator('btn_low', new sfValidatorPass()); 
         $this->setValidator('btn_high', new sfValidatorPass()); 
        } 

       if(empty($values['btn'])) 
        { 
         $this->setValidator('btn_low', new sfValidatorBtn()); 
         $this->setValidator('btn_high', new sfValidatorBtn()); 
         $this->mergePostValidator(
            new sfValidatorSchemaCompare('btn_low', sfValidatorSchemaCompare::LESS_THAN, 'btn_high', array(), 
            array('invalid' => 'The first BTN must be lower than the second.'))); 

        } 

        return $values; 
     } 

} 

這裏是我的自定義的驗證:

<?php 

class sfValidatorBtn extends sfValidatorAnd 
{ 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->setValidators(); 
    } 

    public function setValidators() 
    { 


     //Btn should be required, not trimmed, and min and max length set. 
     $this->addValidator(
       new sfValidatorString(
         array(
          'required' => true, 
          'trim' => false, 
          'min_length' => 10, 
          'max_length' => 10, 
          ), 
         array(
          'invalid' => 'BTN must be 10 digits.' 
          ) 
         ) 
       ); 

     // Adapted from http://stackoverflow.com/questions/1964399/validation-for-a-10-digit-phone-number 
     $this->addValidator(
       new sfValidatorRegex(
         array('pattern' => '/^([1-9]\d*)/'), 
         array('invalid' => 'BTN may not start with a 0.') 
         ) 
       ); 

     //Checking for existance of given btn in database 
     $this->addValidator(
       new sfValidatorDoctrineUnique(
         array('model' => 'dnc_btns', 'column' => 'btn'), 
         array('invalid' => 'This BTN is already in the database.') 
         ) 
       ); 
    } 
} 

回答

1

的checkBtnType()方法,而應執行驗證,然後添加新的驗證。

如果一切正常,你的方法應該返回值,否則拋出一個sfValidatorError異常。

您似乎沒有按照您粘貼的鏈接進行操作。

+0

我以本教程爲起點。我偏離了它,以適應我的目的。如果checkBtnType()方法應該執行驗證,那麼讓代碼的那部分位於自定義驗證器中會更好嗎?還有一種方法來實現現有的驗證器,所以我沒有重寫它們?感謝您的反饋。 – 2011-01-19 15:40:02

0

我沒有測試過(也許我是錯的),BU我認爲你可以在checkBtnType通過只是創造驗證執行全手動驗證對象:

$validator = new sfValidatorBtn(); 
$validator->doClean($values['btn']); 

希望有所幫助。

相關問題