7

Here is my code:

public function store(Request $request){ $this->validate($request, [ 'name' => 'required', 'email' => 'required|email', 'content' => 'required|min:10', ]); $new_array = array(); $new_array['name'] = $request->name; $new_array['email'] = $request->email; $new_array['content'] = $request->content; contact::create($new_array); return back(); } 

Now I need to set some new error-messages for those filters (ex: required, email, ..).

Currently, this is the default error message for required:

The name field is required.

How can I change it?

0

5 Answers 5

10

You can create a Laravel Request for handling the validations using following command

php artisan make:request RequestName 

You will find the file in app\Http\Request

Inside the file you will find the rules() function where you can mentioned all rules

 public function rules() { return [ 'name' => 'required', ]; } 

You can create message() function for custom message like

 public function messages() { return[ 'name.required' => 'The name field is required.' ]; } 

Add this request in controller method

public function methodName(RequestName $request) { //Your code } 
Sign up to request clarification or add additional context in comments.

4 Comments

and then how can I apply RequestName ?
When I submit the form, I am getting: ReflectionException Class App\Http\Controllers\SociallinkRequest does not exist. If I add "use App\Http\Requests\SociallinkRequest;" in the App\Http\Requests\SociallinkRequest.php, at the top, then I am getting: HttpException This action is unauthorized. I am on LV 5.4.
add use App\Http\Requests\SociallinkRequest in your controller
I was using that. Resolved the issue by returning true from authorize() instead of the default false. Everything started working.
9

Crate rules

public function user_registration_rules(array $data) { $messages = [ 'full-name.required' => 'Please enter full name', 'address.required' => 'Please enter address' ]; $validator = Validator::make($data, [ 'full-name' => 'required|min:5|max:70', 'address' => 'required' ], $messages); return $validator; } 

Use in controller like this

$validator = $this->user_registration_rules($request_data); if($validator->fails()) { return redirect()->back()->withErrors($validator)->withInput(); } 

1 Comment

should I write user_registration_rules() function in where? in the request folder?
4

Follow this : View :

<div class="col-md-3"> {{Form::label('Deposit Amount')}} {{Form::text('deposit_amount',Input::old('deposit_amount'),array('class'=>"form-control"))}} <span class="error">{!!$errors->first('deposit_amount')!!}</span> </div> 

Controller

 use App\Http\Requests; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; use DB; use Hash; use Auth; use App\PaymentModel; use Redirect; use Response; class TransactionController extends Controller { public function payment(Request $request){ $validator = Validator::make(Input::all(), PaymentModel::Rules(), PaymentModel::$message); if ($validator->fails()) { return \Redirect::back()->withErrors($validator)->withInput(); } else { $deposit_amount=Input::get("deposit_amount"); $obj = new PaymentModel(); $obj->deposit_amount=$deposit_amount; if ($obj->save()) { Session::flash('success', 'Saved Successfully !!'); } else { Session::flash('error', 'Some thing went wrong!!'); } } return Redirect::action('TransactionController@payment'); } } 

Model

 public static function Rules(){ $rules= array( 'deposit_amount'=>'required|numeric', ); return $rules; } public static $messages=array( 'deposit_amount.required'=>'Please Enter Amount ', 'deposit_amount.numeric'=>'Deposit Amount Must be a number ', ); 

Comments

2

From the docs: https://laravel.com/docs/5.3/validation#customizing-the-error-messages

You may customize the error messages used by the form request by overriding the messages method. This method should return an array of attribute / rule pairs and their corresponding error messages:

/** * Get the error messages for the defined validation rules. * * @return array */ public function messages() { return [ 'title.required' => 'A title is required', 'body.required' => 'A message is required', ]; } 

And this is just a copy paste from the docs. You should refer to laravel docs, they are one of the best laid out docs.

4 Comments

well I'm newbie in Laraver, so I don't know how should I use your code. also I seen that before in the documentation, but still I don't know how exactly should I implement it.
You might want to go through this free course on laracasts. laracasts.com/series/laravel-5-fundamentals
add your code to the request class which you generate through: php artisan make:request MyRequest then go to your controller and replace Request with MyRequest in the parameter of store(Request $request) method or whatever
OK, but where exactly should it be placed?
2

You can modify the validator like this:

$validator = Validator::make($request, [ 'name' => 'required', 'email' => 'required|email', 'content' => 'required|min:10', ]); 

Now you will be able to catch the validation errors with this :

if ($validator->fails()) { $error_message = $validator->errors()->all(); // Write Custom Validator Error Message according to the $error_message found. } 

Laravel Validator gives pretty comprehensive error messages anyway.

2 Comments

Can you please assign a error message as test? for example this message should be for required filter: you must write something in this input
Just return whatever you require from that. if ($validator->fails()) { return "you must write something in this input"; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.