Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

php - How To Make Validations With Requests In Laravel 8?

I am using Laravel 8. I am trying to validate inputs for creating users in the store method of my controller using requests. Store method of my user controller UserController.php

<?php

namespace AppHttpControllers;

use AppHttpRequestsUserCreateRequest;
use AppModelsUser;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;

class UserController extends Controller
{
    public function store(UserCreateRequest $request)
    {
        $user = User::create([
            'name' => $request->input('name'),
            'email' => $request->input('email'),
            'password' => Hash::make($request->input('password')),    
        ]);

        return response($user, 201);
    }

   
}

UserCreateRequest.php is the file I used to make validation. UserCreateRequest.php

<?php

namespace AppHttpRequests;

use IlluminateFoundationHttpFormRequest;



class UserCreateRequest extends FormRequest

{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:3|max:25',
            'email' => 'required|email',
        ];
    }
}

But the problem is I get this error

{
    "message": "Method Illuminate\Validation\Validator::validateRequierd does not exist.",
    "exception": "BadMethodCallException",
    "file": "/app/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
    "line": 1395,
    "trace": [
        {
            "file": "/app/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
            "line": 554,
question from:https://stackoverflow.com/questions/65871895/how-to-make-validations-with-requests-in-laravel-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks all good, but it seems you made a typo somewhere because it tries to call validateRequierd and not validateRequired. You probably wrote requierd somethere in your validation rules.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...