Skip to content

Commit

Permalink
Merge pull request #303 from Wixel/feature/custom_messages_for_field_…
Browse files Browse the repository at this point in the history
…and_rule

feature/custom error messages for field and rule
  • Loading branch information
filisko authored Mar 9, 2020
2 parents 924e218 + bd6917f commit 1a0ee36
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 113 deletions.
45 changes: 34 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,36 @@ composer require wixel/gump
```php
$is_valid = GUMP::is_valid(array_merge($_POST, $_FILES), [
'username' => 'required|alpha_numeric',
'password' => 'required|max_len,100|min_len,6',
'password' => 'required|between_len,4;100',
'avatar' => 'required_file|extension,png;jpg'
]);

// recommended format (supported since v1.7)
// recommended format (supported since v1.7) with field-rule specific error messages example
$is_valid = GUMP::is_valid(array_merge($_POST, $_FILES), [
'username' => ['required', 'alpha_numeric'],
'password' => ['required', 'max_len' => 100, 'min_len' => 6],
'password' => ['required', 'between_len' => [6, 100]],
'avatar' => ['required_file', 'extension' => ['png', 'jpg']]
], [
'username' => ['required' => 'Fill the Username field please.'],
'password' => ['between_len' => '{field} must be between {param[0]} and {param[1]} characters.'],
'avatar' => ['extension' => 'Valid extensions for avatar are: {param}'] // "png, jpg"
]);

if ($is_valid === true) {
// continue
} else {
print_r($is_valid);
var_dump($is_valid); // array of error messages
}
```

### Short format example for filtering

```php
$filtered = GUMP::filter_input([
'field' => ' text ',
'field' => ' text ',
'other_field' => 'Cool Title'
], [
'field' => ['trim', 'upper_case'],
'field' => ['trim', 'upper_case'],
'other_field' => 'slug'
]);

Expand All @@ -65,6 +69,12 @@ $gump->validation_rules([
'credit_card' => 'required|valid_cc'
]);

// field-specific error messages
$gump->set_fields_error_messages([
'username' => ['required' => 'Fill the Username field please, its required.'],
'credit_card' => ['extension' => 'Please enter a valid credit card.']
]);

$gump->filter_rules([
'username' => 'trim|sanitize_string',
'password' => 'trim',
Expand All @@ -73,14 +83,14 @@ $gump->filter_rules([
'bio' => 'noise_words'
]);

$validated_data = $gump->run($_POST);
$valid_data = $gump->run($_POST);

if ($validated_data === false) {
var_dump($gump->get_readable_errors()); // ['Field X is required.']
if ($valid_data === false) {
var_dump($gump->get_readable_errors()); // For HTML: ['Field <span class="gump-field">Somefield</span> is required.']
// or
var_dump($gump->get_errors_array()); // ['field' => 'Field X is required']
var_dump($gump->get_errors_array()); // For APIs?: ['field' => 'Field Somefield is required']
} else {
var_dump($validated_data); // validation successful
var_dump($valid_data); // after filters result: ['field' => 'value']
}
```

Expand Down Expand Up @@ -332,3 +342,16 @@ if ($validated === true) {
print_r($validated);
}
```


Global configuration
--------------------
This configuration values would allow you to turn: `required|contains,value1;value2` into `required|contains:value1,value2`.

```php
GUMP::$rules_delimiter = '|';

GUMP::$rules_parameters_delimiter = ',';

GUMP::$rules_parameters_arrays_delimiter = ';';
```
Loading

0 comments on commit 1a0ee36

Please sign in to comment.