Symfony2 – validation part 1

Featured Image

NOTE: Tested on Symfony2 Beta1. Might not work on later releases!

Few days ago I tried to find validation method for duplicate items in database because I have a table named ‘languages’ and need ‘language_symbol’ field to be unique.

In my entity named Language.php it is written like this:

     * @orm:Column(type="string", length="10", unique=true)
    protected $language_symbol;

I also have a form with field ‘language_symbol’ and need to validate for duplicated entry.
Except I can do it with the AJAX, I didn’t want to and tried to validate with native Symfony2 validation.
Here is how I did this:
I added a validation function inside Entity: ‘Language.php’:

    public function isSymbolDuplicated()
$q = $this->em->createQuery("SELECT count( FROM Surgeworks\AdminBundle\Entity\Language l WHERE (l.language_symbol='" . $this->language_symbol . "' AND <> '".$this->id."')");
        $result = $q->getSingleScalarResult();
        return $result==1?true:false;

Also, I have to put variable to pass entity manager reference to my ‘Language.php’ class so I could make database calls. I know that it was not maybe the best way to implement that, but it was the fastest way to get it work.

 * @orm:Entity(repositoryClass="Surgeworks\AdminBundle\Entity\LanguageRepository")
 * @orm:Table(name="languages")
class Language{
     * @orm:Id
     * @orm:Column(type="integer")
     * @orm:GeneratedValue(strategy="AUTO")
      protected $id;
      protected $em;
    public function setEm($em) {
        $this->em = $em;

After that, in my controller I put this:

     * @extra:Route("/admin/languages/add_new", name="_admin_languages_add")
     * @extra:Template
    public function addAction() {
        $em = $this->get('doctrine.orm.entity_manager');
        $language = new \Surgeworks\AdminBundle\Entity\Language();
	 //Sst reference to entity manager so it can be used in entity class directly for validation purposes

And, here is how my ‘validation.yml’ looks like:

            - NotBlank: { message: "Language name field can not be empty!" }
            #- NotBlank: ~
            - NotBlank: { message: "Language symbol field can not be empty!" }
            - 'False': { message: "Language symbol must be unique.You entered symbol that already exist in database!" }

Just one important note: If you look in Symfony2 documentation about true , false validation Constraints you will see that in above source I put the word true in single quotes. If you do not it will generate error, so I thing that it should be properly written in official Symfony2 documentation page which is not the case now.
That’s it about validation for now. I am sure that I will make more posts about it in the near future.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.