Validiation check in Laravel php - php

I'm trying to write a validation check in PHP Laravel for a username field with the functionality to let the user know what went wrong. I have a couple of if statements with regular expression checks but it won't work. The requirements of the regular expression are: can't start with a ".", No more than 1 "." in a row, No capitals, Only a-z, No special characters. So for example like this "user.name" would be valid, but things like "username." or ".username" would all be invalid.
So far I got this:
$oValidator = Validator::make(Input::all(), [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
'username' => 'required',
'password' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email'
]);
I want to give feedback for the mistakes that user makes, example: user input is ".username", program feedback should be "Dot in front of string is not allowed".

All you have to do is to include a custom message for your validation.
$this->validate($request, [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
], ['regex' => 'Username cannot start with period(.), etc...]);
Your code should look like this. Please remember regex custom message will apply too all of these fields instead of just username so I would separate username validation like above.
$oValidator = Validator::make(Input::all(), [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
'username' => 'required',
'password' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email'
], ['regex' => 'Username cannot start with period, etc...']);

Related

Laravel validation username must not start with specific string

I want to register user but I want to put validation rule on username that username should not start with special characters and also should not start with web. I found regex that work fine special characters but detect the string it give me error of Invalid format
return [
'username' => [
'required',
'regex:/^\S*$/u',
'regex:/^[_]?[a-zA-Z0-9]+([_.-]?[a-zA-Z0-9])*$/',
'unique:users'
],
'full_name' => 'required',
];
Try this?
return [
'username' => [
'required|
not_regex:/^[web_-][a-z_\-0-9]*/i|
regex:/^[A-Za-z_ \-0-9]+$/u|
unique:users'
],
'full_name' => 'required',
];
This should work as you want it to
return [
'username' => [
'required',
'regex:/^(?!web)[a-zA-Z]\w+$/',
'unique:users'
],
'full_name' => 'required',
];
Here's a regex101 demo for you to test it out.

How validate param only if user is guest in laravel?

I want validate name and email (do required) only if user is guest. How I can do it correctly?
I tried paste the if condition. But I don't think this is correct:
if (Auth::check()) {
$request->validate([
'name' => 'required|string',
'email' => 'required|string',
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
} else {
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
}
I can't think of a validation rule that'll do this, but you can at least get the code a little cleaner by skipping the else condition, for instance like so:
$rules = [
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
];
if (Auth::guest()) {
$rules['name'] = 'required|string';
$rules['email'] = 'required|string';
}
$request->validate($rules);
Another option would be to simply check the requirement within the array and just skip the field in the validation for authorised users. It's shorter, but not quite as clean:
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
'name' => Auth::guest() ? 'required|string' : '',
'email' => Auth::guest() ? 'required|string' : '',
]);
By the way, I would recommend validating the email address as an email address: https://laravel.com/docs/5.7/validation#rule-email

Laravel Auth Check For An Organization

when a user tries to register I require them to enter an organization ID, I want that organization ID to be checked against my Organization table and see if it exists. If it exists then register the user and if it fails then return an error message. I've been looking around online and couldn't personally find anything like this. If anybody could help, I'd greatly appreciate it.
I am using Laravel 5.6 with the default auth.
Validator:
return Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'org_id' => 'required|string|max:16',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
User Create:
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'org_id' => $data['org_id'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'is_active' => 1
]);
You're looking for the exists rule of Laravel's Validation:
'org_id' => 'required|string|max:16|exists:organizations,id',
The rule is essentially
exists:{table},{column?}
Where table is required, and column is optional, generally used if the name (in this case org_id) is different from the column you want to compare.
For full details, check the Documentation.

How can I check i 2 fields of a form have the same content using a rule array of laravelcollective/html validator?

I am pretty new in Laravel and I have the following problem.
I have this method that handle the submit of a form:
public function store(Request $request) {
Log::info('store() START');
$data = Input::all();
Log::info('INSERTED DATA: '.implode("|", $data));
$rules = array(
'name' => 'required',
'surname' => 'required',
'login' => 'required',
'email' => 'required|email',
'emailConfirm' => 'required|email',
'pass' => 'required',
'passlConfirm' => 'required',
'g-recaptcha-response' => 'required|captcha',
);
$validator = Validator::make($data, $rules);
if ($validator->fails()){
return Redirect::to('/registration')->withInput()->withErrors($validator);
}
else{
// Do your stuff.
}
}
As you can see it contains a $rules array containing the validation rules.
It works pretty fine but I want also perform the following 2 checks:
The email field have to contains the same text than the emailConfirm field.
The pass field have to contains the same text than the passConfirm field.
Can I implement this kind of check into the $rules array?
Actually Laravel comes with the validation rule called confirmed. You will need to change your fields to email_confirmation and pass_confirmation and add confirmed rule to email and pass fields.
As answered by #Mariusz, Laravel come up with value comparison rule as confirmed rule. To acheive your output you should write,
$rules = array(
'name' => 'required',
'surname' => 'required',
'login' => 'required',
'email' => 'required|confirmed|email',
'pass' => 'required|confirmed',
'g-recaptcha-response' => 'required|captcha',
);
As you have mentioned, you are using laravelcollective/html, then you have to also add following code to generate confirmation fields
echo Form::email("email_confirmation",null, $attributes = []);
echo Form::password("pass_confirmation",null, $attributes = []);

Laravel 5.3 check if email exists only if password field is filled in

My application uses the standard validator, and my form makes the user provide an email address. They may continue as a guest, but if they do want to create an account; the only thing they will have to provide is a password and in combination with that email address will create the user account.
However, my issue is I am not sure how to use the validator exists only if the password field has been filled in.
$this->validate($request, [
'first_name' => 'required',
'email' => 'required|confirmed|email',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]);
I could do a check and return redirect with an error message, but I'd prefer to go through the validator if I can.
The simplest solution is to put your validation rules into an array then perform your desired check. So if the user checked the "account creation" checkbox, add the rules.
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]
if ($request->input('acount') == 1) {
$rules['email'] = 'required|confirmed|email'
}

Categories