Download the PHP package henzeb/ruler-laravel without Composer

On this page you can find all versions of the php package henzeb/ruler-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package ruler-laravel

Ruler for Laravel

Build Status Latest Version on Packagist Total Downloads Test Coverage

This library enhances the way you work with Laravel's validation Rules. It allows you to use your Rule classes as string-based validators and group rules into reusable rulesets.

For example: Laravel has bundled a rule for enums. As the parameter you pass is a string, you should be able to use it like this:

right?

wrong!

In order to use it like this, you need to extend your Validator and use the Illuminate\Validation\Rules\Enum rule, or create your own version. This library simplifies that process.

I've gone ahead and registered enum for you, but you can just as easily add your own rules.

Installation

You can install the package via composer:

note: I only support PHP ^8.1 because of the enums.

Usage

Simply add the Henzeb\Ruler\Concerns\Ruler trait to your service provider and add your rules to the $rules property.

You can either specify a name, or just let Ruler create a name for you.

For example: YourOtherRule will get the name your_other_rule.

If your provider needs to implement the boot method, just call the bootRuler method inside that boot method

It is also possible to do it yourself in case you need to do it conditionally.

Registering rules via the Rule facade

You can also register rules directly using the Rule facade, without needing a service provider:

You can also pass an array to register multiple rules at once:

The Rule class

The rules are implemented just like any other Rule you'd normally define. So you'll be familiar with the implementation.

Note: You can also use invokable rules here.

Parameters

You can use parameters. Just add a constructor with the parameters in the order you'd like to use them. Optional parameters are supported.

Note: As for now, you'll receive them as strings, no casting to other scalars is done at this time.

Implicit rules

To add an implicit rule, all you have to do is implement the Illuminate\Contracts\Validation\ImplicitRule interface. Ruler will do the rest for you.

Dependent rules

To add a dependent rule, just implement the Illuminate\Contracts\Validation\DataAwareRule interface. interface Ruler will do the rest for you.

Validator aware rules

Ruler also supports Validator aware rules. Just implement the required interface.

mixing up interfaces

You can mix up the interfaces just like you would in vanilla Laravel. For instance: A rule that is implicit can also be dependent

The error message

The error message should be placed in the message method as defined in Illuminate\Contracts\Validation\Rule, just as you normally would.

The message method is called dynamically, which means you can store the message in your Rule instance and return it in the message method.

It also supports returning arrays. When an array is returned, the MessageBag contains the messages as if they were coming from different validation rules. This way your Rule can do grouped validations (for instance using another Validator instance).

replacers

Out of the box, you can use :<number> to point to a parameter, but if you want them named, you can use the Henzeb\Ruler\Contracts\ReplacerAwareRule interface.

The parameters are in order as specified. param_1 will point to the value of $param_1 and so on.

Closures

You can add a Closure to a replacer if you have specific needs. A Closure always receives the value of the current parameter, the attributes name, the other parameters (named) and all the fields that are under validation.

Ruleset

The Ruleset class allows you to group multiple validation rules into a single reusable rule object. Instead of writing a custom Rule with manual validation logic, you define a set of existing Laravel rules that are applied together.

Basic usage

Extend the Henzeb\Ruler\Ruleset class and implement the rules method:

Then use it like any other rule object:

You can also return rules as a string:

Implicit behavior

By default, a Ruleset is implicit, meaning it validates even when the value is empty. You can disable this by setting the $implicit property to false:

Custom messages and attributes

Override the messages and attributes methods to customize error messages and attribute names:

Configuring the inner validator

The configure method gives you access to the underlying Validator instance, allowing you to use features like stopOnFirstFailure or sometimes:

Using with the Ruler trait

A Ruleset can also be registered as a string-based rule via the Ruler trait, just like any other rule:

This allows you to use it as a string:

Overriding the Validator resolver

Because Ruler has a custom Validator instance set to resolve by Laravel, you need to extend the Henzeb\Ruler\Validator\RulerValidator class in case you want to change the resolver.

Testing

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The GNU AGPLv. Please see License File for more information.


All versions of ruler-laravel with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/support Version ^11|^12
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package henzeb/ruler-laravel contains the following files

Loading the files please wait ...