valitron icon indicating copy to clipboard operation
valitron copied to clipboard

Custom rule not used unless field is required

Open mikejw opened this issue 7 years ago • 7 comments

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?

mikejw avatar May 16 '18 15:05 mikejw

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.

shrimpwagon avatar Feb 18 '20 18:02 shrimpwagon

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 :)

willemwollebrants avatar Feb 18 '20 18:02 willemwollebrants

@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.

shrimpwagon avatar Feb 19 '20 11:02 shrimpwagon

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?

willemwollebrants avatar Feb 19 '20 11:02 willemwollebrants

Experience the same issue.

saschanos avatar Apr 05 '20 19:04 saschanos

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'.

thanh-taro avatar Dec 11 '20 10:12 thanh-taro

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

keevitaja avatar Oct 07 '21 10:10 keevitaja