-
-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Required rule flawed #321
Comments
Can you provide a short code sample please? |
`Valitron\Validator::addRule('requiredIfOther', function($field, $value, array $params, array $fields) { $rules = [ $v = new Valitron\Validator(['country_id' => 23, 'country_province_id' => '']); |
It's similar to what I've been through. I just inherited public function validate()
{
//...
if (($this->hasRule('optional', $field) && isset($values))
|| ($this->hasRule('requiredWith', $field) || $this->hasRule('requiredWithout', $field)
|| $this->hasRule('requiredIf', $field)) /// ADD THIS LINE
) {
//Continue with execution below if statement
}
//...
} |
One really simple solution to this would be to, now that A more complex solution would be to add a new function to either update // ...
/**
* Always execute requiredWith(out) rules
*
* @var array
*/
protected $_mustBeExecuted = array('requiredWith', 'requiredWithout');
// ...
/**
* Register new validation rule callback
*
* @param string $name
* @param callable $callback
* @param string $message
* @param boolean $mustBeExecuted
* @throws \InvalidArgumentException
*/
public static function addRule($name, $callback, $message = null, $mustBeExecuted = false)
{
if ($message === null) {
$message = static::ERROR_DEFAULT;
}
static::assertRuleCallback($callback);
static::$_rules[$name] = $callback;
static::$_ruleMessages[$name] = $message;
if ($mustBeExecuted) {
static::$_mustBeExecuted[] = $name;
}
}
// ...
private function validationMustBeExcecuted($validation, $field, $values, $multiple){
//always excecute must be executed rules
if (in_array($validation['rule'], static::$_mustBeExecuted)){
return true;
}
//do not execute if the field is optional and not set
if($this->hasRule('optional', $field) && ! isset($values)){
return false;
}
//ignore empty input, except for required and accepted rule
if (! $this->hasRule('required', $field) && ! in_array($validation['rule'], array('required', 'accepted'))){
if($multiple){
return count($values) != 0;
}
return (isset($values) && $values !== '');
}
return true;
} |
Field country_province_id should only be required when field country_id is present in the input data and country_id value is 23.
Custom rule "requiredIfOther" does not work because you will have to make country_province_id required for the custom rule to run, else it will never run.
Rule optional also does not work because then country_province_id is not enforced.
Rule requiredWith also does not help because you cannot specify the value (23) of the "with" field.
So it looks like it boils down to Valitron not processing custom rules when the field is not set to required.
What am I missing, or is this a shortcoming of the Valitron package?
The text was updated successfully, but these errors were encountered: