2012-01-12 40 views
1

我有更改密碼的一種形式,它會檢查password領域min_lengthrequired。然後它將password字段與confirm字段進行比較。的Symfony驗證處理

如果我張貼到這種形式具有小於所需的最小字符(和密碼,並確認框匹配),它會因最小長度(預計)和密碼不匹配(意外)錯誤。

誰能告訴爲什麼?

<?php 

class ChangeMyPasswordForm extends sfForm { 

    const PASSWORD_MIN_LENGTH_MSG = "<li>We could not update your password because your password must be at least 8 characters long. Please choose a different password.</li>"; 
    const PASSWORD_MISMATCH_MSG = "<li>We could not update your password because your passwords didn't match. Please try again.</li>"; 
    const PASSWORD_REQUIRED_MSG = "<li>We could not update your password because your password was blank. Please enter a password.</li>"; 

    protected static $labels = array(
     'password' => 'Your Password', 
     'confirm' => 'Re-enter Password', 
    ); 

    /** 
    * Called in sfForm's constructor. 
    */ 
    public function configure() 
    { 
     $this->setWidgets(array(
      'password' => new sfWidgetFormInputPassword(array()), 
      'confirm' => new sfWidgetFormInputPassword(array()), 
     )); 

     $this->setValidators(array(
      'password' => $this->_setPasswordValidator(), 
       'confirm' => new sfValidatorPass(), 
     )); 

     $this->mergePostValidator(
      new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 
       'confirm', array(), array(
        'invalid' => self::PASSWORD_MISMATCH_MSG, 
       ) 
      ) 
     ); 

     $this->widgetSchema->setLabels(self::$labels); 
    } 

    /** 
    * Returns validators for the password field. 
    * @return sfValidatorString 
    */ 
    private function _setPasswordValidator() 
    { 
     $v = new sfValidatorString(); 
     $v->setOption('min_length', 8); 
     $v->setMessage('min_length', self::PASSWORD_MIN_LENGTH_MSG); 
     $v->setMessage('required', self::PASSWORD_REQUIRED_MSG); 

     return $v; 
    } 
} 

編輯:這可能有助於=>認爲在那裏我拋出錯誤

<?php if ($form->hasGlobalErrors() || $form->hasErrors()): ?> 
<div class="error"> 
    <p class="bottom"><b>Oops!</b></p> 
    <ul> 
    <?php foreach ($form->getGlobalErrors() as $name => $error): ?> 
     <?php echo $error ?> 
    <?php endforeach; ?> 

     <?php foreach ($form->getErrorSchema()->getErrors() as $error): ?> 
     <?php echo $error; ?> 
    <?php endforeach; ?> 
    </ul> 
</div> 
<? endif; ?> 

回答

0

終於想通了。它看起來像sfValidatorSchemaCompare比較整個小部件,驗證器和所有(不只是他們返回的值)。所以,如果我比較兩個窗口小部件,並且他們不具有相同的驗證程序(即一個MIN_LENGTH = 3,其他的MIN_LENGTH = 6),它就會失敗。

1

驗證過程接受兩個phaces的地方:正常驗證器和後validatos,所述第一預期錯誤來自驗證(每場)的正相。另一個意外的錯誤來自後驗證器。當你將表單與請求值綁定在一起,並且作爲$ form-> isValid()處理所有驗證器(正常和post)時,所以如果這些(或者在這兩種情況下)都有錯誤,它們將被存儲在現場以供稍後顯示。

希望這是非常明顯的

編輯:對不起,我的英語也不怎麼好,也是我的腦子好使,在這裏它的東西非常有用(Symfony Forms in Action - Advanced Validaton)。無論如何,當我第一次讀它時,我沒有注意到「兩個密碼馬克」部分,所以我的答案並不準確。下面是登記表格小窗口配置的一個例子,我有Symfony registration form widget configuration

+0

它有點模糊。 。 。你能多解釋一下,還是指點我一篇文章? – TuK 2012-01-12 16:53:39

+0

希望它更清楚一點! – guiman 2012-01-12 17:05:23

+1

感謝噸吉爾曼! http://www.rankmycode.com.ar/code/symfony-registration-form-widget-configuration是我希望看到的! – TuK 2012-01-12 17:34:13