如何在Laravel中自定义错误消息?

时间:2016-10-19 07:09:59

标签: php laravel

这是我的代码:

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

现在我需要为这些过滤器设置一些新的错误消息(例如:requiredemail,..)

目前,这是required的默认错误消息:

  

名称字段是必需的。

如何更改?

5 个答案:

答案 0 :(得分:5)

Crate规则

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

在像这样的控制器中使用

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

答案 1 :(得分:3)

您可以使用以下命令

创建用于处理验证的Laravel请求
php artisan make:request RequestName

您将在app \ Http \ Request

中找到该文件

在文件中你会找到rules()函数,你可以在其中提到所有规则

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

您可以为自定义消息创建message()函数,例如

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

在控制器方法

中添加此请求
public function methodName(RequestName $request)
    {
       //Your code
    }

答案 2 :(得分:2)

您可以像这样修改验证器:

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

现在您将能够捕获验证错误:

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

Laravel Validator无论如何都会提供非常全面的错误消息。

答案 3 :(得分:2)

关注此: 查看:

<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>

控制器

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

模型

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

答案 4 :(得分:1)

来自文档:https://laravel.com/docs/5.3/validation#customizing-the-error-messages

您可以通过覆盖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',
];
}

这只是来自文档的复制粘贴。你应该参考laravel docs,它们是最好的文档之一。