views:

116

answers:

2

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 ?>
A: 

My solution to this problem was to use Zend_Form_Decorator_Callback.

Justin
A: 

I think your IF statement is wrong. Make sure that your code is going inside IF loop.

Try something like this:

if ( NULL != $member )
{
    echo "inside IF loop";         
} else {
    echo "oops missed the loop"; 
}
Sam