How do I customize the Laravel registration form error message? - laravel

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'
]
);
}

Related

How to pair two arrays input validation in Laravel?

I have a form request with rules
return [
'brand_id' => ['required', 'integer'],
'color_id' => ['required', 'integer'],
'name' => ['required', 'max:255', 'string'],
'capital' => ['required', 'string'],
'price' => ['required', 'string'],
'size' => ['required', 'string', 'max:255'],
'incoming.*' => ['required', 'integer', 'gte:outgoing.0'],
];
I want to pair the validation by its each index, it is possible to do that in Laravel?
I ended up with this way
foreach ($this->incoming as $key => $value) {
$incoming["incoming.{$key}"] = ['required', 'integer', "gte:outgoing.{$key}"];
}
return array_merge([
'brand_id' => ['required', 'integer'],
'color_id' => ['required', 'integer'],
'name' => ['required', 'max:255', 'string'],
'capital' => ['required', 'string'],
'price' => ['required', 'string'],
'size' => ['required', 'string', 'max:255'],
], $incoming);
I would suggest a bit more elegant way:
return array_merge(
[
'brand_id' => ['required', 'integer'],
'color_id' => ['required', 'integer'],
'name' => ['required', 'max:255', 'string'],
'capital' => ['required', 'string'],
'price' => ['required', 'string'],
'size' => ['required', 'string', 'max:255'],
],
...array_map(
fn ($key) => ["incoming.{$key}" => ['required', 'integer', "gte:outgoing.{$key}"]],
array_keys($this->incoming),
)
);

Get a session element in the view with 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?

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

How to validate a form field based on the data from another one? - Laravel 6

I have a form which contains the following fields:
n_census (provides information about the total population from a town or city)
n_participants (provides information about the number of participants in an election process according to n_census)
I would like to validate n_participants according to n_census because it's not possible to have more n_participants than n_census.
The code I use to validate both fields (and others I have but not relevant for this issue) is:
protected function validator(array $data)
{
$customMessages = [
'required' => ':attribute es obligatori',
'min' => ':attribute cal que siga, com a mínim, :min caracters',
'max' => ':attribute cal que siga, com a màxim, :max caracters',
'string' => ':attribute cal que siga una cadena de caracters',
'int' => ':attribute cal que siga un enter',
'email' => ':attribute es un email',
'confirmed' => 'No has confirmat el correu electrònic'
];
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'dni' => ['required', 'string', 'min:9', 'max:9'],
'surname1' => ['required', 'string', 'max:50'],
'surname2' => ['required', 'string', 'max:50'],
'v1' => ['required', 'string', 'max: 100'],
'v2' => ['required', 'string', 'max: 100'],
'district' => ['required', 'int'],
'section' => ['required', 'int'],
'chair' => ['required', 'string'],
'municipality' => ['required'],
'province' => ['required'],
'n_census' => ['required', 'int'],
'n_participants' => ['required', 'int', 'max: 50']
], $customMessages);
}
Is it possible to use Validator::make() to do that?
Use this
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'dni' => ['required', 'string', 'min:9', 'max:9'],
'surname1' => ['required', 'string', 'max:50'],
'surname2' => ['required', 'string', 'max:50'],
'v1' => ['required', 'string', 'max: 100'],
'v2' => ['required', 'string', 'max: 100'],
'district' => ['required', 'int'],
'section' => ['required', 'int'],
'chair' => ['required', 'string'],
'municipality' => ['required'],
'province' => ['required'],
'n_participants' => ['required', 'int', 'max: 50'],
'n_census' => ['required', 'int','gte:n_participants'],
], $customMessages);
added gte:n_participants in condtion
https://laravel.com/docs/6.x/validation#rule-gt

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'],

Resources