Search code examples
phplaravellaravel-7

Form Request Rule in Laraval 7 Not Working


I'm using laravel 7

I have a Request that I've built but the required rule is not working. Request sends back without any error.

and dd() also not showing request data.

Function:

public function store(StoreRequest $request)
    {
        dd($request->all());
        if (!auth()->user()->can('add-users')) {
            abort(401);
        }
        try {
            $userStatus = app(CreateUser::class)->execute($request->all());
            if ($userStatus == true) {
                return redirect()->back()->with('success', 'User successfully created.');
            } else {
                return redirect()->back()->with('error', 'Oops Something went wrong!');
            }
        } catch (\Exception $ex) {
            return redirect()->back()->with('error', $ex->getMessage());
        }
    }

Request Code:

class StoreRequest 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','string','max:255'],
            'email' => ['required','string','max:255'],
            'password' => 'required',
            'organization_id' => 'required'
        ];
    }
}

If I use Illuminate\Http\Request showing the request data but not validating the data.

Any idea?


Solution

  • Please can you try this

    public function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'email' => 'required|string|max:255',
            'password' => 'required',
            'organization_id' => 'required'
        ];
    }