Skip to content
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

Custom rule not used unless field is required #251

Open
mikejw opened this issue May 16, 2018 · 7 comments
Open

Custom rule not used unless field is required #251

mikejw opened this issue May 16, 2018 · 7 comments

Comments

@mikejw
Copy link

mikejw commented May 16, 2018

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
Copy link

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
Copy link
Collaborator

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
Copy link

@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
Copy link
Collaborator

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
Copy link

Experience the same issue.

@thanh-taro
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants