Giter Club home page Giter Club logo

Comments (12)

alganet avatar alganet commented on August 22, 2024 1

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.

wesleyvicthor avatar wesleyvicthor commented on August 22, 2024

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.

jackmcdade avatar jackmcdade commented on August 22, 2024

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.

wesleyvicthor avatar wesleyvicthor commented on August 22, 2024

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.

augustohp avatar augustohp commented on August 22, 2024

@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.

jackmcdade avatar jackmcdade commented on August 22, 2024

@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.

alganet avatar alganet commented on August 22, 2024

@jackmcdade that's correct! once built it works like any other validator. v::alnum() returns the same object as v::buildRule('alnum').

from validation.

jackmcdade avatar jackmcdade commented on August 22, 2024

This is working perfectly. Thanks @alganet!

from validation.

thelinuxlich avatar thelinuxlich commented on August 22, 2024

Using this API, how do I refer to an attribute?

from validation.

alganet avatar alganet commented on August 22, 2024

@thelinuxlich

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.

thelinuxlich avatar thelinuxlich commented on August 22, 2024

Ok, got it! Thanks!

from validation.

yasirunilan avatar yasirunilan commented on August 22, 2024

@alganet thanks, you saved my day!!

from validation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.