Laravel form request class not found - php

I'm trying to create laravel form validation, so I created a form validation with the following code. The problem is that I'm getting an error "Class App\Http\Requests\RegisterForm does not exist" when I typehint the request in controller. Any help would be much appriciated. Thanks
RegisterForm.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RegisterForm 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',
'email': 'required',
'mobile_number': 'required',
];
}
}
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\RegisterForm;
class UserController extends Controller
{
public function register(RegisterForm $request) {
}
public function login() {
}
}
Laravel version 5.7
NGINX
php7.2

the issue was caused by using colon (:) instead of => in rules array
return [
'name'=> 'required',
'email'=> 'required',
'mobile_number'=> 'required',
];

Related

Laravel: Validation Rule Ignore submitted ID in FormRequest class

I would like to ignore one of the inputs inside a FormRequest class. Previously, I've already done it on my previous Laravel project using the exact same code (The previous Laravel project was Laravel Version 7). Here is the code of my ignore validation:
class UserRequest extends FormRequest
{
protected $user;
public function __construct(Request $request)
{
$this->user = $request->route()->client;
}
public function rules()
{
return [
'email' => ['nullable', 'string', 'email', 'max:100', 'unique:users,email,'.$this->user.',user_id'],
];
}
The line code "$request->route()->client" is to get the submitted form request data (client is one of the route inside the web.php file).
But when I try this code inside the current project (Current project is Laravel 8). The results was the email was not ignored.
I guess there are something new in Laravel 8 about FormRequest? How can I solve this?
UPDATE
Here is my route (web.php) code:
Route::middleware('auth')->group(function () {
Route::resource('users', UserController::class);
And here is my users table structure:
I used the resource Controller so the route is simple.
try following way
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UserRequest 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<string, mixed>
*/
public function rules()
{
return [
'email' => [
'nullable', 'string', 'email', 'max:100',
Rule::unique('users','email')->ignore($this->route()->client,'user_id'),
],
];
}
}
and ignore method accept two params
ignore($id, $idColumn = null)
for the second column default, it treats as the id column as the primary key.if not then we should specify the column name.
so there is no need to use a constructor and all

My Laravel Controller function is not executed when adding custom request

I am new to Laravel. I decide to apply my understanding on Laravel to create a simple registration API.
This API will receive three data which are name, email, and password. These input data will be validated inside the Request file. But I found that, if I use the RegisterUserRequest $request inside my Controller file, the method inside controller file is not executed.
Here is my AuthController file:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\RegisterUserRequest;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
class AuthController extends Controller
{
public function register(RegisterUserRequest $request)
{
return response()->json([
'message' => 'Here',
]);
}
}
Here is my RegisterUserRequest file
<?php
namespace App\Http\Requests\Auth;
use Illuminate\Foundation\Http\FormRequest;
class RegisterUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'password' => 'required',
];
}
}
Here is my route
Route::group(['namespace' => 'App\Http\Controllers\Api'], function () {
Route::post('register', [AuthController::class, 'register']);
});
Here is the output show on Postman:
Because suppose the output show on Postman would be:
{
"message": "Here"
}
But it don't. So i think that the register method inside the AuthController is not executed.
Is anyone know the problem? Really Appreciated!!!
Thank you.
As you defined, the user is not authorized to make this request:
public function authorize()
{
return false;
}
Set it to true.

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 App\Http\Controllers;
use App\Http\Requests\UserCreateRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
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 App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
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,
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.

Namespace declaration statement has to be the very first statement in the script in Laravel-5

In laravel I make the TrainingRequest File for validation using
php artisan make:request TrainingRequest
FatalErrorException in TrainingRequest.php line 3:
Namespace declaration statement has to be the very first statement in the script
And I make the validation in that created file as below:-
<?php
namespace App\Http\Requests; <!--This is line number 3 -->
use App\Http\Requests\Request;
class TrainingRequest extends Request
{
/**
* 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 [
'topic' => 'required|min:3',
'sub_topic' => 'required',
'category' => 'required',
];
}
}
After I make the validation using that class by making object in controller and my controller is as below:-
<?php
namespace App\Http\Controllers;
use App\Training;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\TrainingRequest;
use Request;
class TrainingController extends Controller
{
public function store(TrainingRequest $request)
{
//store Training
//$training=Request::all();
Training::create($request->all());
return redirect('training');
}
there is a space before the starting php tag
solution just clear the space

Call to a member function fails() on a non-object in Laravel 5.1

FatalErrorException in RegistersUsers.php line 32: Call to a member function fails() on a non-object In Laravel 5
Please help to solve this problem I am thankful for who can solve this error.
The code in the RegistersUsers.php is
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
trait RegistersUsers
{
use RedirectsUsers;
/**
* Show the application registration form.
*
* #return \Illuminate\Http\Response
*/
public function getRegister()
{
return view('auth.register');
}
/**
* Handle a registration request for the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
}
Controller code and the controller name is AuthController.php
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
public function validator(array $data){}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \Illuminate\Contracts\Auth\Authenticatable
*/
public function create(array $data){}
}
Create request by using this command in terminal
php artisan make:request Name_of_request
In requests file create rules under rules() method. For example
public function rules()
{
return [
'title' => 'required|min:3',
'body' => 'required',
'tag_list' => 'required',
'image' => 'required|mimes:jpeg,jpg,png,gif'
];
}
After creating rules add the class on Controller like below.
public function store(ArticleRequest $request){
//Your codes
}
ArticleRequest is the request class and $request is the post object.
In you Auth Controller to need to include 'use Validator;'.
Also your validate and create methods are empty. Those methods should look something like this:
public function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:8',
]);
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}

Categories