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
]);
}
Related
How to check if user exists in my table or redirect back with a message using Laravel 5.6?
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given, called in C:\wamp\www\zainsurgalt\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 35
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
else{
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
I Added my Create method here
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Niigem;
use Validator;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data){
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigems',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
You need to return something when the user is created.
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return ........ you need to return something here
} else {
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users|exists:niigems',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data)
{
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
I see your are trying to create a new user if they exist in the niigem table. To do it the Laravel way, you have to validate using Laravel's validation class. So, this should work:
protected function create(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigem',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Another way is:
Niigem::firstOrCreate(['email' => 'dummy#domain.com'], ['name'=> $data['name'], 'password' => bcrypt($data['password'])]);
Please use this query you can use both condition as per your need:
$user = Niigem::where('email', '=', Input::get('email'))->first();
if ($user === null) {
// user doesn't exist
}
if ($user !== null)
{
// user doesn't exist
}
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',
]);
}
I am creating an admin panel where admin can create an user. I have used laravel default registration system. I have modified the RegisterController little. The user is being created successfully. But the problem is after creating user the admin is getting logged out. What is the problem?
Here are my files. I am using laravel 5.3
web.php
Route::group(['middleware' => 'auth:admin'], function () {
Route::get('/admin/user/register','Auth\CreateUserController#showRegistrationForm');
Route::post('/admin/user/register','Auth\CreateUserController#register');
});
CreateUserController
class CreateUserController extends Controller
{
use RegistersUsers;
protected function redirectTo()
{
if(Auth::guard('admin')->check()) return '/admin/index';
else if(Auth::guard('employee')->check()) return '/employee/dashboard';
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'district' => 'required',
'gender' => 'required',
'mobile' => 'required',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'district' =>$data['district'],
'mobile' => $data['mobile'],
'gender'=>$data['gender'],
]);
}
}
I am trying to register users in my Laravel app. I only want to register users who are already in the employees table. How do I do it?
protected function validator(array $data) {
$names = \App\ Employee::all() - > lists('name') - > toArray();
return Validator::make($data, [
'name' => 'required|max:255|in_array:names',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
You do not have to make a request to your Employee table, you can achieve this using the validation rule exists :
exists:table,column
The field under validation must exist on a given database table.
PHP
protected function validator(array $data) {
return Validator::make($data, [
'name' => 'required|max:255|exists:employee,name',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
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.