Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:43:43+00:00 2026-06-07T11:43:43+00:00

In my Doctrine entity, which is data_class for my form I have a file

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:43:44+00:00Added an answer on June 7, 2026 at 11:43 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have something like this in doctrine: class Entity {
I have a service in which I inject the entity manager service (@doctrine.orm.entity_manager) because
I have an entity ArticlePattern , which has a property pattern (string). I need
I have a Doctrine Entity (News) which I listen for the event prePersist .
I have an Entity that looks like this: class Privilege { /** * @Id
Using Doctrine 2. I have an entity called 'size' and I'm trying to form
I have an Entity class that looks like this: <?php namespace Entities; /** @Entity
I have an entity Movie which has a unique constraint through doctrine annotation. Based
I have a Doctrine entity Page which is loaded by module Foo . The
I have a number of simple controller classes that use Doctrine's entity manager to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.