13

In my Doctrine entity, which is data_class for my form I have a file property defined like this:

/**
     * Image.
     *
     * @Assert\NotBlank
     * @Assert\File
     * @Assert\Image(minWidth="138", minHeight="96")
     */
    protected $file;

Also, added it to form type with ->add('file', 'file')...

Creating entity works perfect, but the problem is when I use form to update this entity. It asks for file again, since it has @Assert\NotBlank. Since I have other fields in this form, I don't want to reupload image on every update.

When I remove @Assert\NotBlank, everithing works fine, but I want this file field to be mandatory.

Any idea?

umpirsky
  • 9,902
  • 13
  • 71
  • 96

3 Answers3

7

You have two ways out this situation and both rely on Callback validators: (Symfony callback)

Either add boolean named isUpdate to you entity which will not be persisted and will tell validator which operation was attempted. This method is completely described in link above.

Another way to tackle this is to add Callback validator to your Form type directly. Again, some isUpdate flag will be needed but this time within Form type (pass it 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 to specify image file.'));                  
        }
    }));
}

Maybe there is simplier way to achieve desired validation but I came upon these two few months back.

Hope this helps...

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • 1
    Thanks, that gave me ideas, definatelly :) I don't understand why you need another isUpdate boolean, and how is that values set? I checked `if (null !== $this->getId())`. You can check my gist https://gist.github.com/3066507. It was even fancyer in Symfony 2.0 when you can pass in context to validator https://gist.github.com/3066488. Thanks again. – umpirsky Jul 07 '12 at 13:42
  • Oh, good point. By comparing entity id with null you could definitely say whether operation is "add" or "update". And the second gist is the perfect solution I guess :) – Jovan Perovic Jul 07 '12 at 13:53
  • Yeah, too bad they closed ExecutionContext and it is not possible to set property path from outside the class any more https://github.com/symfony/symfony/blob/ac6939441fb61a17184f536391387aca78b10013/src/Symfony/Component/Validator/ExecutionContext.php – umpirsky Jul 07 '12 at 14:11
7

You can also use Validation Groups for that. One Validation Group (perhaps default one) will be for create and second for update.

Karol Kasprzak
  • 591
  • 6
  • 4
  • I know this is really old but could you elaborate a little ? I'm struggling to understand the documentation about that, it's not really noob-friendly. If you can make it v3-compliant it would be really nice to – Dan Chaltiel Feb 09 '17 at 11:45
0

I have was in similar situation. I try to edit existing record in database with path to the file. When i edit record i must upload new file, what is not comfortable for users. In my solution i use variable tmp file for file hash and variable file name. All needed operation i made in Action edit class.

Full example action class in bellow link

https://github.com/marekz/php_examples/wiki/Symfony-how-to-edit-attachment-form