Added "optional" rule validation

With 'optional' rule validation, values does not need to be included in data array.
However, if they are, they must pass validation.

Before this patch, if you pass data with empty ( '' ) value, then it is ignored if "required" was not added.
With this version, this is now fixed.

Text case available here: http://pastebin.com/N2eQjJys
This commit is contained in:
Tilen Majerle 2015-11-30 22:23:21 +01:00
parent a06309ce75
commit 0d77529c90

View File

@ -731,6 +731,12 @@ class Validator
return $isInstanceOf; return $isInstanceOf;
} }
//Validate optional field
protected function validateOptional($field, $value, $params) {
//Always return true
return true;
}
/** /**
* Get array of fields and data * Get array of fields and data
* *
@ -868,7 +874,9 @@ class Validator
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field)); list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field));
// Don't validate if the field is not required and the value is empty // Don't validate if the field is not required and the value is empty
if ($v['rule'] !== 'required' && !$this->hasRule('required', $field) && (! isset($values) || $values === '' || ($multiple && count($values) == 0))) { if ($this->hasRule('optional', $field) && isset($values)) {
//Continue with execution below if statement
} elseif ($v['rule'] !== 'required' && !$this->hasRule('required', $field) && (! isset($values) || $values === '' || ($multiple && count($values) == 0))) {
continue; continue;
} }