5

Let me show first my code. Here is my controller function code

public function save(Request $request) {
    try {
        $this->validate($request, Venue::rules()); // Validation  Rules 
        $venue = Venue::saveOrUpdate($request);
        if($venue !== false) {
            if($request->get('continue', false)) {
                return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
            } else {
                return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
            }
        } else {
            return back()->with('error', "Unable to save venue")->withInput();
        }

    } catch (\Exception $ex) {
        return back()->with('error', "Unable to save venue")->withInput();
    }
}

Here is my model function code

public static function rules($id = '') {
    return [
        'name' => 'required|string|max:255',
        'logo' => 'required',
        'status' => 'required|string|in:' . implode(",", Venue::STATUSES),
        'venue_type_id' => 'required|string|not_in:0',
         'client_id' => 'required|string|not_in:0',
    ];
}

So now when i submit form validation show message. I want to change this message.How can i do this.

Let me show my form with validation message : enter image description here

1

4 Answers 4

9

You may customize the error messages used by the form request by overriding the messages() method. Add custom messages on your Venue class as follows-

public static function messages($id = '') {
return [
    'name.required' => 'You must enter your name',
    'logo.required' => 'You must upload logo',
    'key.rules' => 'your messages'
];

And on your controller add messages as third parameter like-

$this->validate($request, Venue::rules(), Venue::messages());
Sign up to request clarification or add additional context in comments.

6 Comments

quick question 'client_id' => 'required|string|not_in:0' is id when i set ` 'client_id.required' => 'Select Client',`its not working.
try dd(Input::all()) before validation and see what you get as client_id
"client_id" => "0" gt this. when client not selected.
then its not empty surely, that's why is not having required validation error
is there any way to set that if id is 0 then count as null in custom message where we define name.required like client_id.required.somethin..like that ?
|
9

You can custom the validation message by ,

Go to the resources->lang->en->validation.php

and here you see,

'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

Edit those as per your need.

2 Comments

is it possible to add new custome message on rules which i created on model.
Gt the answer.thanx
7

This is the way I go about this and could serve as a guide. From your form, you've got basically 4 input fields and lets assume they are named name, client, logo and venue_type. The function in your controller that validates the form request can be like below:
N.B - you should put - use Validator; use Illuminate\Http\Request; - at the top of your class

public function validateFormRequest($request){
    try
    {

        //specify your custom message here
        $messages = [
          'required' => 'The :attribute field is required',
          'string'    => 'The :attribute must be text format',
          'file'    => 'The :attribute must be a file',
          'mimes' => 'Supported file format for :attribute are :mimes',
          'max'      => 'The :attribute must have a maximum length of :max',
        ];


        $validator = Validator::make($request->all(), [
              'name' => 'required|string|max:75',
              'client' => 'required|string|max:75',
              'logo' => 'required|file|mimes:jpeg,png,jpg,gif',
              'venue_type' => 'required|string',
          ], $messages);

        if($validator->fails()){ 
            // Validation Failed..log errors or Return Errors to view/blade
        } else{ 
            // Validation passed..Return true or positive info. i.e request can be saved
        }

    }catch (Exception $ex){
        //Log your errors or return some error message to your view/blade
    }
}

Comments

3

you can add custom errors like this.

$validation->errors()->add('error_input', 'error text');

return redirect()->back()->withInput()->withErrors($validation);

or

return redirect()->back()->withInput()->withErrors(['error_input'=> 'error text');

4 Comments

you can add this error to your validation and return redirect()->back()->withInput()->withErrors($validation);
can someone post editable code ? as i gt error Undefined variable: validation
so if i have a 15 fields in my form ..so all i need to set on return ?
use Illuminate\Support\Facades\Validator; $validation = Validator::make($request->all(), [ 'user_name' => 'required', 'user_last_name' => 'required', 'user_phone' => 'required', 'user_email' => 'required|email', 'user_agreement' => 'required', 'address' => 'required', 'quantity' => 'required|integer|min:1|max:1000', ]); this is custom validator example you first use this and then add your custom validator

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.