![]() |
Custom validation rule, variables on Model - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Custom validation rule, variables on Model (/showthread.php?tid=90937) |
Custom validation rule, variables on Model - sjender - 05-24-2024 Hi, I have created a custom validation rule, it's like 'is_unique', but with an extra value... So I can not only check whether a name already exists in the database, but I can check if a user already exists with a specific company_id. I have added this rule to a model like this: PHP Code: protected $validationRules = [ For INSERTS this works great... But UPDATES always get through, even on duplicates. I noticed that when the 'company_id' doesn't change (but the name does), the value of 'company_name' is '{company_name}'. The user entity has a company_id, but it's not passed through to the validation rule because it's an unchanged value. Resulting a query which checks where 'company_id = {company_id}' instead of 'company_id = 4' for example. How can I include the data of the unchanged fields as well? RE: Custom validation rule, variables on Model - kenjis - 05-26-2024 See the Important note in https://codeigniter.com/user_guide/models/model.html#in-model-validation RE: Custom validation rule, variables on Model - sjender - 05-28-2024 Thanks, that was it. The docs states a warning: "This is to avoid validation errors when updating only some fields.". But in which scenario would 'false' for $cleanValidationRules trigger a validation error which is false? RE: Custom validation rule, variables on Model - kenjis - 05-28-2024 By default, if you use an Entity, only changed field data is passed when updating to the database. So if a required field is not changed, the field is not passed to Validation, and it returns a validation error. See https://codeigniter.com/user_guide/models/model.html#updateonlychanged RE: Custom validation rule, variables on Model - sjender - 05-28-2024 So if I equip all my models with: $updateOnlyChanged = false; $cleanValidationRules = false; It should be OK right? RE: Custom validation rule, variables on Model - kenjis - 05-29-2024 No problem. |