Laravel merge eloquent validation rule with a Rule::

146 Views Asked by At

How to merge eloquent validation rule with a Rule::

This is what I am attempting to run, but it chokes on the [ ] with Method Illuminate\Validation\Validator::validateRequired|email does not exist.

public function rules()
{
    return [
        'email' => [
            'required|email:rfc,dns|min:5|max:75',
            Rule::unique('email_updates', 'email', 'product_uuid', 'affiliate_uuid')
        ],
    ];
}

This line works independently

return [
        'email' => 'required|email:rfc,dns|min:5|max:75',
];

This also works

return [
        'email' => Rule::unique('email_updates', 'email', 'product_uuid', 'affiliate_uuid'),
];

How do I merge these differing validation syntaxs?

1

There are 1 best solutions below

0
wruckie On

A comment on this question gave me the answer. Method Illuminate\Validation\Validator::validateRequired|min does not exist

You can't use a mix of | and rule

So this is my working answer

public function rules()
{
    return [
        'email' => [
            'required', 'email:rfc,dns', 'min:5', 'max:75',
            Rule::unique('email_updates', 'email', 'product_uuid', 'affiliate_uuid')
        ],
    ];
}

It works, but I seemingly lost the eloquent messages that came with the | syntax, but that is a small price to pay.