TheTom
TheTom

Reputation: 1044

symfony 2 formbuilder entity error validation

I build a form with

$form = $this->createFormBuilder($user)
    ->add('locations', 'entity', array('class' =>'PrUserBundle:Location',
           'property' => 'name',
           'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
                 return $er->createQueryBuilder('u')
                            ->where('u.client_id = :client_id')
                            ->setParameter('client_id', $this->clientId)
                            ->orderBy('u.name', 'ASC');
                                },'required' => true, 'multiple'  => true, 'expanded'  => true)
            )

After submitting,I would like to validate the form. This is done by a validation.yml

Pr\UserBundle\Entity\User:
properties:
    usergroups:
        - NotBlank: ~
            message: You must select a location

I only get a error

Unable to parse at line 15 (near " message: You must select a location"). 

EDIT: This has been fixe for removing the ~ tilde like Rooneyl already told me (see comment)

How can I implement the validation of this field inside the form?

EDIT: It's a bit more complicativ I guess.

There are two entities. User and e.g. Location.

The User entity contains

    /**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
protected $client;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
private $client_id;

/**
 * @ORM\Column(type="string", length=16383, nullable=true) //16383 = max varchar utf8
 */
private $imageurl;

/**
 * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $firstname;

/**
 * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $lastname;

    /**
 * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $emailalert;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $phone;

/**
 * @ORM\Column(type="integer", nullable = true)


 */
protected $lock_state;

/**
* @ORM\Column(type="array", nullable=true)

 */
private $locations;

/**
 * @ORM\Column(type="array", nullable=true)

 */
private $usergroups;

/**
 * @ORM\Column(type="string", length=5, options={"fixed" = true, "default" = "de_DE"})
 */
private $locale = 'de_DE';

/**
 * @ORM\Column(type="string", length=32)
 */
private $timezone = 'UTC';

/** * @ORM\Column(type="array", nullable=true)

 */
private $created='1';

For all those fields, I can get a validation. But for the field "locations" I dont get a validation. This will be data from the entity "Location" like you can see below:

    /**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
public $id;


/**
 * @ORM\Column(type="string", length=20)
 */
public $name; //Raumbezeichnung, Ausgabelinie-Bezeichnung, Freitextbezeichnung, Standortname

/**
 * @ORM\Column(type="integer")
 */
public $type;

/**
* @ORM\Column(type="string", length=20)
 */
public $parentLocation;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
public $parentlocation_id;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
public $client;

/**
 * @ORM\Column(type="integer")
 */
public $client_id;

I dont know if I can display this by a many2many connect in the entity, but actually I used to go the way I discribed above (I'm a noob in symfony until now).

I just want to force admins, to setup usergroups for each user at its beeing created within the form.

Upvotes: 2

Views: 266

Answers (1)

Pratik Gadoya
Pratik Gadoya

Reputation: 1513

This will work:

Pr\UserBundle\Entity\User:
properties:
    locations:
        - NotBlank:
            message: { "You must select a location" } 

Upvotes: 1

Related Questions