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),
)
);
Related
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
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
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'],
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'
]
);
}
in laravel validation (registering) i want to compare one of the fields with a php variable (it should be equal with that)
how can i do this?
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
}
You can do it for example for name field like this:
$variable = "something"
return Validator::make($data, [
'name' => [
'required',
Rule::in([$variable]),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Remember to import Rule Class (use Illuminate\Validation\Rule;)
You can get more info in: https://laravel.com/docs/5.4/validation#rule-in
EDIT
As suggested by #patricus, you can also concatenate the variable
$variable = "something"
return Validator::make($data, [
'name' => 'required|in:'.$variable,
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
EDIT2
If you have a variable that is an array:
$variable = ['one','two'];
return Validator::make($data, [
'name' => 'required|in:'.implode(",", $variable),
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Or
$variable = ['one','two']
return Validator::make($data, [
'name' => [
'required',
Rule::in($variable),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);