When a visitor enters their email address into a form, I want to check that it's unique. So I have a simple email form:
class Form_Register extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('register');
$email = new Zend_Form_Element_Text('Email');
$email->setLabel('Your email address:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('EmailAddress')
->addErrorMessage('Please check that email address is correct.');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($email, $submit));
}
}
And then if the request is a post, I check if the email is unique. I'm adding an error message to the element when it's not unique, but it doesn't appear in my view.
if ($this->getRequest()->isPost())
{
$formData = $this->_request->getPost();
// check if email is unique
$isUnique = FALSE;
if ( NULL != $member )
{
$form->Email->addErrors(array('That email is already in use.'))
->markAsError();
}
$form->populate($formData);
$this->view->form = $form;
My view just echos the form:
<?php echo $this->form ?>