0

Validation Message

i wanted to customize this validation message on laravel, i dont know what to do.

i'm using this for error display

@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

and i use my validation on model like below

public static $validation = [
        'schedule_doctor_id' => 'required',
        'nama_lengkap'       => 'required|string',
        'email'              => 'required',
        'no_tlp'             => 'required',
        'tanggal'            => 'required|date_format:d-m-Y'
    ];
1

2 Answers 2

1

By default, you can find all message validation is defined in resources\lang\en\validation.php like this

'accepted'             => 'The :attribute must be accepted.',
'active_url'           => 'The :attribute is not a valid URL.',
'alpha'                => 'The :attribute may only contain letters.',
...

with :attribute is auto replace by name of the input ,and you can edit message in this file. But in my opinion you shouldn't do that.

Another way.

You create your form request validation php artisan make:request UserRequest

/**


* Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ];
}

/**
 * Get the error messages for the defined validation rules.
 *
 * @return array
 */
public function messages()
{
    return [
        'title.required' => 'A title is required', /** You can custom message here */
        'body.required' => 'A message is required',
    ];
}
Sign up to request clarification or add additional context in comments.

Comments

1

Laravel Framework's validation functionality supports the customization of messages in default.

Read this link; https://laravel.com/docs/8.x/validation#custom-error-messages

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.