Ray Kootstra Ray Kootstra - 5 months ago
162 0

With an assert expression you can add an expression to custom validate your entity field.

PHP

Symfony2 assert expression on entity field

<?php

namespace MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Profile
 */
class Profile
{

    /**
     * @var string
     *
     * @ORM\Column(name="street", type="text", nullable=true)
     * @Assert\Type(type="string")
     * @Assert\Expression(
     *     "this.checkAddress()",
     *     message="You didnt enter a streetname"
     * )
     */
    protected $street;

	/**
	 * @return string
	 */
	public function getStreet()
	{
		return $this->street;
	}

	/**
	 * @param $street
	 *
	 * @return $this
	 */
	public function setStreet($street)
	{
		$this->street = $street;
		
		return $this;
	}

	/**
     * Check related news articles languages
     *
     * @return bool $result
     */
    public function checkAddress()
    {
        if($this->street === null) {
			return false
		}

        return true;
    }
}