Merge pull request #218 from vlucas/issue208

validate fields that are required, but may have empty or null values. Handles #208
This commit is contained in:
Willem Wollebrants 2017-08-28 22:06:05 +02:00 committed by GitHub
commit 20a9c2d604
3 changed files with 60 additions and 12 deletions

View File

@ -103,7 +103,7 @@ V::lang('ar');
## Built-in Validation Rules ## Built-in Validation Rules
* `required` - Required field * `required` - Field is required
* `equals` - Field must match another field (email/password confirmation) * `equals` - Field must match another field (email/password confirmation)
* `different` - Field must be different than another field * `different` - Field must be different than another field
* `accepted` - Checkbox or Radio must be accepted (yes, on, 1, true) * `accepted` - Checkbox or Radio must be accepted (yes, on, 1, true)
@ -142,6 +142,16 @@ extension for greater accuracy and reliability. The extension is not required
for Valitron to work, but Valitron will use it if available, and it is highly for Valitron to work, but Valitron will use it if available, and it is highly
recommended. recommended.
## Required fields
the `required` rule checks if a field exists in the data array, and is not null or an empty string.
```php
$v->rule('required', 'field_name');
```
Using an extra parameter, you can make this rule more flexible, and only check if the field exists in the data array.
```php
$v->rule('required', 'field_name', true);
```
## Credit Card Validation usage ## Credit Card Validation usage
Credit card validation currently allows you to validate a Visa `visa`, Credit card validation currently allows you to validate a Visa `visa`,

View File

@ -143,10 +143,16 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params
* @return bool * @return bool
*/ */
protected function validateRequired($field, $value) protected function validateRequired($field, $value, array $params= array(), array $fields = array())
{ {
if (isset($params[0]) && (bool) $params[0]){
$find = $this->getPart($this->_fields, explode('.', $field), true);
return $find[1];
}
if (is_null($value)) { if (is_null($value)) {
return false; return false;
} elseif (is_string($value) && trim($value) === '') { } elseif (is_string($value) && trim($value) === '') {
@ -886,48 +892,49 @@ class Validator
$this->_labels = array(); $this->_labels = array();
} }
protected function getPart($data, $identifiers) protected function getPart($data, $identifiers, $allow_empty = false)
{ {
// Catches the case where the field is an array of discrete values // Catches the case where the field is an array of discrete values
if (is_array($identifiers) && count($identifiers) === 0) { if (is_array($identifiers) && count($identifiers) === 0) {
return array($data, false); return array($data, false);
} }
// Catches the case where the data isn't an array or object // Catches the case where the data isn't an array or object
if (is_scalar($data)) { if (is_scalar($data)) {
return array(NULL, false); return array(NULL, false);
} }
$identifier = array_shift($identifiers); $identifier = array_shift($identifiers);
// Glob match // Glob match
if ($identifier === '*') { if ($identifier === '*') {
$values = array(); $values = array();
foreach ($data as $row) { foreach ($data as $row) {
list($value, $multiple) = $this->getPart($row, $identifiers); list($value, $multiple) = $this->getPart($row, $identifiers, $allow_empty);
if ($multiple) { if ($multiple) {
$values = array_merge($values, $value); $values = array_merge($values, $value);
} else { } else {
$values[] = $value; $values[] = $value;
} }
} }
return array($values, true); return array($values, true);
} }
// Dead end, abort // Dead end, abort
elseif ($identifier === NULL || ! isset($data[$identifier])) { elseif ($identifier === NULL || ! isset($data[$identifier])) {
if ($allow_empty){
//when empty values are allowed, we only care if the key exists
return array(null, array_key_exists($identifier, $data));
}
return array(null, false); return array(null, false);
} }
// Match array element // Match array element
elseif (count($identifiers) === 0) { elseif (count($identifiers) === 0) {
if ($allow_empty){
//when empty values are allowed, we only care if the key exists
return array(null, array_key_exists($identifier, $data));
}
return array($data[$identifier], false); return array($data[$identifier], false);
} }
// We need to go deeper // We need to go deeper
else { else {
return $this->getPart($data[$identifier], $identifiers); return $this->getPart($data[$identifier], $identifiers, $allow_empty);
} }
} }

View File

@ -1240,6 +1240,37 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v3->validate()); $this->assertFalse($v3->validate());
$this->assertNotEmpty($v3->errors()); $this->assertNotEmpty($v3->errors());
} }
public function testRequiredEdgeCases()
{
$v = new Validator(array(
'zero'=>0,
'zero_txt' => '0',
'false'=>false,
'empty_array'=>array()
));
$v->rule('required', array('zero', 'zero_txt', 'false', 'empty_array'));
$this->assertTrue($v->validate());
}
public function testRequiredAllowEmpty(){
$data= array(
'empty_text'=>'',
'null_value' => null,
'in_array'=>array(
'empty_text'=>''
)
);
$v1= new Validator($data);
$v1->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'));
$this->assertFalse($v1->validate());
$v2= new Validator($data);
$v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'));
$this->assertFalse($v2->validate());
}
} }
function sampleFunctionCallback($field, $value, array $params) { function sampleFunctionCallback($field, $value, array $params) {