vlucas / valitron

Valitron is a simple, elegant, stand-alone validation library with NO dependencies
BSD 3-Clause "New" or "Revised" License
1.57k stars 250 forks source link

Custom rule not used unless field is required #251

Open mikejw opened 6 years ago

mikejw commented 6 years ago

I have two custom validation rules:

` Validator::addRule('requiredIfUK', function($field, $value, array $params, array $fields) {

        $this->log->info('post code rule');

        if ($value == '' && $fields['country'] == 'United Kingdom') {
            return false;
        } else {
            return true;
        }
    }, 'Must supply a postcode for UK.');

    Validator::addRule('countryIsSet', function($field, $value, array $params, array $fields) {

        if ($value == 'Choose') {
            return false;
        } else {
            return true;
        }
    }, 'Please select a country.');

    $v = new Validator(
        [
            'country' => $this->country,
            'postcode' => $this->postcode,
            'age' => $this->age,
            'rating' => $this->rating,
            'visitor' => $this->visitor
        ]
    );

    $v->rule('requiredIfUK', 'postcode');
    $v->rule('countryIsSet', 'country');
    $v->rule('required', ['country', 'age', 'rating', 'visitor']);

` countryIsSet is applied but requiredIfUK is not. Perhaps because postcode is not in the required list?

shrimpwagon commented 4 years ago

I have experienced the exact same issue. I tried to write a 'present' rule where the field just needs to be preset in the input regardless of emptiness. Come to find out that the input index needs to be there for the rule to be ran however for some reason this works with the 'required' rule. You would think the functionality would be there to do so.

willemwollebrants commented 4 years ago

From the readme: "Using an extra parameter, you can make this rule more flexible, and only check if the field exists in the data array."

$v->rule('required', 'field_name', true);

Having the extra parameter set to "true", the rule will only check if the key is present in the data array :)

shrimpwagon commented 4 years ago

@willemwollebrants Thank you. Yes, I saw this but the main issue is unsolved. Can you please reopen the issue. The main issue is that custom rules are not applied unless the input filed is there.

willemwollebrants commented 4 years ago

I think that using the required rule with the extra parameter does what you want for a "present" rule: it checks if the field is set in the input and it can be empty.

Can you show me a piece of code that has the problem?

saschanos commented 4 years ago

Experience the same issue.

thanh-taro commented 3 years ago

I got the same issue. In my case, I want to make a custom rule requiredIf, means this field is required when match a condition. Nothing happens if I don't use with 'required'.

keevitaja commented 3 years ago

how can i solve this. fields:

foo, bar, baz, xyz

foo and bar are boolean, bar and xyz numeric or null.

i need to check, if foo is true then bar cannot be null or less than 0