Own Validators

Hi Everyone,

Just want to share with you some new information I found out. So everybody knows that symfony provides “validators” – yml-based files which simplifies life for developers.

There are bunch of complete validator classes – e.g. correct email, unique record validation, etc. But what if you need to implement kinda “forgot password” functionality – where you need to check out if account already exists (agree, this is VERY similar to unique validator). Or you need to implement some kind of the other validator which is different from the ones realized in symfony.

I’m going to show you which way you can create your own validator (lets use “forgot password” as an instance for this). So first of all lets add some code into validator yml file (e.g. passwordsent.yml):

required: Yes
required_msg: You must provide an email
validators: emailNonUniqueValidator

class: sfEmailExistValidator
class: User
column: email
unique_error: Account with such email does not exist

So here we just did 2 tricks – defined own validator – emailNonUniqueValidator and related it with a self-written class – sfEmailExistValidator. I put this class into apps/frontend/lib folder and called it sfEmailExistValidator.php.

Common format of this class should be the following:

class sfEmailExistValidator extends sfValidator

public function execute (&$value, &$error) {}

public function initialize ($context, $parameters = null) {}


And of course it’s up to you what exactly will be implemented here. That’s all – your validator is running up.

I hope it was helpfull.
Wish you good coding 😉

Leave a Reply

Your email address will not be published. Required fields are marked *