stack
stack

Reputation: 10218

How to customize error messages in Laravel?

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?

Upvotes: 7

Views: 31213

Answers (5)

Pratik Kumar
Pratik Kumar

Reputation: 66

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 ', 
        );

Upvotes: 4

Komal
Komal

Reputation: 2736

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();
}

Upvotes: 9

Akshay Deshmukh
Akshay Deshmukh

Reputation: 1292

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
    }

Upvotes: 10

Ani
Ani

Reputation: 740

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.

Upvotes: 2

Vivek V Dwivedi
Vivek V Dwivedi

Reputation: 2615

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.

Upvotes: 2

Related Questions