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

//Surgeworks/AdminBundle/Entity/Language.php
//..
    public function isSymbolDuplicated()
    {
$q = $this->em->createQuery("SELECT count(l.id) FROM Surgeworks\AdminBundle\Entity\Language l WHERE (l.language_symbol='" . $this->language_symbol . "' AND l.id <> '".$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
        $language->setEm($em);

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

Surgeworks\AdminBundle\Entity\Language:
    properties:
        language_name:
            - NotBlank: { message: "Language name field can not be empty!" }
            #- NotBlank: ~
        language_symbol:
            - NotBlank: { message: "Language symbol field can not be empty!" }
    getters:
        SymbolDuplicated:
            - '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.

0
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Darko Goles

Magento backend developer

Other posts from this author

Discussion 0 Comments

Add Comment

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top