validation - Non blank file input field in Symfony2 form -
in doctrine entity, data_class
form have file property defined this:
/** * image. * * @assert\notblank * @assert\file * @assert\image(minwidth="138", minheight="96") */ protected $file;
also, added form type ->add('file', 'file')
...
creating entity works perfect, problem when use form update entity. asks file again, since has @assert\notblank. since have other fields in form, don't want reupload image on every update.
when remove @assert\notblank, everithing works fine, want file field mandatory.
any idea?
you have 2 ways out situation , both rely on callback
validators: (symfony callback)
either add boolean
named isupdate
entity not persisted , tell validator operation attempted. method described in link above.
another way tackle add callback validator form type directly. again, isupdate
flag needed time within form type (pass via constructor):
if ( $this->isupdate == false ){ $builder->addvalidator(new callbackvalidator(function(forminterface $form){ if ( $form['image_file']->getdata() == null ){ $form->adderror(new formerror('you need specify image file.')); } })); }
maybe there simplier way achieve desired validation came upon these 2 few months back.
hope helps...
Comments
Post a Comment