Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand All @@ -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:

Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
{
"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",
"homepage": "https://www.vancelucas.com"
}
],
"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."
Expand Down
3 changes: 2 additions & 1 deletion lang/ar.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
'dateFormat' => "يجب ان يكون تاريخ بهذه الصيغة '%s'",
'dateBefore' => "التاريخ يجب ان يكون قبل '%s'",
'dateAfter' => "التاريخ يجب ان يكون بعد '%s'",
'contains' => "يجب ان يحتوي %s"
'contains' => "يجب ان يحتوي %s",
'extra_input' => "مدخلات غير متوقعة",
);
1 change: 1 addition & 0 deletions lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
19 changes: 19 additions & 0 deletions src/Valitron/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

}