0

LoginFormというフォームがあります。これはRecipeFormを拡張し、次にZend_Formを拡張します。RecipeFromは私のデコレータのみを返します。

フォームを送信すると、「メッセージ:メソッドaddValidatorが存在しません」というエラーが表示されます。

class Recipe_Form_LoginForm extends Recipe_Form_RecipeForm {
    public function init()
    {
        parent::init();
        $this->setName('loginform')
             ->setAction('/login');


        //  Add Email Element
        $email = $this->addElement('text', 'email', array(
            'label' => 'Email Addresses',
            'required'=> true,
            'size'=>12,
            'filters'=>array('StringTrim'),
            'decorators' => $this->getElementDecorator('email'),

        ));
       $email->addValidator(new Recipe_Validate_EmailAddress(), true, array(
           'messages' => array(
               Recipe_Validate_EmailAddress::INVALID => 
               'Please enter email in correct format',
               Recipe_Validate_EmailAddress::EMAILISEMPTY =>
               'Please enter email address'
       )));
}

class Recipe_Validate_EmailAddress extends Zend_Validate_Abstract
{
    const INVALID           =   'notvalid';
    const EMAILISEMPTY      =   'isempty';

     protected $_messageTemplates = array(
             self::INVALID => "Email is in invalid format",
             self::EMAILISEMPTY => "You have to fill email field"
        );

    public function isValid($value){
        $response = parent::isValid($value);
        if(!$response){
            $this->_message = array(self::INVALID => "Please enter a valid email address");
        }
        return $response;
    }
}
?>
4

1 に答える 1

1

Zend_Form オブジェクト内から呼び出すと$this->addElement()、作成したばかりの要素ではなく、フォーム オブジェクト自体が返されます。

次のいずれかの変更を行うことができます。

$this->addElement('text', 'email', ...);
$email = $this->getElement('email');
$email->addValidator(...);

// or

$email = new Zend_Form_Element_Text('email');
$email->addValidator(...)
      ->setLabel(...)
      ->setRequired(...);
$this->addElement($email);

エラーメッセージを設定するには、$this->_message を設定する代わりにこれを行うべきだと思います。

$this->_error(self::INVALID);

あなたのクラスはメッセージをオーバーライドするために Zend の電子メール バリデータのみを拡張しているように見えるため、このように Zend のメッセージをオーバーライドでき、クラスを拡張する必要はありません。これは私のプロジェクトの 1 つのバリデーターから取得したものなので、余分なものは無視して、EmailAddress バリデーターのメッセージに注意してください。

$this->addElement('text', 'email', array(
        'label' => 'Email Address:',
        'required' => false,
        'filters' => array('StringTrim', 'StringToLower'),
        'validators' => array(
            array('EmailAddress', true, array(
                'messages' => array(
                    Zend_Validate_EmailAddress::INVALID_FORMAT =>
                    "'%value%' is not a valid email address. Example: you@yourdomain.com",
                    Zend_Validate_EmailAddress::INVALID_HOSTNAME =>
                    "'%hostname%' is not a valid hostname for email address '%value%'"
                )
            )),
            array('Db_RecordExists', true, array(
                'table' => 'accounts', 'field' => 'email',
                'messages' => array(
                    Zend_Validate_Db_RecordExists::ERROR_NO_RECORD_FOUND =>
                    "No account with that email address was found"
                )
            ))
        ),
        'decorators' => $this->elementDecorators
    ));
于 2012-01-21T21:02:17.050 に答える