Download the PHP package jsl/ensure without Composer

On this page you can find all versions of the php package jsl/ensure. 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 ensure

Ensure - A PHP validation library


Installation

Using Composer:

Basic example

Managing rules

Managing values

Getting the result & errors

Managing error templates

Since you might not like the default error templates/messages the validators return, there are several ways you can customize them

Note: These methods are also available on the Result instance, so you don't need to define them on the Ensure instance before the validation.

Error templates explained

Error templates is the error message returned if a rule/field fails. They can contain placeholders that will be resolved when the errors are rendered

Note: Only the {field} placeholder will be replaced for templates added using setFieldTemplate() since that is a generic message for that field and not for a specific rule.

Set fancy field names

When showing errors to users (specifically on a web page), you don't want to use the input names. To solve this, you can set "fancy" field names which will be used for the {field} when rendering the error templates

Another way to set the fancy names is to use the rule as.

Managing validators

The validators are the heart and soul in a validation library. Without them, the rest would be pretty pointless.

If you're missing/need some validator that isn't included in the default set, you can easily add your own. You can add as many as you want

Custom validators

To add validators, you can use

// addValidator(string $name, mixed $callback, ?string $template = null)
$ensure->addValidator($name, $callback, $optionalTemplate);

Your validator should return true on pass and false on fail.

You can add as many custom validators as you want, and you can create them in many different ways:

A validator can be a callable, fully qualified class name, class instance, array with class name/instance and method.

No matter which type of callback you use, you can pass the error template as the third parameter when adding the validator. I however omit them here for readability.

Closure

The easiest way to add a simple validator

Fully qualified class name

This requires the class to use the magic method __invoke()

Array with fully qualified class name & method

Named function

Using other field values in your validator

If you want to access other field values in your validator, the options are a bit more limited. Create a class that implements the Jsl\Ensure\Contracts\ValidatorInterface

The easiest way is to extend Jsl\Ensure\Abstracts\Validator, which implements that interface with all necessary methods

Imagine the rule:

The implementation could look like this:

Class resolver

As default, Ensure will create an instance of any fully qualified class name in the easiest possible way:

This works perfectly well, unless you want to use some IoC/Dependency container to instantiate your validator classes and inject some dependencies.

Luckily, you can add your own class resolver in form of a closure.

It could look something like this:

Now all validator classes will be resolved using that closure (including the default validators)

The factory

So far, we've only been talking about how to add error templates and validators to a specific Ensure instance.

By using the factory, you can do all those things on a higher level, making sure all new instances inherits those settings:

Existing rules

These are the default rules.
If you want to replace the implementation of any of them, just add a validator with the same name.

If a rule only have one argument, you can pass it directly:
'rule' => 'arg'
If a rule needs multiple arguments, pass them as an array:
'rule' => ['arg1', 'arg2']

required - Can not be replaced Set the field as required. If it's not required and it's missing, it will just be ignored. If it's set as required and is missing, the validation will fail.
Arguments: N/A

nullable - Can not be replaced
Set a field as nullable. If the value is nullable and null, it will skip any further rules. If it's not nullable and the value is null, the validation will fail.
Arguments: N/A

as - Can not be replaced
Set a fancy field name for the field to be used in the error templates.
Arguments: (string $fancyName)
Example: 'as' => 'A fancy name'

alphanum
Check if a string only contains alpha numeric characters
Arguments: N/A

alpha
Check if a string only contains alpha characters
Arguments: N/A

email
Check if a string is a valid email address
Arguments: N/A

hex
Check if a string is a hex decimal value
Arguments: N/A

ip
Check if a string is a valid IP address
Arguments: N/A

mac
Check if a string is a valid MAC address
Arguments: N/A

url
Check if a string is a valid URL
Arguments: N/A

size
Check if the value is of a specific size.

Arguments: (int $size)
Example: 'size' => 10

minSize
Check if the size of the value is at least x

Arguments: (int $size)
Example: 'minSize' => 5

maxSize
Check if the size of the value is at most x.

Arguments: (int $size)
Example: 'maxSize' => 30

in
Check if a value exists in a list of values. Like in_array()
Arguments: (array $haystack)
Example: 'in' => [1,2,3,4]

notIn
Check if a value does not exists in a list of values. Like in_array() === false
Arguments: (array $haystack)
Example: 'notIn' => [1,2,3,4]

same
Check if a value is the same as another field value
Arguments: (string $fieldname)
Example: 'same' => 'nameOfAnotherField'

different
Check if a value is different than another field value
Arguments: (string $fieldname)
Example: 'different' => 'nameOfAnotherField'

type
Check if a value is a specific type
Arguments: (string $type)
Example: 'type' => 'string'
Possible values: string, numeric, integer, decimal, array, boolean

Rulesets

If you need/want to use the same list of rules in multiple places, it can be ineffective to manually add them in all those places.

Instead of passing around an array with the rules, you can add them as a ruleset to your main EnsureFactory-instance:

To use a ruleset, pass the ruleset name instead of a list of rules when creating a new Ensure-instance using the factory:


All versions of ensure with dependencies

PHP Build Version
Package Version
No informations.
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 jsl/ensure contains the following files

Loading the files please wait ....