2010-03-05 18 views
5

मैं पुस्तकालय के लिए इस वर्ग को जोड़ने/मेरी/मान्य/PasswordConfirmation.phpपासवर्ड पुष्टिकरण

<?php 
require_once 'Zend/Validate/Abstract.php'; 
class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract 
{ 
    const NOT_MATCH = 'notMatch'; 

    protected $_messageTemplates = array(
     self::NOT_MATCH => 'Password confirmation does not match' 
    ); 

    public function isValid($value, $context = null) 
    { 
     $value = (string) $value; 
     $this->_setValue($value); 

     if (is_array($context)) { 
      if (isset($context['password']) 
       && ($value == $context['password'])) 
      { 
       return true; 
      } 
     } elseif (is_string($context) && ($value == $context)) { 
      return true; 
     } 

     $this->_error(self::NOT_MATCH); 
     return false; 
    } 
} 
?> 

तो मैं इस तरह मेरे रूप में दो क्षेत्र बनाने के लिए:

 $userPassword = $this->createElement('password', 'user_password'); 
    $userPassword->setLabel('Password: '); 
    $userPassword->setRequired('true'); 
    $this->addElement($userPassword); 

    //create the form elements user_password repeat 
    $userPasswordRepeat = $this->createElement('password', 'password_confirm'); 
    $userPasswordRepeat->setLabel('Password repeat: '); 
    $userPasswordRepeat->setRequired('true'); 
    $userPasswordRepeat->addPrefixPath('My_Validate','My/Validate','validate'); 

    $userPasswordRepeat->addValidator('PasswordConfirmation'); 
    $this->addElement($userPasswordRepeat) 

सब कुछ अच्छा है लेकिन जब मैं हमेशा फॉर्म जमा करता हूं तो मुझे 'पासवर्ड पुष्टिकरण मेल नहीं खाता' संदेश मिलता है? क्या मेरी कोड

+0

यहाँ Zend के प्रमाणकों का उपयोग कर पासवर्ड सत्यापन करने के लिए (मुझे लगता है कि पता ही एक रास्ता है यह एन हो सकता है जब यह सवाल पूछा गया था तब संभव हो सकता है): http://stackoverflow.com/questions/347856/zend-form-how-to-check-2-fields-are-identical/3782388#3782388 –

उत्तर

2

में गलत है मुझे लगता है कि आप $context['user_password'] कि के रूप में चाहते हो सकता है आपके "पहली" पासवर्ड तत्व

2

सत्यापनकर्ता पुन: प्रयोज्य बनाने का नाम है। वैधकर्ता में हार्ड कोड फ़ील्ड नाम न करें। इस IdenticalField Validator को देखें जो अधिक सार्वभौमिक है।

3

ऐसा करने के लिए एक bettter तरीका है। अपने रूप में पुष्टि passoword मैदान पर समान सत्यापनकर्ता डाल दिया, और फिर बस $ फार्म के ऊपर लिख> अगर है() विधि मान सेट करने मान्य करने की:

public function __construct($options = NULL) 
{ 
    // ... 
    $confirm->addValidator('Identical'); 
    // ... 
} 
public function isValid($data) 
{ 
    $confirm = $this->getElement('confirm_password'); 
    $confirm->getValidator('Identical')->setToken($data['password']); 
    return parent::isValid($data); 
} 
9

एक कम सुरुचिपूर्ण और सरल यह करने के लिए जिस तरह से:

$password = new Zend_Form_Element_Password('password'); 
    $password->setLabel('Password:') 
      ->addValidator('StringLength', false, array(6,24)) 
      ->setLabel('Choose your password:') 
      ->setRequired(true); 

    $password2 = new Zend_Form_Element_Password('password-confirm'); 
    $password2->setLabel('Confirm:') 
      ->addValidator('StringLength', false, array(6,24)) 
      ->setLabel('Confirm your password:') 
      ->addValidator(new Zend_Validate_Identical($_POST['password'])) 
      ->setRequired(true); 
+0

धन्यवाद उपयोगी आपके लिए मार्सेल और लघु रीप्ले – 3ehrang

+0

और यदि आपके पास partialloop में कुछ अनुभव है तो कृपया मुझे सूचित करें। – 3ehrang

17

आप Zend_Form- ओवरराइड करने के लिए> अगर है विधि या superglobal $ _POST का उपयोग की जरूरत नहीं है, इस जाँच:

$frmPassword1=new Zend_Form_Element_Password('password'); 
$frmPassword1->setLabel('Password') 
    ->setRequired('true') 
    ->addFilter(new Zend_Filter_StringTrim()) 
    ->addValidator(new Zend_Validate_NotEmpty()); 

$frmPassword2=new Zend_Form_Element_Password('confirm_password'); 
$frmPassword2->setLabel('Confirm password') 
    ->setRequired('true') 
    ->addFilter(new Zend_Filter_StringTrim()) 
    ->addValidator(new Zend_Validate_Identical('password')); 
+0

मैंने zend_form को ओवरराइड नहीं किया है वैध विधि है, मैं बस अपना खुद का सत्यापनकर्ता जोड़ता हूं। – 3ehrang

संबंधित मुद्दे