Using sfCryptographp with custom validation

Oleh: Jawad Khan
July 18, 2008

Using sfCryptographp with custom validation..

I just had to use captcha with symfony, Symfony is a great MVC framework, but i really don’t like its default validation method, rather I like javascript validations, so that user don’t have to wait for the server response after missing just a text field. So, I put mostly fields checking validations with javascript and then check at the server for the specific fields which do require server response like checking username if its unique in the database. Such validations are also possible with the quick response using Ajax, but all we need to do is to avoid symfony’s default validation method in order to achieve that.

Now coming back to captcha, the php-captha is a great library for the normal php apps, It can also be used with symfony, but I gave preference to symfony’s already available pluggins..
so here is the pluggin: sfCryptographp
its installation is pretty straight forward.

$ symfony plugin-install http://plugins.symfony-project.com/sfCryptographpPlugin

After your done with installation, enable the crypt pluggin in your settings.yml file

like:

.settings:
enabled_modules:        [default, cryptographp]

Now, Open your template file, add following line:
<?php use_helper(‘Cryptographp’); ?>

Now insert following code in anywhere within the form where you want captcha to be visible.

<?php echo cryptographp_picture(); ?>
<?php echo cryptographp_reload(); ?>

<?php echo input_tag(‘captcha’); ?>

It was pretty easy, now if u test your template you can see captcha images, The above code lines are same as described in sfCryptographp wiki page. Now for the validation we will not follow the wiki, we will put some logic at the actions file. before we do it we need little modification in sfCryptographpValidator class, which can be found at:

plugins\sfCryptographpPlugin\lib\validator\sfCryptographpValidator.class.php

open that class

add another function like:

public function checkCode($value)
{
if (chk_crypt($value)) {
return true;
} else {
return false;
}

}

Now goto your actions class, put the following code at the place where you are handling form post actions.

// create an instance from sfCryptographpValidator class
// and use the new function to validate user entered text

$captcha = new sfCryptographpValidator();

if (!$captcha->checkCode($_POST['captcha']))
{
//user entered wrong code…
}

Tags: , ,

Category: PHP, symfony | RSS 2.0 | Give a Comment | trackback

No Comments

Leave a Reply

*