Comments (12)
There is also the buildRule()
method which accepts strings:
<?php
use Respect\Validation\Validator as v;
$dynamicallyBuiltValidator = new v;
$dynamicallyBuiltValidator->addRule(v::buildRule('alnum'));
$dynamicallyBuiltValidator->addRule(v::buildRule('noWhitespace'));
$dynamicallyBuiltValidator->addRule(v::buildRule('length', array(1,32)));
That should work! Let me know if that helps you @jackmcdade =D
from validation.
I'm not sure if I get it, but... I think you can do things dynamically just with PHP, like: {$rule}->{$rule2}->{$rulle3}();
maybe you can implement a smarter interface to do so.
if it sounds like a mess, try to use a pattern, maybe a decorator fixes your problem. then you can decorate your validation.
did you tried it ?
from validation.
I tried doing something like that, but it gets really messy trying to figure out if the rule has parameters or not, how many rules there are, and so on.
from validation.
The Respect/Validation ecosystem is very dynamic, you can look through it to take some ideas.
I think that currently we do not support a way to do what you want, so you have to figure out the best approach to do so.
If your problem was to handle params try using call_user_func_array(); using some PHP meta methods. Maybe you have a design issue not directly related with Respect/Validation. If you want, post a gist with your code, maybe anyone can help you. :D
from validation.
@jackmcdade I don`t quite get what are you are trying to do, but here is my guess =P
All rules implement an interface Respect\Validation\Validatable
and regarding the constructor params of each rule you will really have to figure it out since some of them have optional arguments, others have required options and some of them don`t even need any parameters at all.
Every rule is a simple class, you can create a new instance of it and use it all by itself. There are rules that can be used to validate one or more rules all together:
- AllOf
- NoneOf
- OneOf
The actual Respect\Validation\Validator
class is just a tiny layer on top of an AllOf
validation rule, adding new rules every time __call()
is called, this meaning:
<?php
use Respect\Validation\Validator as v;
use Respect\Validation\Rules as Rule;
$idValidator = v::int()->min(1, true);
// is the same as ...
$idValidator = new Rule\AllOf(new Rule\Int, new Rule\Min(1, true));
I think instantiating the classes directly is quite what you what, but I really don't know how solve your problem with parameters need by rules. Let us know if that helped you, if not then with a little more information (or code) we can try again =D
from validation.
@alganet THAT'S the type of thing i'm looking for! So once I've built my ruleset, presumably I execute $dynamicallyBuiltValidator->validate($my_data);
, correct?
from validation.
@jackmcdade that's correct! once built it works like any other validator. v::alnum()
returns the same object as v::buildRule('alnum')
.
from validation.
This is working perfectly. Thanks @alganet!
from validation.
Using this API, how do I refer to an attribute?
from validation.
Using the concrete api (best suited for DI containers and out-of-scope reuse):
// Importing Namespace
use \Respect\Validation\Rules;
// Our validated data:
$member = new stdClass;
$member->age = 16;
// Building the validator for age
$minimumAge = new Rules\MinimumAge(18);
// Building the validator for the attribute bound to $minimumAge
$validMember = new Rules\Attribute('age', $minimumAge);
$isValidMember = $validMember->validate($member);
... or using the builder API (when building tools that use dynamic rules):
// Importing Namespace
use \Respect\Validation\Validator as v;
// Our validated data:
$member = new stdClass;
$member->age = 16;
$validMember = v::buildRule(
'attribute',
array('age', v::buildRule('minimumAge', array(16))
);
$isValidMember = $validMember->validate($member);
... or the fluent API (for end users):
// Importing Namespace
use \Respect\Validation\Validator as v;
// Our validated data:
$member = new stdClass;
$member->age = 16;
$validMember = v::attribute('age', v::minimumAge(16));
$isValidMember = $validMember->validate($member);
All three samples run the exact same validation. The differences are only in the API.
from validation.
Ok, got it! Thanks!
from validation.
@alganet thanks, you saved my day!!
from validation.
Related Issues (20)
- ctype_digit deprecation notice HOT 1
- Release 2.2.4 HOT 2
- Roadmap to 2.3 HOT 13
- Code coverage in local environment HOT 2
- [documentaiton] Validation for a full array of data HOT 3
- Formatter inconsequently stringifies values HOT 6
- IBAN Validation with non-breaking spaces HOT 1
- How do custom validation rules throw custom error messages? HOT 2
- How to compare two field HOT 1
- DateTime validation fails on valid ISO-8601 timestamp HOT 1
- phone() validation incorrectly invalidates valid UK phone numbers HOT 2
- phone() in 2.3 dont work correctly HOT 13
- RFC 3339 Z suffix no longer works with date-time validation in 2.3 HOT 16
- Date-related rules are not working today HOT 3
- Unexpected behavior with the country code rule in 2.3 HOT 4
- Roadmap to 3.0
- Increase code coverage HOT 3
- Email validation with accented characters HOT 3
- Why is it mandatory to require the PHP version to be so new? HOT 2
- DateDiff rule HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validation.