Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

How to validate notEmpty bit value 0/1 #154

Open
NeedToMeet opened this Issue · 3 comments

4 participants

@NeedToMeet

I noticed that the NotEmpty rule uses PHP's empty(), which always evaluates the value of numeric 0 as false. Values of non-nullable 0 or 1 are common in bit-type fields where an on/off value is set. Any suggestions on how to validate this as non-empty?

@augustohp
Owner

I am afraid you are right about the notEmpty rule, which exists to conform with the empty() function.

Although there is no specific rule to do what you want, there a couple of different ways that involve creating a rule composed of more rules:

use Respect\Validation\Validator as v; // as of version 0.5.0

$onOffValidator = v::oneOf(array(
    v::int()->equals(0)->setName('Off'),
    v::int()->equals(1)->setName('On')
))->setName('On/Off');

$onOffValidator = v::int()->in(array(0,1))->setName('On/Off');

$onOffValidator = v::base(2)->length(1,1,true)->setName('On/Off');

Please, let me know if that helps you and if you feel the provided solutions are appropriate for your use cases.

@augustohp augustohp self-assigned this
@flip111 flip111 referenced this issue from a commit in flip111/Validation
@flip111 flip111 notEmpty fix
For strings only: notEmpty now means "there was content supplied", rather than "the content was not empty according to the php empty function".

Effect: notEmpty no longer validates to false when string "0" is supplied.

This make more sense when validating HTML forms.

Related issue: Respect#154
19d882c
@flip111 flip111 referenced this issue
Open

notEmpty fix #173

0 of 4 tasks complete
@maboiteaspam

@augustohp

Hi,
Is that possible to have a not empty string validator ? Just that, consider value is a string, evaluate it as not empty. That will make things much more simple for everyone and you would keep going with your semantic matter safely, right ?

@alganet alganet referenced this issue from a commit in alganet/Validation
@alganet alganet Fixes #244, #154, #196 and #173
  - Moves code from  __invoke on AbstractRule into NotEmpty.
  - Moves foreign type check to the chain.
  - Fixes tests using the chain instead of concrete instances.
  - Adds a parameter for juggling types on NotEmpty. (no BC break)
  - Adds new tests for the BC behavior and new parameter.
c3d2cb7
@alganet alganet referenced this issue from a commit in alganet/Validation
@alganet alganet Fixes #244, #154, #196 and #173
  - Moves code from  __invoke on AbstractRule into NotEmpty.
  - Moves foreign type check to the chain.
  - Fixes tests using the chain instead of concrete instances.
  - Adds a parameter for juggling types on NotEmpty. (no BC break)
  - Adds new tests for the BC behavior and new parameter.
43fb8d1
@alganet alganet referenced this issue from a commit in alganet/Validation
@alganet alganet Fixes #244, #154, #196 and #173
  - Moves code from  __invoke on AbstractRule into NotEmpty.
  - Moves foreign type check to the chain.
  - Fixes tests using the chain instead of concrete instances.
  - Adds a parameter for juggling types on NotEmpty. (no BC break)
  - Adds new tests for the BC behavior and new parameter.
50a82fc
@henriquemoody henriquemoody referenced this issue from a commit
@alganet alganet Fixes #244, #154, #196 and #173
  - Moves code from  __invoke on AbstractRule into NotEmpty.
  - Moves foreign type check to the chain.
  - Fixes tests using the chain instead of concrete instances.
  - Adds a parameter for juggling types on NotEmpty. (no BC break)
  - Adds new tests for the BC behavior and new parameter.
558fd61
@henriquemoody henriquemoody referenced this issue from a commit
@alganet alganet Fixes #244, #154, #196 and #173
- Moves code from  __invoke on AbstractRule into NotEmpty.
- Moves foreign type check to the chain.
- Fixes tests using the chain instead of concrete instances.
- Adds a parameter for juggling types on NotEmpty. (no BC break)
- Adds new tests for the BC behavior and new parameter.
- Fixes #235, changes NotEmpty parameter name
fb864b6
@henriquemoody henriquemoody referenced this issue from a commit
@alganet alganet Fixes #244, #154, #196 and #173
- Moves code from  __invoke on AbstractRule into NotEmpty.
- Moves foreign type check to the chain.
- Fixes tests using the chain instead of concrete instances.
- Adds a parameter for juggling types on NotEmpty. (no BC break)
- Adds new tests for the BC behavior and new parameter.
- Fixes #235, changes NotEmpty parameter name
53e6286
@henriquemoody henriquemoody referenced this issue from a commit
@alganet alganet Fixes #244, #154, #196 and #173
  - Moves code from  __invoke on AbstractRule into NotEmpty.
  - Moves foreign type check to the chain.
  - Fixes tests using the chain instead of concrete instances.
  - Adds a parameter for juggling types on NotEmpty. (no BC break)
  - Adds new tests for the BC behavior and new parameter.
2c3d675
@dereuromark

You could have a method boolNotEmpty() which would allow 0 and 1 to pass as not empty, whereas "null" or empty string would be empty then. Just an idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.