Search code examples
zend-frameworkuser-registration

Zend Framework - Registration Form, Elegant way to check for duplicate usernames?


Does Zend Framework provide an elegant way to check if the username already exist in the database?

Or is my only option to code a validator using a combination of php if/else and mysql select statements?


Solution

  • You can add a validator which will check in database if username already exist, if so it will notice the user. I am assuming you are using Zend_Form and Zend_Db_Table with at least one default db table database.

    For example:

       $element = new Zend_Form_Element_Text('username');
        $element->setLabel('User:')
                ->addValidator(new Zend_Validate_Db_NoRecordExists('user', 'username'))
                ->setRequired(true);
        $this->addElement($element);
    

    You will pass 2 parameters to validator, first is table name and second is the column you want to check.

    That's it!