forms - Constraint NotBlank/NotNull on Entity field Symfony2 -


i have user entity. every user can belong 1 domain.

class user extends baseuser implements domainableinterface {     …      /**     * @var domain     *     * @orm\manytoone(targetentity="domain", inversedby="users")     * @orm\joincolumns({     *   @orm\joincolumn(name="domain_id", referencedcolumnname="id")     * })     */     private $domain;      … } 

when create user, want domain mandatory. here form (it's super class because many other entities belong domain), user form inherits one

abstract class hasdomainformtype extends abstracttype {     private $user;      public function __construct($securitycontext)     {         $this->user = $securitycontext->gettoken()->getuser();     }      public function buildform(formbuilderinterface $builder, array $options)     {         $user = $this->user;         $builder             ->add('domain', 'entity', array(                     'class'    => 'appbundle:domain',                     'property' => 'id',                     'multiple' => false,                     'query_builder' => function(domainrepository $er) use ($user) {                         if (!in_array('role_super_admin', $user->getroles())) {                             return $er->createquerybuilder('d')                                     ->andwhere('d.id = :domainid')                                     ->setparameter('domainid', $user->getdomain()->getid());                         } else {                             return $er->createquerybuilder('d');                         }                     },                 )             )         ;     } } 

and here validation.xml file

    <class name="appbundle\entity\user">       <property name="domain">           <constraint name="notnull">               <option name="message">fos_user.domain.blank</option>               <option name="groups">                   <value>update</value>               </option>           </constraint>         </property>     </class> 

when submit form omitting domain field (it's rest api), error :

an exception occurred while executing 'select b0_.id id0, b0_.name name1, b0_.enabled enabled2 bo_domain b0_ b0_.id in (?)' params [""]: sqlstate[22p02]: invalid text representation: 7 error:  invalid input syntax integer: "" 

but works fine if fill in domain field.

i don't understand why. form tell me field "domain" can not null.

edit : error occurs while

$form->handlerequest($request); 

not @ the

$form-isvalid(); 

i couldn't make work, made workaround.

abstract class hasdomainformtype extends abstracttype {     private $user;     private $domainmanager;      public function __construct($securitycontext, $domainmanager)     {         $this->user = $securitycontext->gettoken()->getuser();         $this->domainmanager = $domainmanager;     }      public function buildform(formbuilderinterface $builder, array $options)     {         $user = $this->user;          $arrchoice = $this->domainmanager->getdomainforuser($user);         $builder             ->add('domain', 'entity', array(                     'class'    => 'appbundle:domain',                     'property' => 'id',                     'multiple' => false,                     'choices'  => $arrchoice                 )             )         ;     } } 

instead of using query_builder option, fetch choices need domainmanager before building form, , use them in choices options.

thanks @jovan perovic, think know problem lies, though haven't tested. i'm using form create user, here line in controller

$form = $this->createform(new userformtype(), $user); 

my guess $user variable in parameters override $user variable use in formtype in query_builder option. since it's new user, not have domain.

if time, i'll try rename $user in form $currentuser, , see if works.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -