Skip to content

Array validation with non iterative rules #753

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

Closed
wants to merge 1 commit into from
Closed

Array validation with non iterative rules #753

wants to merge 1 commit into from

Conversation

KaneCohen
Copy link
Contributor

Ok, one more PR related to Validator and its ability to validate arrays.

How it works.

We have several additional methods in Factory and Validator:

Factory:

  • setNonIterative()
  • addNonIterative()
  • removeNonIterative()
  • getNonIterative()

Validator:

  • setNonIterative() - not recommended in use (overrides default nonIterative rules)
  • addNonIterative()
  • removeNonIterative()
  • getNonIterative()

These methods allow user to set/remove/view so called nonIterative rules. If currently validated rule is set in nonIterative array, then current value will be validated as a whole array (as it is), otherwise it'll be validated item by item if that value is an array but not present in nonIterative.

With default settings, Validator will work the same way as described in #255 PR.
Except of several cases:

  • exists rule will use "WHERE IN", so if value is an array there won't be tons of queries
  • same & different will compare whole array with other attribute

Default nonIterative: array('Exists', 'Same', 'Different').

Add/Remove rules

Global:

// sets new global non iterative array
Validator::setNonIterative('custom_rule');

// adds new rule to global non iterative array
Validator::addNonIterative('extra_rule');

// result: array('custom_rule', 'extra_rule');

Local:

// $val - new validator class
$val->addNonIterative('custom_rule');
// or multiple
$val->addNonIterative(array('custom_rule', 'extra_rule'));

You can explicitly remove any rule from Factory and Validator:

Validator::removeNonIterative('custom_rule');

// $val - new validator class
$val->removeNonIterative('custom_rule');
// or multiple
$val->removeNonIterative(array('custom_rule', 'extra_rule'));

In order to check which rules are set you can call getNonIterative (both on Factory and Validator class).

Custom rules

If user creates custom validation rule via extends or just extends Validator class, then this rule will be validated on per item basis. You'll have to explicitly add your custom rule to the nonIterative array via addNonIterative on Factory or Validator class.

@KaneCohen KaneCohen mentioned this pull request Mar 29, 2013
@taylorotwell
Copy link
Member

I want to work on a solution where the validator is smart enough to handle this without these extra methods. No more PRs on this issue are necessary.

@KaneCohen
Copy link
Contributor Author

@taylorotwell, ok, looking forward to it.

@KaneCohen KaneCohen deleted the val3 branch February 22, 2014 20:42
gonzalom pushed a commit to Hydrane/tmp-laravel-framework that referenced this pull request Oct 12, 2023
…sole

[5.6] Added phpdbg to runningInConsole to bring in line with Laravel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants