Get a session element in the view with Laravel - laravel

I'm trying to implement Google Recaptcha in my user registration form.
Controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'surname' => ['required', 'string', 'max:255'],
'nickname' => ['required', 'string', 'max:255', 'min:5', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'g-recaptcha-response' => function ($attribute, $value, $fail) {
$secretKey = config('services.recaptcha.secret');
$response = $value;
$userIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$response&remoteip=$userIP";
$response = file_get_contents($url);
$response = json_decode($response);
if (!$response->success) {
Session::flash('errorCaptcha', 'Per favore controlla il reCaptcha!');
$fail($attribute . 'Google reCaptcha fallito!');
}
}
]);
}
Instead, the code in the view should show an error message if the user does not confirm the Recaptcha.
Blade
#if(Session::has('errorCaptcha'))
<span class="invalid-feedback" role="alert">
<strong>{{ Session::get('errorCaptcha') }}</strong>
</span>
#endif
In the controller I run:
dd(session::get('errorCaptcha'))
I get the value in the controller but not in the view. Can anyone help me, please?

Related

Avoid html tags in laravel string validation

I want to prevent users of using html tags in their username fields when they are registering, how can I do that?
For instance currently I have a user he registered with such username
E7JfyqxE4lsbqQ <html><img src="https://...../d28/2011/19/93045d3fb9c4.jpg" width="600" height="234" alt="bill"> </html>
I just want to allow letters and numbers (a-z, 0-9) that's all.
RegisterController
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'username' => 'required|string|max:255|unique:users|not_exist',
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
If you only want to allow alphanumeric characters, you can use Laravel's built in alpha_num validation.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'alpha_num', 'max:255'],
'username' => 'required|string|alpha_num|max:255|unique:users|not_exist',
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
You can do something like this
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'username' => [
'required',
'string',
'max:255',
'string',
'unique:users',
'not_exist',
'not_regex:<\s*a[^>]*>(.*?)<\s*/\s*a>'
],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
You can add more regex if you have more to include

laravel link a registration form to database field

I am using teams in laravel and want to add a company name field to my registration form instead of it running the standard:
'name' => explode(' ', $user->name, 2)[0]."'s Team",
I am still wanting to keep the field called name but use the input fields data, I have tried changing the method to:
protected function createTeam(User $user)
{
$user->ownedTeams()->save(Team::forceCreate([
'user_id' => $user->id,
'name' => $user['company_name'],
'personal_team' => true,
]));
}
but it doesn't work as intended. My register.blade.php is:
<div>
<x-jet-label value="{{ __('Company Name') }}" />
<x-jet-input class="block mt-1 w-full" type="text" name="company_name" :value="old('company_name')" autofocus autocomplete="company_name" />
</div>
my migration is unchanged as I am wanting to force the name through my form instead of it generating it based on the user input of name.
The complete CreateNewUser method is:
public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
])->validate();
return DB::transaction(function () use ($input) {
return tap(User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]), function (User $user) {
$this->createTeam($user);
});
});
}
/**
* Create a personal team for the user.
*
* #param \App\Models\User $user
* #return void
*/
protected function createTeam(User $user)
{
$user->ownedTeams()->save(Team::forceCreate([
'user_id' => $user->id,
'name' => $user['company_name'],
'personal_team' => true,
]));
}
}

How to send a query in a variable using laravel?

I have this function to register my users
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'apellido' => ['required', 'string', 'max:255'],
'idop' => ['required', 'string', 'max:6', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'cedula' => ['required', 'int'],
'fecha_nacimiento' => ['required', 'date'],
'fecha_ingreso' => ['required', 'date'],
'extension' => ['required', 'int'],
'movil' => ['required', 'int'],
'tel_hab' => ['required', 'int']
]);
}
I would like to send this query in a variable ($generos) to make a select
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
How could I do that?
You can pass the variable from controller to view using below ways:
Way1
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", compact("generos "));
Way2
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index", ["generos" => $generos]);
Way3
$generos = DB::table('tbl_lista_generos')
->select('id','genero')
->get();
return view("index")->with(["generos" => $generos]);
On your view file::
<select name="genero">
<option value=''>select</option>
#foreach ($generos as $genero)
<option value="{{ $genero->id }}">{{ $genero->name}}</option>
#enforeach
</select>
by the comments, you want to send the result of the query to the view. So,
In your controller function do:
return view('generos.index', [
'generos' => DB::table('tbl_lista_generos')->select('id','genero')->get()
]);
In the view:
<select class='' name='select_form_generos'>
<option value=''>select genero</option>
#foreach ($generos as $genero)
<option value='{{ $genero->id }}'>{{ $genero->genero}}</option>
#enforeach
</select>
Hope it helped!

validateUsername does not exist

when i am tried to add one text field which is called username for registration in laravel it says "Method Illuminate\Validation\Validator::validateUsername does not exist.
i am trying to be change the datatype of the column but still does not give any other response
{{ __('username') }}
#error('username')
{{ $message }}
#enderror
You are calling the validator method creator on a type that it doesn't know anything about. I'm guessing that you took something like:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
and copied the email line and replaced 'email' with 'username' in two places, yielding something like this:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'username' => ['required', 'string', 'username', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
the problem is that the second 'email' isn't doing the same thing that the first one is. The first 'email' is your field name. The second one, however, is a validation type to ensure that the value is actually a valid email (e.g., user#mail.com). The problem, then, is that the validator doesn't know anything about a username validation type, and so when it tries to construct that method, it fails.
What you need to do is just get rid of your second reference to username, yielding this:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
Please post your validation rules. This can either you controller code (i.e. from $this->validate or your form request class.
Alternatively, if you're using the default Laravel auth scaffolding, please post the contents of your registration controller (RegisterController).
Finally, do rule out the obvious (sorry), does your database definitly have a column in the users table titled username? For example, have you followed a guiode such as http://techiescircle.com/add-extra-fields-laravel-5-registration-form/ to add a new field to the users table?
It appears you're incorrectly attempting to run a validation rule that doesn't yet exist.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
This is the issue
Right
'username' => ['required', 'string', 'max:255', 'unique:users'],
wrong
'username' => ['required', 'string','username', 'max:255', 'unique:users'],

How do I customize the Laravel registration form error message?

protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users'],
'phone' => ['required', 'unique:users'],
'password' => ['required', 'string', 'min:6']
]);
}
I am sending the member form via Ajax. I want to show a custom message when I receive an error message.
public function messages()
{
return [
'first_name.required' => 'A title is required'
];
}
I tried the functions of these messages but it didn't work. Is there another way? Thanks. My English is bad, sorry.
You can pass the messages array as 3rd parameter in Validator::make method. Like following:
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users'],
'phone' => ['required', 'unique:users'],
'password' => ['required', 'string', 'min:6']
], [
'first_name.required' => 'A title is required'
]
);
}

Resources