#Laravel validation 1 required and either one of two other required

17 messages · Page 1 of 1 (latest)

native isle
#

Im currently working on a search request that has 1 mandatory filter and either one of two other filters mandatory.

Currently I'm working with:

public function rules(): array
{
  return [
    'period' => [
      'required',
      'numeric',
      Rule::exists('periods', 'id'),
    ],
    'lessonClass' => [
      'required_without:students',
      'numeric',
      Rule::exists('lesson_classes', 'id'),
    ],
    'students' => [
      'required_without:lessonClass',
      'array',
      'min:1',
      'max:50',
    ],
    'students.*' => [
      'numeric',
      Rule::exists('students', 'id'),
    ],
  ];
}

Ofcourse it works like a charm, only downside is that it will give two error messages when either lessonClass or students is missing.
Is there a way to get this to only show one error message, something like: "Either lessonClass or students is required"

charred oxide
#

Hi

#

@native isle

#

Here's how you can modify your rules function to achieve the desired single error message:

public function rules(): array
{
return [
'period' => [
'required',
'numeric',
Rule::exists('periods', 'id'),
],
'lessonClass' => [
'required_with_all:students', // This ensures both are required if one is present
'numeric',
Rule::exists('lesson_classes', 'id'),
],
'students' => [
'required_with_all:lessonClass', // This ensures both are required if one is present
'array',
'min:1',
'max:50',
],
'students.*' => [
'numeric',
Rule::exists('students', 'id'),
],
];
}

native isle
#

Thanks for your reply, but it's not what I'm looking for 🙂
period is always required and either lessonClass or students is required, both is fine, but bare minimum period and either lessonClass or students

charred oxide
#

public function rules(): array
{
return [
'period' => [
'required',
'numeric',
Rule::exists('periods', 'id'),
],
'lessonClass' => [
'required_without:students',
'numeric',
Rule::exists('lesson_classes', 'id'),
],
'students' => [
'required_without:lessonClass',
'array',
'min:1',
'max:50',
],
'students.*' => [
'numeric',
Rule::exists('students', 'id'),
],
];
}

#

Example:

Valid:
period = 123 , lessonClass = 456
period = 123 , students = [789, 1011]
Invalid:
period = 123
period = 123 , lessonClass = 456 , students = [789, 1011]

native isle
#

period = 123 , lessonClass = 456 , students = [789, 1011] is also valid. The rules you added are the same i had in my orginal post 😁 my question is, can it be done without 2 error messages when neither lessonClass and students are present

charred oxide
#

use Illuminate\Contracts\Validation\Rule;

class EitherRequired implements Rule
{
/**
* The fields that are required.
*
* @var array
*/
protected $fields;

/**
 * Create a new rule instance.
 *
 * @param  array  $fields
 * @return void
 */
public function __construct(array $fields)
{
    $this->fields = $fields;
}

/**
 * Determine if the validation rule passes.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @return bool
 */
public function passes($attribute, $value): bool
{
    foreach ($this->fields as $field) {
        if (isset($value[$field])) {
            return true;
        }
    }

    return false;
}

/**
 * Get the validation error message.
 *
 * @return string
 */
public function message(): string
{
    return 'Either :attribute or :other is required.';
}

}

#

Now, you can use this custom rule in your rules function:

public function rules(): array
{
return [
'period' => [
'required',
'numeric',
Rule::exists('periods', 'id'),
],
'lessonClass' => [
'numeric',
Rule::exists('lesson_classes', 'id'),
],
'students' => [
'array',
'min:1',
'max:50',
],
'students.*' => [
'numeric',
Rule::exists('students', 'id'),
],
// Custom rule
'lessonClass|students' => [
new EitherRequired(['lessonClass', 'students']),
],
];
}

#

Let me know if you have any other help ?

native isle
charred oxide
#

The pipe character (|) is a powerful tool for combining validation rules in Laravel. It's a great way to apply a single set of rules to multiple fields.

#

What is your name

#

and are you a website developer ?

native isle
#

My name is already here 🙂 And not perse a website developer, been developing applications for 15 years and for the last 7 years mostly in laravel 🙂

#

I knew about the pipe in the validation rules, though prefer not to use them for readability, but never knew they work in the keys as well