From e2251aa31a50449afd01a963df446924a30d2762 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Sun, 25 Nov 2018 10:35:31 +0700 Subject: [PATCH 1/5] Rules detail using collapsible element --- README.md | 246 +++++++++++++++++++++++++++--------------------------- 1 file changed, 122 insertions(+), 124 deletions(-) diff --git a/README.md b/README.md index bf7fe89..f717b8c 100644 --- a/README.md +++ b/README.md @@ -260,51 +260,7 @@ $validation_a->validate(); ## Available Rules -Below is list of all available validation rules - -* [required](#rule-required) -* [required_if](#rule-required_if) -* [required_unless](#rule-required_unless) -* [required_with](#rule-required_with) -* [required_without](#rule-required_without) -* [required_with_all](#rule-required_with_all) -* [required_without_all](#rule-required_without_all) -* [uploaded_file](#rule-uploaded_file) (uploaded file) -* [mimes](#rule-mimes) (uploaded file) -* [default/defaults](#rule-default) -* [email](#rule-email) -* [uppercase](#rule-uppercase) -* [lowercase](#rule-lowercase) -* [json](#rule-json) -* [alpha](#rule-alpha) -* [numeric](#rule-numeric) -* [alpha_num](#rule-alpha_num) -* [alpha_dash](#rule-alpha_dash) -* [in](#rule-in) -* [not_in](#rule-not_in) -* [min](#rule-min) -* [max](#rule-max) -* [between](#rule-between) -* [digits](#rule-digits) -* [digits_between](#rule-digits_between) -* [url](#rule-url) -* [integer](#rule-integer) -* [ip](#rule-ip) -* [ipv4](#rule-ipv4) -* [ipv6](#rule-ipv6) -* [array](#rule-array) -* [same](#rule-same) -* [regex](#rule-regex) -* [date](#rule-date) -* [accepted](#rule-accepted) -* [present](#rule-present) -* [different](#rule-different) -* [after](#after) -* [before](#before) -* [callback](#callback) - - -#### required +
required The field under this validation must be present and not 'empty'. @@ -323,40 +279,47 @@ Here are some examples: For uploaded file, `$_FILES['key']['error']` must not `UPLOAD_ERR_NO_FILE`. - -#### required_if:another_field,value_1,value_2,... +
+ +
required_if:another_field,value_1,value_2,... The field under this rule must be present and not empty if the anotherfield field is equal to any value. For example `required_if:something,1,yes,on` will be required if `something` value is one of `1`, `'1'`, `'yes'`, or `'on'`. - -#### required_unless:another_field,value_1,value_2,... +
+ +
required_unless:another_field,value_1,value_2,... The field under validation must be present and not empty unless the anotherfield field is equal to any value. - -#### required_with:field_1,field_2,... +
+ +
required_with:field_1,field_2,... The field under validation must be present and not empty only if any of the other specified fields are present. - -#### required_without:field_1,field_2,... +
+ +
required_without:field_1,field_2,... The field under validation must be present and not empty only when any of the other specified fields are not present. - -#### required_with_all:field_1,field_2,... +
+ +
required_with_all:field_1,field_2,... The field under validation must be present and not empty only if all of the other specified fields are present. - -#### required_without_all:field_1,field_2,... +
+ +
required_without_all:field_1,field_2,... The field under validation must be present and not empty only when all of the other specified fields are not present. - -#### uploaded_file:min_size,max_size,extension_a,extension_b,... +
+ +
uploaded_file:min_size,max_size,extension_a,extension_b,... This rule will validate `$_FILES` data, but not for multiple uploaded files. Field under this rule must be following rules below to be valid: @@ -374,13 +337,15 @@ Here are some example definitions and explanations: * `required|uploaded_file:0,1M,png,jpeg`: uploaded file size must be between 0 - 1MB and mime types must be `image/jpeg` or `image/png`. - -#### mimes:extension_a,extension_b,... +
+ +
mimes:extension_a,extension_b,... The `$_FILES` item under validation must have a MIME type corresponding to one of the listed extensions. - -#### default/defaults +
+ +
default/defaults This is special rule that doesn't validate anything. It just set default value to your attribute if that attribute is empty or not present. @@ -400,48 +365,57 @@ $validation->passes(); // true Validation passes because we sets default value for `enabled` and `published` to `1` and `0` which is valid. - -#### email +
+ +
email The field under this validation must be valid email address. - -#### uppercase +
+ +
uppercase The field under this validation must be valid uppercase. - -#### lowercase +
+ +
lowercase The field under this validation must be valid lowercase. - -#### json +
+ +
json The field under this validation must be valid JSON string. - -#### alpha +
+ +
alpha The field under this rule must be entirely alphabetic characters. - -#### numeric +
+ +
numeric The field under this rule must be numeric. - -#### alpha_num +
+ +
alpha_num The field under this rule must be entirely alpha-numeric characters. - -#### alpha_dash +
+ +
alpha_dash The field under this rule may have alpha-numeric characters, as well as dashes and underscores. - -#### in:value_1,value_2,... +
+ +
in:value_1,value_2,... The field under this rule must be included in the given list of values. @@ -461,15 +435,17 @@ $validation = $validator->validate($data, [ Then 'enabled' value should be boolean `true`, or int `1`. - -#### not_in:value_1,value_2,... +
+ +
not_in:value_1,value_2,... The field under this rule must not be included in the given list of values. This rule also using `in_array`. You can enable strict checking by invoking validator and call `strict()` like example in rule `in` above. - -#### min:number +
+ +
min:number The field under this rule must have a size greater or equal than the given number. @@ -486,8 +462,9 @@ $validation = $validator->validate([ ]); ``` - -#### max:number +
+ +
max:number The field under this rule must have a size lower or equal than the given number. Value size calculated in same way like `min` rule. @@ -503,8 +480,9 @@ $validation = $validator->validate([ ]); ``` - -#### between:min,max +
+ +
between:min,max The field under this rule must have a size between min and max params. Value size calculated in same way like `min` and `max` rule. @@ -520,18 +498,21 @@ $validation = $validator->validate([ ]); ``` - -#### digits:value +
+ +
digits:value The field under validation must be numeric and must have an exact length of `value`. - -#### digits_between:min,max +
+ +
digits_between:min,max The field under validation must have a length between the given `min` and `max`. - -#### url +
+ +
url The field under this rule must be valid url format. By default it check common URL scheme format like `any_scheme://...`. @@ -554,62 +535,74 @@ $validation = $validator->validate($inputs, [ > For common URL scheme and mailto, we combine `FILTER_VALIDATE_URL` to validate URL format and `preg_match` to validate it's scheme. Except for JDBC URL, currently it just check a valid JDBC scheme. - -#### integer -The field under this rule must be integer. +
+ +
integer +The field under t rule must be integer. - -#### ip +
+ +
ip The field under this rule must be valid ipv4 or ipv6. - -#### ipv4 +
+ +
ipv4 The field under this rule must be valid ipv4. - -#### ipv6 +
+ +
ipv6 The field under this rule must be valid ipv6. - -#### array +
+ +
array The field under this rule must be array. - -#### same:another_field +
+ +
same:another_field The field value under this rule must be same with `another_field` value. - -#### regex:/your-regex/ +
+ +
regex:/your-regex/ The field under this rule must be match with given regex. - -#### date:format +
+ +
date:format The field under this rule must be valid date format. Parameter `format` is optional, default format is `Y-m-d`. - -#### accepted +
+ +
accepted The field under this rule must be one of `'on'`, `'yes'`, `'1'`, `'true'`, or `true`. - -#### present +
+ +
present The field under this rule must be exists, whatever the value is. - -#### different:another_field +
+ +
different:another_field Opposite of `same`. The field value under this rule must be different with `another_field` value. - -#### after:tomorrow +
+ +
after:tomorrow Anything that can be parsed by `strtotime` can be passed as a parameter to this rule. Valid examples include : - after:next week @@ -617,13 +610,15 @@ Anything that can be parsed by `strtotime` can be passed as a parameter to this - after:2016 - after:2016-12-31 09:56:02 - -#### before:yesterday +
+ +
before:yesterday This also works the same way as the [after rule](#after). Pass anything that can be parsed by `strtotime` - -#### callback +
+ +
callback You can use this rule to define your own validation rule. This rule can't be registered using string pipe. @@ -666,6 +661,9 @@ $validation = $validator->validate($_POST, [ > Note: `Rakit\Validation\Rules\Callback` instance is binded into your Closure. So you can access rule properties and methods using `$this`. +
+ + ## Register/Modify Rule Another way to use custom validation rule is to create a class extending `Rakit\Validation\Rule`. From 77410322d7f142e7bf4a104fcee29654e7e46209 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Sun, 25 Nov 2018 10:36:04 +0700 Subject: [PATCH 2/5] Change minimum PHP version --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f717b8c..be4fe08 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ PHP Standalone library for validating data. Inspired by `Illuminate\Validation` ## Requirements -* PHP 5.5 or higher +* PHP 7.0 or higher * Composer for installation ## Quick Start From 0817b4f5571628512de91de9f96417aeef3ee022 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Sun, 25 Nov 2018 10:42:59 +0700 Subject: [PATCH 3/5] Update available rules section --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index be4fe08..a398ff0 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,8 @@ $validation_a->validate(); ## Available Rules +> Click to show details. +
required The field under this validation must be present and not 'empty'. @@ -345,7 +347,7 @@ The `$_FILES` item under validation must have a MIME type corresponding to one o
-
default/defaults +
default/defaults This is special rule that doesn't validate anything. It just set default value to your attribute if that attribute is empty or not present. From 175eadb32b25472dc1551cb39e7b991706f25eda Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Sun, 25 Nov 2018 10:47:08 +0700 Subject: [PATCH 4/5] Update section register/override rule --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a398ff0..26b8f17 100644 --- a/README.md +++ b/README.md @@ -666,7 +666,7 @@ $validation = $validator->validate($_POST, [
-## Register/Modify Rule +## Register/Override Rule Another way to use custom validation rule is to create a class extending `Rakit\Validation\Rule`. Then register it using `setValidator` or `addValidator`. @@ -693,7 +693,7 @@ class UniqueRule extends Rule $this->pdo = $pdo; } - public function check($value) + public function check($value): bool { // make sure required parameters exists $this->requireParameters(['table', 'column']); From 19d4c22c42fe1feedf28d769f779961facef71ee Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Sun, 25 Nov 2018 11:28:16 +0700 Subject: [PATCH 5/5] Add section working with error messages --- README.md | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/README.md b/README.md index 26b8f17..fd82afa 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,123 @@ $validation_a = $validator->make($dataset_a, [ $validation_a->validate(); ``` +## Working with Error Message + +Errors messages are collected in `Rakit\Validation\ErrorBag` object that you can get it using `errors()` method. + +```php +$validation = $validator->validate($inputs, $rules); + +$errors = $validation->errors(); // << ErrorBag +``` + +Now you can use methods below to retrieves errors messages: + +#### `all(string $format = ':message')` + +Get all messages as flatten array. + +Examples: + +```php +$messages = $errors->all(); +// [ +// 'Email is not valid email', +// 'Password minimum 6 character', +// 'Password must contains capital letters' +// ] + +$messages = $errors->all('
  • :message
  • '); +// [ +// '
  • Email is not valid email
  • ', +// '
  • Password minimum 6 character
  • ', +// '
  • Password must contains capital letters
  • ' +// ] +``` + +#### `firstOfAll(string $format = ':message', bool $dotNotation = false)` + +Get only first message from all existing keys. + +Examples: + +```php +$messages = $errors->firstOfAll(); +// [ +// 'email' => Email is not valid email', +// 'password' => 'Password minimum 6 character', +// ] + +$messages = $errors->firstOfAll('
  • :message
  • '); +// [ +// 'email' => '
  • Email is not valid email
  • ', +// 'password' => '
  • Password minimum 6 character
  • ', +// ] +``` + +Argument `$dotNotation` is for array validation. +If it is `false` it will return original array structure, if it `true` it will return flatten array with dot notation keys. + +For example: + +```php +$messages = $errors->firstOfAll(':message', false); +// [ +// 'contacts' => [ +// 1 => [ +// 'email' => 'Email is not valid email', +// 'phone' => 'Phone is not valid phone number' +// ], +// ], +// ] + +$messages = $errors->firstOfAll(':message', true); +// [ +// 'contacts.1.email' => 'Email is not valid email', +// 'contacts.1.phone' => 'Email is not valid phone number', +// ] +``` + +#### `first(string $key)` + +Get first message from given key. It will return `string` if key has any error message, or `null` if key has no errors. + +For example: + +```php +if ($emailError = $errors->first('email')) { + echo $emailError; +} +``` + +#### `toArray()` + +Get all messages grouped by it's keys. + +For example: + +```php +$messages = $errors->toArray(); +// [ +// 'email' => [ +// 'Email is not valid email' +// ], +// 'password' => [ +// 'Password minimum 6 character', +// 'Password must contains capital letters' +// ] +// ] +``` + +#### `count()` + +Get count messages. + +#### `has(string $key)` + +Check if given key has an error. It returns `bool` if a key has an error, and otherwise. + + ## Available Rules > Click to show details.