Skip to content

[3.0] Constraints should not ignore empty strings #11956

@webmozart

Description

@webmozart

As has been brought up multiple times (e.g. #11943, #11083, #7412), it's confusing that the string validation constraints ignore empty strings. Should we change this behavior with Symfony 3.0?

  • Remove Blank/NotBlank
  • Remove if ('' === $value) { return; } from all constraint validators

A consequence would be that for manually validating form submissions, one will have to pre-process the $_POST array before the validation and convert empty strings to null manually:

$formValues = array_replace_recursive($defaultValues, $_POST['form']);
array_walk_recursive($formValues, function (&$value) { if ('' === $value) { $value = null; } });

$validator->validate($formValues, new Collection(array(
    'name' => array(
        new NotNull(),
        new Length(array('min' => 3)),
    ),
    // ...
));

See also the related discussion about separating the type validation from constraint validators in #10221.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)Validator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions