Below is a code snippet if you want a simple sign-up form on you website using Zend_Form.
Features are
- set email adres to lowercase using
Zend_Filter_StringToLower. - Use
Zend_Validate_Emailaddress, to check the correctness of the email address. - Check password for
StringLenght; minimal 6 & maximum 20 characters. - Custom error messages for
Zend_Validate_StringLenght. - Check for identical password entered using
Zend_Validate_Identical, and custom error message. - Decorators to display the error messages.
Zend_Filter_StringTrimto trim possible whitespace.
<?php
class Login_Form extends Zend_Form
{
public function init()
{
$username = $this->addElement('text', 'email', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'EmailAddress',
),
'required' => true,
'label' => 'E-mail address',
));
$password = $this->addElement('password', 'password', array(
'filters' => array('StringTrim'),
'validators' => array( array(
'StringLength', false, array(6,20,'messages' => array(
Zend_Validate_StringLength::TOO_SHORT => 'Your password is too short.',
Zend_Validate_StringLength::TOO_LONG => 'Your password is too long.',
)))),
'required' => true,
'label' => 'Your password'
));
$password2 = $this->addElement('password', 'password2', array(
'filters' => array('StringTrim'),
'validators' => array('Identical'),
'required' => true,
'label' => 'Retype your password',
));
$login = $this->addElement('submit', 'Sign up', array(
'required' => false,
'ignore' => true,
'label' => 'Register',
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
array('Description', array('placement' => 'prepend')),
'Form'
));
}
public function isValid ($data)
{
$passTwice = $this->getElement('password2');
$passTwice->getValidator('Identical')->setToken($data['password'])->setMessage('Passwords don\'t match.');
return parent::isValid($data);
}
}
Enjoy.
Tags: php, zend framework