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

  • Home
  • SEARCH
  • 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 7800809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:45:21+00:00 2026-06-02T00:45:21+00:00

I’m having trouble validating a value to allow NULL but not an empty string

  • 0

I’m having trouble validating a value to allow NULL but not an empty string with the Symfony2 validator component.

I’ve integrated the component in a Silex application and used the Property Constraint target to validate some properties of my Application Entities (not a Doctrine Entity).

I’ve added this static method to my Entity class to validate name and service_id on my Entity, problem is that when service_id is NULL which should be valid the NotBlank constraint kicks in and reports a violation.

static public function loadValidatorMetadata(ClassMetadata $metadata)
{
    // name should never be NULL or a blank string
    $metadata->addPropertyConstraint('name', new Assert\NotNull());
    $metadata->addPropertyConstraint('name', new Assert\NotBlank());

    // service_id should either be a non-blank string or NULL
    $metadata->addPropertyConstraint('service_id', new Assert\NotBlank());
}

Bottomline, I’m looking how to allow either a String or NULL as service_id but not allow an empty string.

PS: I’ve also tried the MinLength(1) constraint but that allows empty strings unfortunately.

  • 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-02T00:45:23+00:00Added an answer on June 2, 2026 at 12:45 am

    The NotBlank constraint treats null as a blank value, as can be seen in this test.

    When using doctrine, this can be solved by using the Valid constraint. If the value of the field is not null, it will attempt to validate it.

    Since you are not using doctrine entities, you’ll probably have to use a callback validator or write your own constraint.

    EDIT

    To answer your new question on adding a callback constraint as a property constraint: No, it is not possible to do that.

    The callback constraint acts on the whole object, not just a single property. Here’s an example of how you can use the callback constraint:

    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\Validator\Mapping\ClassMetadata;
    use Symfony\Component\Validator\ExecutionContext;
    
    $app = new Silex\Application();
    
    $app->register(new Silex\Provider\ValidatorServiceProvider());
    
    class Person
    {
        public $name;
    
        public function validateName(ExecutionContext $context)
        {
            if ('John Doe' === $this->name) {
                $context->addViolationAtPath('name', 'Name must not be "John Doe"');
            }
        }
    
        static public function loadValidatorMetadata(ClassMetadata $metadata)
        {
            $metadata->addConstraint(new Assert\Callback(array('validateName')));
            $metadata->addPropertyConstraint('name', new Assert\NotNull());
        }
    }
    
    $person = new Person();
    $person->name = 'John Doe';
    $violations = $app['validator']->validate($person);
    var_dump('Violations for John Doe');
    var_dump((string) $violations);
    
    $person = new Person();
    $violations = $app['validator']->validate($person);
    var_dump('Violations for Person with name null');
    var_dump((string) $violations);
    
    $person = new Person();
    $person->name = 'Igor Wiedler';
    $violations = $app['validator']->validate($person);
    var_dump('Violations for Igor Wiedler');
    var_dump((string) $violations);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.