简体   繁体   English

在通过 Laravgel API 使用请求验证 class 时,对 Z37A62786688DAFFD29 上的成员 functionvalidated() 的错误调用

[英]While using request validation class through Laravgel API, Error Call to a member function validated() on null

I am using Laravel Request validation class to validate input through API Reference我正在使用 Laravel 请求验证 class 来验证通过 API 参考的输入

I implemented as it is mentioned in documentation and got the following error:我按照文档中提到的方式实现并收到以下错误:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function validated() on null Symfony\Component\Debug\Exception\FatalThrowableError:在 null 上调用成员 function 验证()

The only difference I feel that they submit a form from a web and I am making a request from postman.我觉得唯一的区别是他们从 web 提交表格,而我正在向 postman 提出请求。

It's been a couple of hours, no clue yet.已经几个小时了,还没有任何线索。 any help, please?有什么帮助吗?

use Illuminate\Foundation\Http\FormRequest;

class UserTypeRequest extends FormRequest
{        
  public function authorize()
  {
    return true;
  }

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

Calling validation in controller:在 controller 中调用验证:

$this->user_type_request = new UserTypeRequest();
$this->user_type_request->validated();

It should be validation input filed coming from the api, instead its showing null in lavatorial它应该是来自 api 的验证输入文件,而不是在厕所中显示 null

Try and pass the form request via Dependancy injection:尝试通过依赖注入传递表单请求:

public function store(UserTypeRequest $request)
{
    $var = $request->validated();

    ...
}

Creating a new form request probably is not validating the previous request (or getting the validation response from the previous request).创建一个新的表单请求可能不会验证前一个请求(或从前一个请求中获取验证响应)。

You don't need to call the validate function if you're passing the Form Request class in the controller function, just proceed and your request is automatically validated如果您在 controller function 中传递表单请求 class,则无需调用validate函数,只需继续并自动验证您的请求

public function controllerMethod(\App\Http\Requests\UserTypeRequest $request) // Just pass it here
{
  // Data is already checked
  $data = $request->validated();
}

Hope this helps希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 laravel 通过 Signalwire api 发送短信时出现错误“调用成员 function send() on null” - getting error "Call to a member function send() on null" while sending sms through Signalwire api using laravel 呼叫会员 function validated() on null (Unit testing Laravel 9) - Call to a member function validated() on null (Unit testing Laravel 9) osiset / laravel-shopify 错误调用 null 上的成员 function api() - osiset / laravel-shopify Error Call to a member function api() on null 使用 laravel orm 时出现错误“呼叫成员 function count() on null” - error “Call to a member function count() on null” when using laravel orm 在 Laravel 中使用背包时,调用成员 function hasAccessOrFail() on null 错误 - Call to a member function hasAccessOrFail() on null error when using backpack in Laravel Laravel/React - 在 Controller 中创建不同的 FormRequest 时,“在 null 上调用成员 function 验证()” - Laravel/React - “Call to a member function validated() on null” when creating a different FormRequest in Controller 登录并显示错误消息:“调用成员函数prepare()为null” - login with error 'Call to a member function prepare() on null' ModelFactory:错误:在null上调用成员函数connection() - ModelFactory: Error: Call to a member function connection() on null 使用 Laravel 在 null 上调用成员函数 getCLientOriginalExtension() - Call to a member function getCLientOriginalExtension() on null using Laravel 在 null 上调用成员函数 hasRole() 出错 - getting error Call to a member function hasRole() on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM