Should you be creating your slug by hand in first place? Let the model handle it for you while you create/update it.
Format Nova field input before validation
I have an attribute slug that is both nullable and unique. Validation in Nova doesn't seem to hold place using:
->rules('required_if:is_article,1', 'unique:newsfeeds,slug')
I'm getting a constraint violation (duplicate entry), so the validation isn't working, I guess! Are the rules used correct?
P.S. Since it's a slug, I'm using ->resolveUsing(function ($slug) { return Str::slug($slug); }) too; or should I?
P.P.S. How can I override the Nova field entry (input) exactly?
P.P.P.S. I'm using a mutator to set the attribute to be Str::slug()ed, but it's not helping still...
I had to abandon the Field's validation with ->rules() and use ->fillUsing() method as follows:
->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
if ($request->exists($requestAttribute)) {
// apply the slug format
$value = Str::slug($request[$requestAttribute]);
$request[$requestAttribute] = $value;
if ($request->method() === 'POST') {
// validate a new record (POST)
$request->validate([
$requestAttribute => 'required_if:is_article,1|unique:newsfeeds,slug',
]);
} elseif ($request->method() === 'PUT') {
// validate an updated record (PUT)
$request->validate([
$requestAttribute => 'required_if:is_article,1|unique:newsfeeds,slug,'.$model->id,
]);
}
// upon a validation pass
$model->{$attribute} = $value;
}
})
Please or to participate in this conversation.