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

RequiredWith #75

Closed
dikaso opened this issue Jun 9, 2014 · 4 comments
Closed

RequiredWith #75

dikaso opened this issue Jun 9, 2014 · 4 comments

Comments

@dikaso
Copy link

dikaso commented Jun 9, 2014

Title says it all.

How would you implement this?

@vlucas
Copy link
Owner

vlucas commented Jun 9, 2014

Unfortunately, the title doesn't say it all. Required with what? Another field? A specific value? Please elaborate. A specific use-case would be appreciated.

@dikaso
Copy link
Author

dikaso commented Jun 9, 2014

Dummy example:

If i have field "type" with rule "in" allowing only 1 and 2.

I need another field to be required only if "type" field is 2.

We can see those examples in popular frameworks like laravel and symphony.

@ellotheth
Copy link

@DinkoMiletic probably means something like this:

protected function validateRequiredWith($field, $value, array $params) {
    list($match, $child) = $params;
    if ($value == $match) {
        return array_key_exists($child, $this->_fields) && $this->validateRequired($child, $this->_fields[$child]);
    }

    return true;
}

When $value is $match, $child is required.

$v = new Validator($post);
$v->rule('requiredWith', 'type', '2', 'second-field);

It'd be useful if you have a form field with multiple possible values (maybe a set of checkboxes), one of which requires extra information (maybe a text field has to be filled out if one of the checkboxes is selected).

The rest of the logic gets a little hinkey (e.g. hasRule(), the initial validate() check for required, nested fields, etc.), but I think that's the gist.

@mangopeaches
Copy link
Contributor

#267 should take care of this

@dikaso dikaso closed this as completed Jun 23, 2023
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

4 participants