fulinqingjie/app/Http/Requests/ContactRequest.php

58 lines
2.0 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use App\Rules\CaptchaRule;
use Illuminate\Validation\Rule;
class ContactRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|max:20',
'phone' => 'required|string|regex:/^1[3-9]\d{9}$/|unique:contacts,phone',
'captcha' => ['required', 'string', new CaptchaRule(request()->input('key'))],
'company' => 'required|string|max:50',
'type' => ['required', 'integer', Rule::exists('keywords', 'id')->where('parent_key', 'contact_types')],
'content' => 'required|string|max:255',
'key' => 'required|string|max:255',
];
}
public function messages()
{
$messages = [
'name.required' => '姓名不能为空',
'name.max' => '姓名最大不能超过20长度',
'phone.required' => '手机号不能为空',
'phone.regex' => '手机号格式不正确',
'phone.unique' => '该手机号已提交过询价,请耐心等待工作人员联系',
'company.required' => '公司不能为空',
'company.max' => '公司名称最大不能超过50长度',
'content.required' => '需求描述不能为空',
'content.max' => '需求描述最大不能超过200长度',
'captcha.required' => '请输入验证码',
'type.required' => '请选择业务需求',
'type.exists' => '业务需求类型不存在',
'key.required' => '非法请求',
];
return $messages;
}
protected function failedValidation(Validator $validator)
{
$error = $validator->errors()->all();
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
}
}