Bug #33300

Validators should allow empty values

Added by Bastian Waidelich over 9 years ago. Updated almost 8 years ago.

Status:
Resolved
Priority:
Should have
Category:
Validation
Start date:
2012-01-18
Due date:
% Done:

100%

Estimated time:
PHP Version:
Has patch:
No
Complexity:

Description

Currently most of the provided validators do not accept empty values. This makes it impossible to have an optional email address for example (without using the disjunction validator).
If an email is required one should add two validation rules, NotEmpty and EmailAddress to the respective property.


Related issues

Related to TYPO3.Flow - Task #33537: Rename NotEmpty Validator to RequiredClosedBastian Waidelich2012-01-30

Actions
#1

Updated by Gerrit Code Review over 9 years ago

  • Status changed from Accepted to Under Review

Patch set 1 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/8691

#2

Updated by Felix Oertel over 9 years ago

I went into the same problems two weeks ago and tried to come up with a good solution.

I would highly prefer to enable proper disjunction usage over breaking the current behavior. Validating "EmailAddress" in my eyes means "EmailAddress" and not "EmtpyString" (and thus, no "EmailAddress").

Allowing usage of OR (disjunction validator) would solve our problems much better and non-breaking I think.

Ps: I came up with some (EmailAddress OR Empty) syntax, but don't know if this is really the way to go.

#3

Updated by Bastian Waidelich over 9 years ago

Felix Oertel wrote:

I would highly prefer to enable proper disjunction usage over breaking the current behavior.

I thought about this quite a while and I'm pretty sure the new behavior makes much more sense.
E.g. when working with Forms (which is the most common scenario for Validators) you want to differentiate whether a field is required (the label gets an asterisk for example) and/or whether it should be a valid email address if specified.
I agree, that you would probably add NotEmpty as well in most cases, but the behavior is at least consistent. BTW: Some validation engines I looked at handle it the same (only they call it "required" and not "NotEmpty" which is maybe a better choice)

#4

Updated by Bastian Waidelich over 9 years ago

Felix Oertel wrote:

Ps: I came up with some (EmailAddress OR Empty) syntax, but don't know if this is
really the way to go.

What syntax did you come up with by the way?

#5

Updated by Bastian Waidelich over 9 years ago

Bastian Waidelich wrote:

I thought about this quite a while and I'm pretty sure the new behavior makes much more sense.

I (we) thought about it some more and now I'm not so sure anymore ;)
At least in domain models an optional email address is rather an edge case. So if we found a nice way to specify validation rules as logical or this would be a good solution too.

Something like

@FLOW3\ValidateFirst({type="NotEmpty"}, {type="StringLength", options={maximum=100}})

only nicer.. ;)

#6

Updated by Felix Oertel over 9 years ago

Yeah, this is exactly, what I talked about in the first place. ;-)

I did some testing implementation (in extbase of course) and found, that specifying validators in the disjunction validator's options were much too complex and error prone. So I thought that probably a simple but new syntax would be good

@validate $email (NotEmpty OR EmailAddress)

or

@validate $email ((Empty OR StringLength(maximum=100)) OR EmailAddress)

So (for extbase) it would just be
  • look if the first char is "("
  • look for "AND" or "OR"
  • trigger parsing of the seperate validators again and join in con- or disjunction

This would even allow to nest validations as the first char of the first parsed/nested expression can be a "(" again.

As long as it's forbidden to use AND and OR in the same nesting level, it should be fairly simple to implement.

#7

Updated by Gerrit Code Review over 9 years ago

Patch set 2 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/8691

#8

Updated by Gerrit Code Review over 9 years ago

Patch set 3 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/8691

#9

Updated by Gerrit Code Review over 9 years ago

Patch set 4 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/8691

#10

Updated by Bastian Waidelich over 9 years ago

As discussed in todays daily scrum meeting we'll change the validators as suggested in the first changeset (with some refactoring to reduce the amount of code duplication) because it is the expected behavior.
Besides the NotEmpty validator should be renamed to "Required" (see #33537)

#11

Updated by Gerrit Code Review over 9 years ago

Patch set 5 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/8691

#12

Updated by Bastian Waidelich over 9 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
#13

Updated by Simon Schaufelberger about 9 years ago

can this be backported (is this the correct word?) for TYPO3 as well?

Also available in: Atom PDF