I want to strengthen Laravel's standard password validation: [A-Z] [a-z] [0-9]. I want to accept input with more than eight special characters and I need maximum support for Laravel Hash.
php artisan make:rule StrongPassword
What should I do after this?
You can add wathever validation rules you want in the Laravel RegisterController. Just update the rule for password with the extra limitations:
https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php#L49
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'], // <-- This one
]);
Related
Is it possible to add validation if another validation passed for example I have the following validation rules
return [
'name' => ['required', 'string', 'min:3', 'max:30'],
'password' => ['required', 'min:6'],
'photo' => ['imageable'],
'business_uuid' => ['required', 'uuid', 'exists:businesses,uuid'],
];
and I would like if the business_uuid validation passed to add validation for the phone number
'phone' => ['nullable', 'phone:'.$countryCode],
notice that I'm the country code will be brought from the business That's why I'm I'd like to add it if the validation passed
I have a user table where there are two fields email and password
$validator = Validator::make($request->all(),[
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:5', 'confirmed','min:8'],
'password_confirmation' => ['min:8']
]);
I'm using unique field to check whether email is unique but i want to show message user already exist only if email already exist and password field not null, because i have a case if user is login through social media account then password is not saved and is null, so when user is trying to register through normal account then i want to update user fields.
Any Suggestion Thanks
You can have a more advanced unique rule:
$validator = Validator::make($request->all(),[
'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->where(function ($q) {
$q->whereNotNull('password');
}) ],
'password' => ['required', 'string', 'min:5', 'confirmed','min:8'],
'password_confirmation' => ['min:8']
]);
In my form, I have two fields pass and pass_repeat. On the browser side, I validate it using javascript. On the server-side I want to check whether they are the same. How can I do it using request()->validate() method?
I can check the equality with php, however, I'd like to know if it is possible with request()->validate() or not.
Even keywords for further search are appreciated.
P.S. My try:
$result = request()->validate([
'email' => ['required', 'email'],
'password' => ['required', 'string', 'max:30', 'min:8'],
'password_repeat' => ['required', 'string', 'max:30', 'min:8']
]);
if ($result['password' != $result['password_repeat'] || $result['password_repeat'] == '' || $result['password'] == '')
{
abort();
}
Thanks.
just change the name of input 'password_repeat' to 'password_confirmation' in your form and use validator 'confirmed'
$result = request()->validate([
'email' => ['required', 'email'],
'password' => ['required', 'string', 'max:30', 'min:8','confirmed'],
]);
you are doing this in a wrong way, laravel has confirmed rule that can be use for confirmation purposes, you should add pass_confirmation input and pass it to controller via form and use confirmed rule for pass input.
I want to remove validation on the name in laravel 6 on creating a new user. The user is created successfully but when I enter a name with space or capital letters, the login page opens up. But if I remove all spaces from the name everything works fine with the following code.
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'],
]);
}
protected function create(array $data)
{
$username = slugify($data['name']) . "-" . mt_rand(10000, 99999);
return User::create([
'name' => $data['name'],
'username' => $username,
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
blade.php code
https://codeshare.io/5e1kX7
Try
//...
'name' => ['required', 'string', 'regex:/^[a-zA-Z0-9\s]+$/', 'max:255'],
//...
I did it by adding regex in the validation
'name' => ['required', 'string','regex:/^[\pL\s\-]+$/u', 'max:255'],
I'm creating two different registration (for admin and user). But the username->unique in the validator is only for table users.
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => ['required', 'string', 'max:255', 'min:3'],
'middlename' => ['required', 'string', 'max:255', 'min:3'],
'lastname' => ['required', 'string', 'max:255', 'min:3'],
'username' => ['required', 'string', 'max:255', 'min:3', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);
}
validate unique username for user registration from the users table and validate unique username for admin registration from the admins table.
You could use the route name or something similar, from the current request to determine which rules have to get used.
protected function validator(array $data)
{
$route = Request::route()->getName();
$rules = [
'firstname' => ['required', 'string', 'max:255', 'min:3'],
'middlename' => ['required', 'string', 'max:255', 'min:3'],
'lastname' => ['required', 'string', 'max:255', 'min:3'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
];
// whatever your route name or identifier to differentiate between
// user and admin registration may be.
if ($route === 'registration.user') {
$rules = $rules + [
'username' => ['required', 'string', 'max:255', 'min:3', 'unique:users'],
];
}
return Validator::make($data, $rules);
}
A Validator should only validate data, it's bad to couple it with concepts about routes etc...
I would create a custom Validator class which will take a table name as parameter.
Then you can instantiate that validator with the correct table parameters in your controller (which will be aware about route, request etc...)