For the below code , i don't want the password field to have the same value of currentPassword field. The desired output is to show some validation error like "password & currentPassword canot be the same" ,when one types same value for both password & currentPassword fields. To be more precise, it should work in opposite of the rule - same does.
$validator = Validator::make(
$request->all(),
[
'currentPassword' => 'required',
'password' => 'required|min:6|confirmed',
]
);
You would expect the rule to be named "NotSame", but it is not. Instead the validation rule is called different.
'password' => 'different:currentPassword'
Related
I have two fields: Email and Telephone
i want to create a validation where one of two fields are required and if one or both fields are set, it should be the correct Format.
I tried this, but it doesnt work, i need both though
public static array $createValidationRules = [
'email' => 'required_without:telephone|email:rfc',
'telephone' => 'required_without:email|numeric|regex:/^\d{5,15}$/',
];
It is correct that both fields produce the required_without error message if both are empty. This error message clearly says that the field must be filled if the other is not. You may change the message if needed:
$messages = [
'email.required_without' => 'foo',
'telephone.required_without' => 'bar',
];
However, you must add the nullable rule, so the format rules don't apply when the field is empty:
$rules = [
'email' => ['required_without:telephone', 'nullable', 'email:rfc'],
'telephone' => ['required_without:email', 'nullable', 'numeric', 'regex:/^\d{5,15}$/'],
];
Furthermore: It is recommended writing the rules as array, especially when using regex.
I want to validate a value that I got from a certain form. The value type is text. I want it to match a specific username from the database from the users table, but also to not match the current user's username.
To achieve that, I used the following validation rules:
'username' => [
'required',
'string',
'exists:App\User,username',
'different:' . auth()->user()->username
]
What I've discovered is that whenever the auth()->user()->username value includes a digit, it passes the validation even if request()->username = auth()->user()->username. Is there anything I can do to prevent this from happening?
Thanks in advance.
Use unique like -
Considering id is your user's id.
'username' => 'required|string|unique:username,id,'.auth()->user()->username,
This will check if username is unique or not except this userId.
The answer to this issue was creating own Rule::exists validation, which is shown below:
'username' => [
'required',
'string',
Rule::exists('users')->where(function ($query) {
$query->where('username', '<>', auth()->user()->username);
})
],
I solved a similar problem as follows.
$request->validate([
'email' => ['required', 'email','unique:users,email,'.Auth::id()],
'phone' => ['required', 'unique:users,phone,'.Auth::id()],
]);
I have a validator for users to update their profile, and on the same page the user can change their password. Although, I do not want to run a check if all the 3 fields (current password, new / confirmed) is empty.
Is there a way I can add in a custom check in the Validator::makeand check, or add to the validator and add the return with errors page?
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:191',
'last_name' => 'required|max:191',
'email' => 'required|email|max:191'
]);
example 3 field are empty and wouldnt be a problem, although what if they're filled out... can I append to this for check
example
if ($request->new_password) {
$validator .= array('new_password' => 'required');
}
my last solution would be to have two validators... would that work?
You can accomplish this by adding sometimes to your rule list. sometimes runs validation checks against a field only if that field is present in the input array. As an exmaple,
$v = Validator::make($data, [
'email' => 'sometimes|required|email',
]);
For more rules options you can refer to Laravel Validator Documentation.
I'm trying to create a user update validation through form, where I pass, for example 'password'=>NULL, or 'password'=>'newone';
I'm trying to make it validate ONLY if it's passed as not null, and nothing, not even 'sometimes' works :/
I'm trying to validate as :
Validator::make(
['test' => null],
['test' => 'sometimes|required|min:6']
)->validate();
But it fails to validate.
Perhaps you were looking for 'nullable'?
'test'=> 'nullable|min:6'
Though the question is a bit old, this is how you should do it. You dont need to struggle so hard, with so much code, on something this simple.
You need to have both nullable and sometimes on the validation rule, like:
$this->validate($request, [
'username' => 'required|unique:login',
'password' => 'sometimes|nullable|between:8,20'
]);
The above will validate only if the field has some value, and ignore if there is none, or if it passes null. This works well.
Do not pass 'required' on validator
Validate like below
$this->validate($request, [
'username' => 'required|unique:login',
'password' => 'between:8,20'
]);
The above validator will accept password only if they are present but should be between 8 and 20
This is what I did in my use case
case 'update':
$rules = [
'protocol_id' => 'required',
'name' => 'required|max:30|unique:tenant.trackers'.',name,' . $id,
'ip'=>'required',
'imei' => 'max:30|unique:tenant.trackers'.',imei,' . $id,
'simcard_no' => 'between:8,15|unique:tenant.trackers'.',simcard_no,' . $id,
'data_retention_period'=>'required|integer'
];
break;
Here the tracker may or may not have sim card number , if present it will be 8 to 15 characters wrong
Update
if you still want to pass hardcoded 'NULL' value then add the
following in validator
$str='NULL';
$rules = [
password => 'required|not_in:'.$str,
];
I think you are looking for filled.
https://laravel.com/docs/5.4/validation#rule-filled
The relevant validation rules are:
required
sometimes
nullable
All have their uses and they can be checked here:
https://laravel.com/docs/5.8/validation#rule-required
if you want validation to always apply
https://laravel.com/docs/5.8/validation#conditionally-adding-rules
if you want to apply validation rules sometimes
https://laravel.com/docs/5.8/validation#a-note-on-optional-fields
if you want your attribute to allow for null as value too
I'm writing a Request Validator in Laravel, where I have the following conditions. To check if passwords given are the same, this will fail, with the message that the passwords are not the same, even though the passwords are the same.
'password1' => 'string',
'password2' => 'string|same: password1'
If I disable the validation and dd(Input::only('password1','password2'));, it will print out the following.
array:2 [
"password1" => "123"
"password2" => "123"
]
Why is the same validation not working?
Your issue is the space in your validation rule. You don't have a field named [space]password1 in your input, so the validation fails. Instead of same: password1, it should be same:password1.
'password1' => 'string',
'password2' => 'string|same:password1'
Another way that password confirmation is usually done is with the confirmed validation. Typically you have a password field and a password_confirmation field, and then the confirmed validation will validate that your password input has matching input from a *_confirmation field.
'password' => 'string|confirmed',
'password_confirmation' => 'string',