These are my rules in my class:
class AppointmentsController extends Controller
{
protected $rules = [
'appointment' => ['required', 'min:5'],
'slug' => ['required', 'unique:appointments'],
'description' => ['required'],
'date' => ['required', 'date_format:"Y-m-d H:i"'],
];
This is in the laravel official docs:
Sometimes, you may wish to ignore a given ID during the unique check.
For example, consider an "update profile" screen that includes the
user's name, e-mail address, and location. Of course, you will want to
verify that the e-mail address is unique. However, if the user only
changes the name field and not the e-mail field, you do not want a
validation error to be thrown because the user is already the owner of
the e-mail address. You only want to throw a validation error if the
user provides an e-mail address that is already used by a different
user. To tell the unique rule to ignore the user's ID, you may pass
the ID as the third parameter:
'email' => 'unique:users,email_address,'.$user->id.',user_id'
I tried using this in my rules:
'slug' => ['required', 'unique:appointments,id,:id'],
This indeed ignores the current row BUT it ignores it completely. What I want to accomplish is, I want it to ignore the current row only if the slug is unchanged. When it is changed to something that is already unique in another row, I want it to throw an error.
The Unique validator works like that
unique:table,column,except,idColumn
So in your case, you can do it like that:
Get the id you want to validate against, you can get it from the route or with any other way that works for you; something like that
$id = $this->route('id');
'slug' => ['required','unique:appointments,slug,'.$id],
For example we need to update contact info into Users table.
In my model User I created this static method:
static function getContactDataValidationRules( $idUserToExcept ) {
return [
'email' => 'required|email|max:255|unique:users,email,' . $idUserToExcept,
'pec' => 'required|email|max:255',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:8|max:20',
'mobile' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:8|max:20',
'phone2' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:8|max:20',
'recovery_email' => 'required|email|max:255',
];
}
and in my UsersController, into the method that update User I've:
$id = $request->input('id');
$request->validate(User::getContactDataValidationRules( $id ));
:-)
Related
I am trying to follow best practice for REST api on CRUD.
GET: users/ -> all
GET: users/:id -> one specific user.
POST: users/ -> add one
PUT: users/:id -> update specific user.
DELETE: users/:id -> delete one user.
On laravel 8 I want to validate the url :id using the validator, so I have like this on delete user:
$validator = Validator::make(['id' => $request->id], [
'id' => 'exists:users,id,deleted_at,NULL',
]);
And this way to update a user:
$validator = Validator::make(array_merge($request->all(), ['id' => $request->id]), [
'id' => 'required|exists:users,id,deleted_at,NULL',
'name' => 'required',
'surname' => 'required',
'email' => 'required|email:rfc,dns'
]);
As you can see I have to put the id on an array and/or merge with the $request->all().
There is any way in laravel to do this with the request?
I have found 3 ways by Laravel:
$request->add(['variable' => 'value']);
$request->merge(["key"=>"value"]);
$request->request->set(key, value);
But a solution for adding route params to the request before hitting the controller method would be even great.
You can update the request object on the fly and add the ID field, before you validate it, something like
$request['id'] = $id;
// note: the $id is your function's parameter name
$validator = Validator::make(array_merge($request->all()), [
'id' => 'required|exists:users,id,deleted_at,NULL',
'name' => 'required',
'surname' => 'required',
'email' => 'required|email:rfc,dns'
]);
You can do it like you are doing, but doing it with route model binding would be the way to go.
Now when you want to update a user by sending a PUT to /users/:id, and the user does not exist you will get a 422. But what you really want would be a 404.
With route model binding, Laravel will check if the model exists for you and abort with a 404 when it does not.
If route model binding is not an option, you can also just not validate the id with the validator and retrieve the user with User::findOrFail($request->input('id')), the framework will then still abort with a 404 if it can't be found.
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 am working on a Laravel project and I have the following problem related to validation.
In the past I created this validation rules (related to a new user registration form):
$rules = [
'name' => 'required',
'surname' => 'required',
'login' => 'required|unique:pm_user,login',
'email' => 'required|email|confirmed|unique:pm_user,email',
'pass' => 'required|required|min:6',
'g-recaptcha-response' => 'required|captcha',
];
In particular this rules array contains this rule:
'login' => 'required|unique:pm_user,login',
it seems to me that this last rule check if the inserted login doesn't yet exist into the pm_user table (so it ensure that not exist a row of the pm_user table having the same inserted value into the login column).
Is it? Correct me if I am doing wrong assertion.
If it work in this way now my problem is how to do the opposite thing in another set of validation rule.
In particular I have this other array of rule (defined into a class extendingFormRequest:
public function rules() {
return [
'email' => 'required|email',
'token' => 'required',
];
}
In particular I have to ensure that into the pm_user table yet exist a record having the value of the column named email that is the same of the emai field of the request.
How can I change this request to perform this validation rule?
Laravel 5.4 already has a built in validation rule for this called exists.
https://laravel.com/docs/5.4/validation#rule-exists
I think you are looking for:
'email' => 'required|email|exists:pm_user,email'
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 currently have an account create and login page. I have rules for both pages in my model and for my register my rules are like this:
public static $rules = [
'username' => 'unique:users,username',
'group_id'=>'required',
'password' => 'required'
]
So this just basically gives an error when the username already exists.
Now I want the username field only unique when the group_id inserted, already has the same username.
So basically people can have the same username if they dont have the same group_id
How do I do this? I cant seem to find out how to check in the rules if the username already exists under the same group_id. The laravel validation documentation isn't saying anything about this.
it is actually simple.... just extend the validation.
let's say your array looks like this
$input = ['name' => 'mike', 'group' => 1];
write the usual rules,
rules = ['name' => 'required|foo', 'group' => 'required'];
here foo will be the extrended rule.
Now, extend it.
Validator::extend('foo', function($attribute, $value, $parameters) use($input)
{
$count = DB::select('SELECT COUNT(*) AS c FROM users
WHERE group_id=? AND username=?',[$input['group'], $input['name']]);
return $count[0]->c;
});
that's it.
it works but i prefer to put unique constraints in database and catch the error and take action accordingly.
It is in Laravel documentation.
You just have to use extended UNIQUE validation rule:
$this->validate($request, [
'email' => 'unique:users,email_address,NULL,id,account_id,1'
]
In the rule above, only rows with an account_id of 1 would be included in the unique check.
Read more:
https://laravel.com/docs/5.2/validation#rule-distinct