0
votes

I have laravel validation request form, like this:

public function rules()
{
    $id = $this->input('wlId');

    return [
        'id'          => ['sometimes ', 'integer'],
        'domain_name'   => ['required', 'string', 'unique:white_label,domain_name' . $id],
        'disabled'      => ['required', 'boolean']
    ];
}

I set id for ignore my entry during the unique check, but if id is't an integer but for example a string then I will get sql error from validator.

How can I stop validation if the id field fails validation?

2
because you specified that should be integer: 'id' => ['sometimes ', 'integer'], That means that should be only integer, but if you want to be an integer you should use 'numeric'Marinario Agalliu

2 Answers

1
votes

You are going to ignore a certain value while using unique validation rule. Consider the following snippet;

use Illuminate\Validation\Rule;

// ...

public function rules() {
    $id = $this->input('wlId');

    return [
        'id' => ['sometimes ', 'integer'],
        'domain_name' => [
            'required',
            'string',
            Rule::unique('white_label', 'domain_name')->ignore($id)
        ],
        'disabled' => ['required', 'boolean']
    ];
}

For more details, see this link; https://laravel.com/docs/8.x/validation#rule-unique

0
votes

As mentioned in the documentation you can use bail rule

Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute

    $request->validate([
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
    ]);

In this example, if the unique rule on the title attribute fails, the max rule will not be checked. Rules will be validated in the order they are assigned.