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.
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:
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: