![]() |
|
Snippets |
|
using AJAX to check username duplication. there's another submit button on form, should not use submit whole page to check username availability.
in template:
<DIV id='usercheck'> </DIV>
<?php
echo submit_to_remote('ajax_submit', 'Check Username', array(
'update' => 'usercheck',
'url' => '/user/checkusername',
));
in action class, check dupliation:
public function executeCheckusername(){ $username=$this->getRequestParameter('obj_username'); $c = new Criteria(); $c->add(UserPeer::USERNAME, $username); $user = UserPeer::doSelectOne($c); if ($user) { // username already exists $this->setFlash('username_exists', 'y'); return sfView::SUCCESS; } else { $this->setFlash('username_exists', 'n'); return sfView::SUCCESS; } }
in checkusernameSuccess, display error message:
<?php if($sf_flash->get('username_exists') == 'y'){ ?> <FONT COLOR="RED">Username Not Available</FONT> <?php } else { ?> <FONT COLOR="GREEN">Username Available</FONT> <?php } ?>
Comments on this snippet
Thank you I was looking for something like this
I'm sorry to tell you, but for things like that there's already the sfPropel/DoctrineUniqueValidator which you should use in your <form>.yml in validators/.
Pierre, correct me if I am wrong please but to use the validator would require the form to be submited to the server. This code snippet allows you to now submit the whole form and just have an AJAX action check the name against the database prior to submting the whole form.
Thanks..