Skip to content

Add conditional required validators - #366

Open
chaungoclong wants to merge 1 commit into
Wixel:masterfrom
chaungoclong:feature/add-conditional-required-rules
Open

chaungoclong wants to merge 1 commit into
Wixel:masterfrom
chaungoclong:feature/add-conditional-required-rules

Conversation

@chaungoclong

Copy link
Copy Markdown

This PR introduces 5 new validation rules, expanding support for conditional validation and resolving #235.

New Validators

  • required_with: Requires the field when any of the specified fields are present and non-empty.
  • required_with_all: Requires the field when all of the specified fields are present and non-empty.
  • required_without: Requires the field when any of the specified fields are missing or empty.
  • required_without_all: Requires the field when all of the specified fields are missing or empty.
  • required_array_keys: Requires the field to be an array containing all specified keys.

Changes

  • Implement all five validation rules.
  • Add test coverage for all new validators.
  • Update validation error messages across all 19 supported languages.
  • Update the README with documentation and usage examples for both string and array formats.

Usage Examples

1. required_with

Require an email address when either first_name or last_name is provided.

String format:

$rules = [
    'email' => 'required_with,first_name;last_name'
];

Array format:

$rules = [
    'email' => ['required_with' => ['first_name', 'last_name']]
];

2. required_with_all

Require shipping_address when both first_name and last_name are provided.

String format:

$rules = [
    'shipping_address' => 'required_with_all,first_name;last_name'
];

Array format:

$rules = [
    'shipping_address' => ['required_with_all' => ['first_name', 'last_name']]
];

3. required_without

Require at least one of two contact methods, phone or mobile.

String format:

$rules = [
    'phone' => 'required_without,mobile',
    'mobile' => 'required_without,phone'
];

Array format:

$rules = [
    'phone' => ['required_without' => ['mobile']],
    'mobile' => ['required_without' => ['phone']]
];

4. required_without_all

Require an email address when both phone and mobile are missing or empty.

String format:

$rules = [
    'email' => 'required_without_all,phone;mobile'
];

Array format:

$rules = [
    'email' => ['required_without_all' => ['phone', 'mobile']]
];

5. required_array_keys

Require data to be an array containing both id and name.

String format:

$rules = [
    'data' => 'required_array_keys,id;name'
];

Array format:

$rules = [
    'data' => ['required_array_keys' => ['id', 'name']]
];

Issue

Resolves #235.

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

Successfully merging this pull request may close these issues.

Two fields, at least one is required

1 participant