【问题标题】:Custom validation rule with request class not working laravel 7请求类的自定义验证规则不起作用 laravel 7
【发布时间】:2020-09-07 15:13:01
【问题描述】:

下面是我的代码;

FruitRequest.php

class FruitRequest extends Request
{

public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'name' => 'required|alpha',
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ];
}

public function messages()
{
    return ['name.required' => response("Name should be mandatory", 404),
        'name.alpha' => response("Name should be contains only letters", 404),
        'image.required' => response("Foto should be mandatory", 404),
        'image.mimes' => response('Foto should be jpeg,png,jpg,gif,svg', 404),
        'image.max' => response('Foto size should be blow 2 MB', 404),
    ];
}

}

FruitController.php

use App\Http\Controllers\Controller;
use App\Http\Requests\FruitRequest;

class FruitController extends Controller
{

public function store(FruitRequest $request)
{
    echo $request->input('name');

    //above line gives nothing to me
}

}

如果我使用extends Request 而不是extends FruitRequest,那么这给了我用户在邮递员中传递的值。我不知道为什么这个自定义请求类不起作用。我附上了屏幕截图。请帮忙....

【问题讨论】:

  • 它扩展了 Illuminate\Foundation\Http\FormRequest;不是 Illuminate\Http\Request;
  • @AlzafanChristian 这是有效的,但我想知道如果验证失败,我如何从控制器返回自定义响应,即使我都正确插入了参数,我也收到了验证消息。你对此有什么想法吗?

标签: php laravel laravel-7


【解决方案1】:

使用 FormRequest 扩展您的请求类

use Illuminate\Foundation\Http\FormRequest;

class FruitRequest extends FormRequest

更多详情请访问 laravel 官方文档:https://laravel.com/docs/7.x/validation#creating-form-requests

【讨论】:

    【解决方案2】:

    好久不用邮递员了,我正在用我的代码测试

    我正在像这样使用 FormRequest:

    use Illuminate\Foundation\Http\FormRequest;
    use Illuminate\Contracts\Validation\Validator;
    use Illuminate\Http\Exceptions\HttpResponseException;
    
    class YourRequest extends FormRequest
    {
      //this function called if Validator::make()->fails();
      //here where you can modifying your message
      protected function failedValidation(Validator $validator)
      {
        //note this only for API, for formData use \Illuminate\Validation\ValidationException($validator)
        throw new HttpResponseException(response()->json($validator->errors()->all(), 422));
        //this will get parameter attribute set from FormRequest
        //attributes() along with the error message, 
        //or $validator->errors()->all() to get messages only like my screenshot
        //or modify message with your logic
      }
    
      public function authorize() { return true; }
      public function rules() { return []; }
      public function attributes() { return []; }
      public function messages() { return []; }
    }
    

    在控制器中:

    use YourRequest;
    
    public function store(YourRequest $req)
    {
      return response($req->all())->setStatusCode(200); 
    }
    

    在您的 FormRequest 中替换 response(),只需文本:

    public function messages()
    {
        return ['name.required' => "Name should be mandatory"],
    }
    

    第二,验证alpha只接受字母,你的名字是数字, 我的代码的结果(我使用消息数组中的默认验证器消息):

    【讨论】:

    • 我插入了正确的两个字段仍然验证给出消息。我无法回显字段
    • 用正常的 Request from function 替换您的 Request,这样您就可以转储输入字段
    • 或者如果你尝试使用我的代码,在 failedValidation() dd($validator->errors()->all());
    • 我不明白你在说什么。其实我是laravel的新手。你能在你的答案中写代码吗?我想返回客户响应,例如 {"code":"404","message":"Name field is empty"}。
    • 你不是在你的问题中要求它。我的答案已编辑
    猜你喜欢
    • 2020-05-08
    • 2020-12-10
    • 2020-07-17
    • 2017-12-06
    • 2015-12-26
    • 1970-01-01
    • 2018-06-03
    • 2020-02-17
    • 1970-01-01
    相关资源
    最近更新 更多