验证永远无效

时间:2018-11-12 12:53:51

标签: laravel

我使用验证规则创建了Request类:

class RegistrationRequest extends Request{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
          'number' => 'required|regex:/(01)[0-9]{9}/',
          'test' => 'required'
        ];
    }
}

但是验证永远不会在控制器中起作用。

public function register(RegistrationRequest $request)

  return false;
}

1 个答案:

答案 0 :(得分:2)

您应在请求类中加入FormRequest

use Illuminate\Foundation\Http\FormRequest;

class RegistrationRequest extends FormRequest{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
          'number' => 'required|regex:/(01)[0-9]{9}/',
          'test' => 'required'
        ];
    }
}

文档:https://laravel.com/docs/5.7/validation#form-request-validation