customizing error messages with a str_replace() #1338
-
In a CRUD you can do <?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CommonUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return backpack_auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'currentDues.*.amountPaid' => ['nullable', 'integer', 'numeric', 'lte:36'],
];
}
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}
/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
'currentDues.*.amountPaid' => 'The amount paid field must be less than or equal to 36.',
];
}
} If I don't do the
That's suboptimal because what the end user sees isn't a field name with that but rather one that says, simply, "Amount Due". That was my motivation for changing it. The problem is if someone tries to submit
Like the error message changes based on the validation method that's being employed. My question is... is there a way to do an str_replace('currentDues.0.amountPaid', 'amount paid', $message); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes, you can customize error messages in Backpack to make them more user-friendly. There are a few approaches to handle this situation: 1. Using attributes() methodThe best way to handle this is to use the public function attributes()
{
return [
'currentDues.*.amountPaid' => 'amount paid',
];
} This will replace all instances of "currentDues.*.amountPaid" with "amount paid" in all validation error messages, regardless of which validation rule failed. 2. Specific validation rule messagesIf you need different messages for different validation rules, you can specify them individually: public function messages()
{
return [
'currentDues.*.amountPaid.integer' => 'The amount paid must be a whole number.',
'currentDues.*.amountPaid.numeric' => 'The amount paid must be a number.',
'currentDues.*.amountPaid.lte' => 'The amount paid must be less than or equal to 36.',
];
} 3. For repeatable fieldsFor repeatable fields specifically, Backpack has improved how error messages are displayed. In newer versions, you can customize messages for repeatable fields using the standard Laravel nested array validation syntax as mentioned in the GitHub issue #4089: public function messages()
{
return [
'repeatable.*.field_name.required' => 'this field is required'
];
} Alternative approachIf you're working with a simpler model, you could also use the array validation approach directly in your CrudController instead of creating a separate FormRequest class: $rules = ['currentDues.*.amountPaid' => 'nullable|integer|numeric|lte:36'];
$messages = [
'currentDues.*.amountPaid.integer' => 'The amount paid must be a whole number.',
'currentDues.*.amountPaid.lte' => 'The amount paid must be less than or equal to 36.',
];
$this->crud->setValidation($rules, $messages); This approach is documented in the Backpack Create Operation docs and Update Operation docs. Remember that Backpack uses standard Laravel validation under the hood, so all Laravel validation customization techniques will work. ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (closed): Customize integrity error message (question) |
Beta Was this translation helpful? Give feedback.
Yes, you can customize error messages in Backpack to make them more user-friendly. There are a few approaches to handle this situation:
1. Using attributes() method
The best way to handle this is to use the
attributes()
method in your FormRequest class. This method allows you to map field names to more user-friendly labels:This will replace all instances of "currentDues.*.amountPaid" with "amount paid" in all validation error messages, regardless of which validation rule failed.
2. Specific validation rule messages
If you need different messages for different validation rules, you ca…