Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation: notEmpty vs. notBlank #6752

Closed
steefaan opened this issue Jun 7, 2015 · 7 comments

Comments

@steefaan
Copy link
Contributor

commented Jun 7, 2015

I'm a bit confused about the usage of notEmpty and notBlank and the usage of custom error messages. It seems that you can use notEmpty in two different ways:

(1) Not possible to set the error message. You always get the default one.

$validator
    ->add('username', [
        'notEmpty' => [
            'rule' => 'notEmpty',
            'message' => 'My custom message.'
   ]);

(2) Possible to set a custom error message.

$validator
    ->notEmpty('username', 'My custom message');

Since 3.0.2 it's recommended to use notBlank instead of notEmpty. As far as I can see there is only one way for notBlank.

(1) Also not possible to set a custom error message.

$validator
    ->add('username', [
        'notBlank' => [
            'rule' => 'notEmpty',
            'message' => 'My custom message.'
   ]);

Both ways (notEmpty and notBlank) are documented in the docs so know I'm quite unsure which one I should use. I preferred the notBlank because the other one is deprecated but there it wasn't possible to set a custom error message.

There is an issue about consistency at the validation rules, so I will reference it here but is not really related. Refs: #6444

@lorenzo

This comment has been minimized.

Copy link
Member

commented Jun 7, 2015

where is it recommended to use notBlank ?

@lorenzo lorenzo added this to the 3.0.7 milestone Jun 7, 2015
@steefaan

This comment has been minimized.

Copy link
Contributor Author

commented Jun 7, 2015

notEmpty is deprecated so I thought it's better to use notBlank or I'm on the wrong way?

@ADmad

This comment has been minimized.

Copy link
Member

commented Jun 7, 2015

(1) Not possible to set the error message. You always get the default one.

The reason why that happens is stated in #6284. So fix that problem Validation::notEmpty() was deprecated and Validation::notBlank() added in #6285.

@ADmad

This comment has been minimized.

Copy link
Member

commented Jun 7, 2015

So if want to ensure a field is not empty use

$validator->notEmpty('username', 'custom mesage');

and if you want to ensure a field is not empty and also does not contain only whitespace use

$validator
    ->add('username', [
        'notBlank' => [
            'rule' => 'notBlank',
            'message' => 'My custom message.'
   ]);
@ADmad

This comment has been minimized.

Copy link
Member

commented Jun 7, 2015

I preferred the notBlank because the other one is deprecated but there it wasn't possible to set a custom error message.

It is possible. Your usage in the example you provided is incorrect. You need 'rule' => 'notBlank' instead of 'rule' => 'notEmpty'.

@steefaan

This comment has been minimized.

Copy link
Contributor Author

commented Jun 7, 2015

Thanks for your quick response. I understand the difference between them now. They are two different things and that was probably the reason for renaming. So in general I could use both togehter?

$validator
    ->notEmpty('username', 'custom mesage') // check if field is empty
    ->add('username', [
        'notBlank' => [ // check if field contains more than whitespaces
            'rule' => 'notBlank',
            'message' => 'My custom message.'
   ]);
@ADmad

This comment has been minimized.

Copy link
Member

commented Jun 7, 2015

So in general I could use both togehter?

Yes you can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.