queyring database for existing username with Zend and doctrine

I am querying database records to check if a username already exists this way:

$q = Doctrine_Query::create()
        ->from('Tcc_Model_User u')
        ->where('u.Username = ?', $input['Username']);

 $object = $q->fetchOne();

  if(is_object($object)) {

  // not sure what to do here

 } else {

 // ok to record

What I cannot make it work is how to show a message that username is already taken next to the Username field of the submitted form. Could please anyone help me? Really appreciated any help. Thank you

EDITED:

Thanks to Drew010 I solved the above problem by adding:

 $form->getElement('Username')->addError('Sorry, that username is already taken!');

in the first of the conditional statement. Thank again drew010.

Answers


Since you are using Zend_Form, there is a validator that can do this for you.

See Zend_Validate_DbNoRecordExists

They give an example for validating that a given username does not exist in the database.

Here is an example from one of my applications, the third validator checks that the username does not already exist:

    $this->addElement('text', 'username', array(
        'label' => 'User Name:',
        'required' => true,
        'validators' => array('NotEmpty'),
        'decorators' => $this->elementDecorators,
        'validators' => array(
            array('StringLength', true, array('min' => 4,
                'messages' => array(
                    Zend_Validate_StringLength::TOO_SHORT =>
                    'Account username must be at least %min% characters'
                )
            )),
            array('Regex', true, array('pattern' => '/^\w(?:[\w\d\.\-_]+)(?:\w|\d)$/',
                'messages' => array(
                    Zend_Validate_Regex::NOT_MATCH =>
                    "The username contained invalid characters"
                )
            )),
            array('Db_NoRecordExists', true, array(
                'table' => 'accounts', 'field' => 'username',
                'messages' => array(
                    Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND =>
                    "An account with the username '%value%' is already registered"
                )
            ))
        )
    ));

Also, for future reference, if you do extended validation you can't do with a validator, you can add an error to a specific form element like this:

if ($something_was_wrong) {
    $form->getElement('username')
         ->addError('Username already exists!');
}

Need Your Help

Image-alignment error in Wordpress

css image wordpress

I'm developing a theme for Wordpress.

Set linux permissions allow writing to existing files, but not creation of new files

linux file-permissions chmod

Is there a recursive chmod command that would allow editing of existing files, but not creation of new files or directories? I have a hosting user who has a website that keeps getting hacked to ho...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.