diff --git a/README.md b/README.md index e8e08f4..82244ee 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Valitron uses [Composer](http://getcomposer.org) to install and update: ``` curl -s http://getcomposer.org/installer | php -php composer.phar require vlucas/valitron +php composer.phar require okbach/valitron ``` The examples below use PHP 5.4 syntax, but Valitron works on PHP 5.3+. @@ -56,6 +56,21 @@ if($v->validate()) { } ``` + +## AllowedFields Usage + +The `AllowedFields` method is used to restrict input data to a predefined set of fields. +If extra fields are detected, an error message will display the unexpected fields. + +### Example: +```php +$allowedKeys = ['name', 'email', 'phone_number', 'password']; +$v->AllowedFields($allowedKeys); +``` + + + + Using this format, you can validate `$_POST` data directly and easily, and can even apply a rule like `required` to an array of fields: diff --git a/composer.json b/composer.json index 37c8712..4d4983c 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,16 @@ { - "name": "vlucas/valitron", + "name": "okbach/valitron", "type": "library", "description": "Simple, elegant, stand-alone validation library with NO dependencies", "keywords": ["validation", "validator", "valid"], - "homepage": "https://github.com/vlucas/valitron", + "homepage": "https://github.com/okbach/valitron", "license": "BSD-3-Clause", "authors": [ + { + "name": "Okbach", + "email": "gokbahack@gmail.com", + "homepage": "https://github.com/okbach" + }, { "name": "Vance Lucas", "email": "vance@vancelucas.com", @@ -13,10 +18,10 @@ } ], "require": { - "php": ">=5.3.2" + "php": ">=7.2" }, "require-dev": { - "phpunit/phpunit": ">=4.8.35" + "phpunit/phpunit": "^9.0" }, "suggest": { "ext-mbstring": "It can support the multiple bytes string length." diff --git a/lang/ar.php b/lang/ar.php index 533ccab..f0cbbb9 100644 --- a/lang/ar.php +++ b/lang/ar.php @@ -25,5 +25,6 @@ 'dateFormat' => "يجب ان يكون تاريخ بهذه الصيغة '%s'", 'dateBefore' => "التاريخ يجب ان يكون قبل '%s'", 'dateAfter' => "التاريخ يجب ان يكون بعد '%s'", - 'contains' => "يجب ان يحتوي %s" + 'contains' => "يجب ان يحتوي %s", + 'extra_input' => "مدخلات غير متوقعة", ); diff --git a/lang/en.php b/lang/en.php index 8e66cb5..b9fe6d3 100644 --- a/lang/en.php +++ b/lang/en.php @@ -39,4 +39,5 @@ 'requiredWithout'=> "is required", 'subset' => "contains an item that is not in the list", 'arrayHasKeys' => "does not contain all required keys", + 'extra_input' => "Unexpected input", ); diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 3ce5fc0..0997d90 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -1591,4 +1591,23 @@ private function isAssociativeArray($input){ //array contains at least one key that's not an can not be cast to an integer return count(array_filter(array_keys($input), 'is_string')) > 0; } + + + /** + * + * @param array $allowedFields + * @return void + */ + public function AllowedFields(array $allowedFields) + { + $extraFields = array_diff(array_keys($this->_fields), $allowedFields); + if (!empty($extraFields)) { + $langMessages = $this->getRuleMessages(); + $errorMessage = isset($langMessages['extra_input']) ? $langMessages['extra_input'] : 'Unexpected input'; + foreach ($extraFields as $field) { + $this->_errors[$field][] = $errorMessage; + } + } + } + }