I'm trying to use the RegisterController in Laravel, but I can't get the Validator to work. I don't understand what the problem is, because it should just take an array and validate it.
When I try to send a JSON with the right fields to the register route, I get this error:
BadMethodCallException: Method validate does not exist. in file /home/deb85528n3/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php on line 96
Below is my code:
protected function validator(array $data)
{
$validator = Validator::make($data,
[
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer',
'lat' => 'required',
'lon' => 'required',
]);
echo $validator->errors();
if ($validator->fails())
{
return response()->json(['errors'=>$validator->errors()]);
}
if ($validator->passes())
{
$response = "validator passed";
return response()->json($response);
}
}
I also tried using the Validator in a different way:
public function validator(Request $request){
$validator = Validator::make($request->all(), [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
]);
}
But then I get this error:
Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to App\Http\Controllers\Auth\RegisterController::validator() must be an instance of App\Http\Controllers\Auth\Request, array given, called in /home/deb85528n3/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 31 in file /home/deb85528n3/app/Http/Controllers/Auth/RegisterController.php on line 103
Edited to include the whole RegisterController:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
$validator = Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
]);
echo $validator->errors();
if ($validator->fails())
{
return response()->json(['errors'=>$validator->errors()]);
}
if ($validator->passes())
{
$response = "validator passed";
return response()->json($response);
}
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
//maybe check if facebook login here?
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'birth_year' => $data['birth_year'],
'lat' => $data['lat'],
'lon' => $data['lon'],
]);
}
}
change your function name
use Validator;
add this in your controller,
remove this
public function validator(Request $request){
Write your validation rules in model like below
public static function rules()
{
return [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
];
}
and call this rule in your controller
$validator = Validator::make($request->all(), "Your model Name"::rules());
if ($validator->fails()) {
//throw exception
}
The register method calls the validator method and passes through an array, it then expects that an instance of \Illuminate\Contracts\Validation\Validator to be returned.
Unless you're needed to override the default responses for registering a user, you should just be able to have:
public function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'birth_year' => 'required|integer|min:4',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
]);
}
Related
I can sign up and details are then added to the database but I can't use info from the database to successfully login. It just brings back the same page with no error messages. This is the UserController code
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
class UserController extends Controller
public function getSignin()
{
return view('user.signin');
}
public function postSignin(Request $request)
{
$this->validate($request, [
'email' => 'email|required',
'password' => 'required|min:4'
]);
if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
return redirect()->route('user.profile');
}
return redirect()->back();
}
```auth file
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
Probably because you're saving your password with bcrypt, but on Auth::attempt you pass it in a plain text. Try this:
Auth::attempt(['email' => $request->input('email'), 'password' => bcrypt($request->input('password'))])
Started the project again using Laravel's built in login and register system.
as the title says..
My project was hashing passwords properly until recently I noticed that passwords of new users dont get hashed while it is supposed to be as I am using Hash::make and I used Hash on the top of the controller. Please Help...
here is my User controller
I am using laravel 5.6 if this would help...
namespace App\Http\Controllers\Auth;
use App\User;
use Hash;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectPath = '/admin';
protected function validator(array $data)
{
return Validator::make($data, [
'f_name' => 'required|string|max:255',
'user_id' => 'required|string|max:255|unique:person',
'm_name' => 'required|string|max:255',
'g_name' => 'required|string|max:255',
's_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:person',
'field_of_study'=> 'string|max:255',
'gender' => 'required|boolean',
'b_date' => 'required|date|max:255',
'type' => 'required|integer|max:3',
'job_title' => 'string|max:255',
'orgnaization' => 'required|string|max:255',
'sector' => 'boolean',
'mobile' => 'required|string|min:10',
'password' =>'required|string|min:10',
]);
}
protected function create(array $data)
{
return User::create([
'f_name' => $data['f_name'],
'user_id' => $data['user_id'],
'm_name' => $data['m_name'],
'g_name' => $data['g_name'],
's_name' => $data['s_name'],
'address' => $data['address'],
'field_of_study' => $data['field_of_study'],
'gender' => $data['gender'],
'b_date' => $data['b_date'],
'type' => $data['type'],
'job_title' => $data['job_title'],
'orgnaization' => $data['orgnaization'],
'mobile' => $data['mobile'],
'sector' => $data['sector'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
public function register(Request $request)
{
if($user=User::create($request->all()))
{
return redirect('Admin')->with('message', 'done');
}
else
{
return redirect()->back()->with('error', 'error');
}
}
}
and here is my model
protected $fillable = [
'f_name','m_name','g_name','s_name','address','user_id','field_of_study','gender','b_date','type','job_title','orgnaization','mobile','sector', 'email', 'password',
];
protected $hidden = [
'password',
];
use
if ($user = $this->create($request->all()))
instead of
if($user=User::create($request->all()))
You're not calling your controller function, you're just doing User::create. that's why none of your data is being hashed
Maybe this code help you:
$inputData = $request->all();
$inputData['password'] = bcrypt($request->password);
User::create($inputData);
the script returns an error that i don't think i included in my code.
SQLSTATE[HY000]: General error: 1364 Field 'phone' doesn't have a default
value (SQL: insert into `users` (`name`, `email`, `location`, `password`,
`steps`, `incubation_days`, `updated_at`, `created_at`) values (ilamini
Ayebatonye Dagogo, dagogo#gmail.com, Uniben Road, Ugbowo, Benin City, Nigeria,
$2y$10$aoJRS61Bn/q1eNcUFALjne8erLXD11y1.OmHhurlQJDrex73DPWJW, settings, 8,
2017-03-01 14:11:54, 2017-03-01 14:11:54))
Can someone point me to where this phone field is coming from.
Below my Register Controller Class.
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\paring_by_location;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'location' => 'required|min:5',
'name' => 'required|max:255',
'password' => 'required|min:6|confirmed',
'email' => 'required|email|max:255',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
$phDay = rand(2,8);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'location' => $data['location'],
'password' => bcrypt($data['password']),
'steps' => 'settings',
'incubation_days' => $phDay
]);
paring_by_location::create([
'name' => $data['name'],
'email' => $data['email'],
'location' => $data['location'],
]);
event(new \App\Events\UserReferred(request()->cookie('ref'), $user));
return $user;
}
}
and Below is my HomeController that i think may be Interfering with the Register Controller.
public function AccountSettings(Request $request)
{
$id = Auth::user()->id;
$user = User::findOrFail($id);
$this->validate($request, [
'account_name' => 'required|string|min:5',
'account_number' => 'required|digits:10',
'bank_name' => 'required|string|min:3',
'phone' => 'required|digits:11'
]);
$input = $request->all();
$user->update(array('steps' => 'notification'));
$update = $user->fill($input)->save();
return redirect()->route('home');
}
also is my USER MODEL TAHT has the protected field
protected $fillable = [
'name', 'email', 'password', 'location','steps','incubation_days','phone','bank_name','account_name','account_number',
];
So I want to understand why it is returning an error when i did not include the phone in the register controller
You should set nullable() or default() value for the phone field. It should look like this in migration for users table:
$table->string('phone')->nullable();
Or make the phone field required:
protected function validator(array $data)
{
return Validator::make($data, [
'location' => 'required|min:5',
'name' => 'required|max:255',
'password' => 'required|min:6|confirmed',
'email' => 'required|email|max:255',
'phone' => 'required'
]);
}
And add it to create() method:
create([
'name' => $data['name'],
'email' => $data['email'],
'location' => $data['location'],
'phone' => $data['phone']
]);
I am using Laravel 5.2,
I added a verification code input in register form,
verification code was saved in session when making it,
the question is:
How to write the validation rule of verification code in function validator,
AuthController.php
protected function validator(array $data)
{
$verification_code_session = $request->session()->get('verification_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'verification_code' => '',// How to write the validation rule?
]);
}
Add:
add an argument:Request $request like this:
protected function validator(array $data,Request $request)
{
$verification_code_session = $request->session()->get('verification_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'verification_code' => 'required|in:' . $verification_code_session
]);
error:
FatalThrowableError in AuthController.php line 56:
Type error: Argument 2 passed to App\Http\Controllers\Auth\AuthController::validator() must be an instance of Illuminate\Http\Request, none given, called in D:\wnmp\www\laravel-entrust\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 69
Where do I ought to use $this?
Maybe you could do that using the in pattern :
protected function validator(array $data)
{
$verification_code_session = \Request::session()->get('verification_code', '');
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'verification_code' => 'required|in:' . $verification_code_session
]);
}
I have a protected function that I want to use to check my login validation. However I am getting the error
Call to undefined method Illuminate\Database\Query\Builder::loginValidation
Do I have to write a model for my function?
Login Validation:
protected function loginValidation($data)
{
$rules = array(
'fname' => 'required|max:255',
'lname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
);
return Validator::make($data, $rules);
}
getLoginCredentials
protected function getLoginCredentials(Request $request)
{
$validator = User::loginValidation(Request::all());
if($validator->passes())
{
return[
'email' => Request::input('email'),
'password' => Request::input('password'),
'type' => 1
];
return true;
}else{
return redirect()->back()->withErrors();
}
}
I guess the user model can not see your function. Do composer dump-autoload to see if the changes will reflect. if it does not try a new name and see if it will work.