Download the PHP package tobento/service-validation without Composer
On this page you can find all versions of the php package tobento/service-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tobento/service-validation
More information about tobento/service-validation
Files in tobento/service-validation
Package service-validation
Short Description Easily validating data.
License MIT
Homepage https://www.tobento.ch
Informations about the package service-validation
Validation Service
The Validation Service provides an easy way to validate data.
Table of Contents
- Getting started
- Requirements
- Highlights
- Documentation
- Validating
- Single value
- Multiple values
- Nested values
- Rules Definition
- Validator
- Create Validator
- Validator Interface
- Rules Aware
- Validation
- Validation Interface
- Error Messages
- Validated Data
- Rules
- Rules Interface
- Default Rules
- Available Rules
- Adding Rules
- Custom Rules
- Convert Rules To HTML Validation Attributes
- Rule
- Rule Interface
- Passes Rule
- Custom Rule
- Rules Parser
- Default Rules Parser
- Custom Rules Parser
- Messages
- Messages Factory
- Message Translation
- Validating
- Credits
Getting started
Add the latest version of the validation service running this command.
Requirements
- PHP 8.0 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
Documentation
Validating
Single value
Easily validate a single value.
Check out Validator to learn more about the Validator.\ Check out Validation to learn more about the ValidationInterface.
Parameters explanation
Parameter | Description |
---|---|
value | The value to validate. |
rules | The rules definition. See Rules Definition for more detail. |
data | Any data used by certain rules for validation. See Rules for more detail. |
key | Used for error messages. See Error Messages for more detail. |
Multiple values
Validate multiple values.
Check out Validator to learn more about the Validator.\ Check out Validation to learn more about the ValidationInterface.
Parameters explanation
Parameter | Description |
---|---|
data | The data to validate. |
rules | The rules definitions. See Rules Definition for more detail. |
Nested values
If the incoming values contains "nested" data, you may specify these attributes in your rules using "dot" syntax:
Check out Validator to learn more about the Validator.\ Check out Validation to learn more about the ValidationInterface.
Parameters explanation
Parameter | Description |
---|---|
data | The data to validate. |
rules | The rules definitions. See Rules Definition for more detail. |
Rules Definition
The Default Rules Parser supports the following rules definition.\ You may also check out the Default Rules to learn more about the rules it provides.\ If you add rules "lazy" with dependencies you will need to use the AutowiringRuleFactory for resolving see Default Rules.
string definition
array definition with string rules
If you need to define additional rule parameters or custom error messages, wrap the rule into an array:
object rules
You may define object rules implementing the Rule Interface:
object rules with different validation method
You may define object rules with different validation methods:
Parameters
For each rule you can define custom parameters.
Global parameters
Sometimes you may need custom parameters for all rules.
Validator
Create Validator
Parameters explanation
Parameter | Description |
---|---|
rules | Provides the rules for validation. See Rules for more detail. |
rulesParser | Parses the rules. See Rules Parser for more detail. |
messagesFactory | Creates the error messages. See Messages Factory for more detail. |
Validator Interface
Check out Validating to learn more about the methods.\ Check out Collection Service to learn more it.
Rules Aware
Check out Rules Interface to learn more about the RulesInterface.
Validation
Validation Interface
Methods explanation
Parameter | Description |
---|---|
isValid | Returns true if the validation is valid, otherwise false. |
errors | Returns the error messages. See Message Service for more detail. |
data | Returns the data to validate. See Collection Service for more detail. |
valid | Returns the valid data. See Collection Service for more detail. |
invalid | Returns the invalid data. See Collection Service for more detail. |
rule | Returns the rule or null. |
Error Messages
By default, the rules keys are used as the :attribute parameter when defined in the error messages.\ For translation reason, it is not recommended to write messages like "The :attribute must ...", it is better to add "The title" in the :attribute parameter because "The" might be different for an attribute name depending on the language.
Retrieving the first error message for a data key
Check out the Message Service to learn more about messages in general.
Retrieving all error messages for all data keys
Check out the Message Service to learn more about messages in general.
Determine if messages exist for a data key
Check out the Message Service to learn more about messages in general.
Custom error message
Custom error message parameters
Global custom error message parameters
You might want to define global message parameters for all rules defined:
Skipping first parameter
You might need to skip the first value of the parameters by declaring it as :parameters[-1].
Validated Data
Check out the Collection Service to learn more about Collection in general.
Rules
Rules Interface
Default Rules
With autowiring rule factory
The autowiring rule factory is needed if you define or add rules "lazy" with dependencies.
Available Rules
The following rules are available out of the box:
Rule | Parameters Example | Allows Empty | Description |
---|---|---|---|
alnum | true | The value must be entirely alpha [a-zA-Z] characters and/or numbers. | |
alpha | true | The value must be entirely alpha [a-zA-Z] characters. | |
alphabetic | true | The value must be entirely alphabetic characters. | |
alphabeticNum | true | The value must be entirely alphabetic characters and/or numbers. | |
array | true | The value must be an array. | |
bool | true | The value must be a bool. valid: true, false, 1, 0, "1", "0" |
|
date | true | The value must be a valid date. | |
dateAfter:date | dateAfter:2021-12-24 dateAfter:2021-12-24:true (same time is past) |
true | The value must be a date after the date set. |
dateBefore:date | dateBefore:2021-12-24 dateBefore:2021-12-24:true (same time is past) |
true | The value must be a date before the date set. |
dateFormat:format | dateFormat:Y-m-d:Y.m.d | true | The value must match any of the date formats. |
decimal | true | The value must be a decimal. valid: 23.00, '22.50', 0, -0.00000, '-0.1', 50, '55' |
|
digit | true | The value must only contain digits [0-9]. | |
each:list | each:blue:red (starting for 0) each:5=>blue:8=>red |
true | Each value in the array must be within the list of values provided with the same keys. |
eachIn:list | eachIn:blue:red | true | Each value in the array must be within the list of values provided. |
eachWith:key_rules:value_rules | eachWith:int/minNum;1:alpha/maxLen;3 ['key' => 'int', 'value' => 'required|alpha'] |
true | The value must be an with the rules passing. |
true | The value must be a valid email address. | ||
float | true | The value must be a float. | |
htmlclean | true | The value must be html clean. | |
in:list | in:blue:red | true | The value must be in the list provided. |
int | true | The value must be a int. | |
json | true | The value must be a valid JSON string. | |
maxItems:number | maxItems:5 | true | The array must be at most the number of items. |
maxLen:length | maxLen:5 | true | The value must at most have the maximun length set. |
maxNum:number | maxNum:5 | true | The value must be at most the number set. |
minItems:number | minItems:5 | true | The array must have at least the number of items. |
minLen:length | minLen:5 | true | The value must at least have the minimum length set. |
minNum:number | minNum:5 | true | The value must be at least the number set. |
notEmpty | true | The value must be not be empty. | |
notNull | true | The value must be not be null. | |
numeric | true | The value must be numeric. | |
regex:pattern | regex:#^[a-z0-9]+$# | true | The value must match the pattern. |
required | false | The value must not be empty. | |
required_ifEqual:field:value | required_ifEqual:role:admin | false | Required when field is equal to value. |
required_ifIn:field:value:value1 | required_ifIn:role:admin:editor | false | Required when field is one of the values. |
required_with:field:field1 | required_with:firstname:lastname | false | Required when one of the fields is present and not empty. |
required_without:field:field1 | required_without:firstname:lastname | false | Required when one of the fields is not present and not empty. |
same:field | same:user.password | true | The value must be the same as the field. |
scalar | true | The value must be scalar. | |
string | true | The value must be a string. | |
url | true | The value must be a valid URL. |
Adding Rules
You may add additional rules by the following way. If you add rules "lazy" with dependencies you will need to use the AutowiringRuleFactory for resolving.
Custom Rules
You may write your own rules class or adjusting the default rules for your needs.
Convert Rules To HTML Validation Attributes
You may use the to convert rules to HTML validation attributes:
Supported Rules
The following rules are supported. Any other rule will just be ignored.
, , , , , , , , , , .
Mapping
Validation Attribute | Supported Input Types |
---|---|
, , , , , | |
, , , , , , , , , , , , , , , | |
, | Except: |
, , | , , , , , , |
Render Attributes
You may install and use the Service Tag - Attributes Class to render the validation attributes on HTML form elements:
Rule
Rule Interface
Passes Rule
With the Passes rule you can create any custom rule.
Passes parameters
The following parameters are available:
If you have set up the validator with the autowiring rule factory, the and callable are autowired:
Ensure Declared Closure Type
By default, the declared type of a closure parameter will be automatically verified. If it does not match the input value type, the rule does not pass and the closure will never be executed.
Custom error message
You may specify a custom error message:
Skip validation
You may use the skipValidation parameter in order to skip validation under certain conditions:
Custom Rule
With multiple validation methods
See Rule\Strings for demo.
Needs Validation for validation
See Rule\Arr for demo.
Needs Validator for validation
See Rule\Arr for demo.
Skip validation when passes
See rules for demo.
Rules Parser
The role of the rules parser is to parse the Rules Definition.
Default Rules Parser
See the Rules Definition for more detail.
Custom Rules Parser
You may write your own parser for your needs implementing the following interface.
Messages
Messages are used for the validation Error Messages.\ Check out the Message Service to learn more about messages in general.
Messages Factory
With the message factory you can fully control how messages are created and modified.
Parameters explanation
Parameter | Description |
---|---|
messageFactory | Creating the messages. See Message Service - Message Factory for more detail. |
modifiers | With modifiers you can modify messages such as translating for instance. See Message Service - Modifiers for more detail. |
logger | You might set a looger to log messages. |
Default modifiers
If you do not set modifiers on the factory, the following modifiers are added:
Message Translation
If you want to translate messages you may use the translator modifiers:\ First you will need to install Translation Service though.
Credits
- Tobias Strub
- All Contributors
All versions of service-validation with dependencies
tobento/service-message Version ^1.0
tobento/service-collection Version ^1.0
tobento/service-autowire Version ^1.0
tobento/service-dater Version ^1.0
psr/container Version ^2.0
psr/log Version ^1.1 || ^2.0 || ^3.0