is it possible to have different validation rules depending in which action I am? e.g. a password is mandatory for creation, but not for editing.
In some examples validation is made in controller, but I think it maes for senxe to have it in the model, or?
In my controller I call the validations with: $val = \Masterdata\Model_Type::validate('create'); or $val = \Masterdata\Model_Type::validate('edit');
In my Model I have: public static function validate($factory) { $val = \Validation::forge($factory); $val->add_callable('Myvalidation'); //Myvalidation-Datei einbinden! unter Classes $val->add_field('name', 'Name', 'required|max_length[150]')->add_rule('unique', 'master_pages.name');
return $val; }
For what is "create" or "edit" used? Can I define that for dofferent rules in the model? If yes, how can I do that?
What is the best way for handling the rules of validation?
You define your default rules in your model, and you can alter them in your controller.
$val->field('password')->delete_rule('required');
With the method you use (the validate method in your Model) you can also opt to centralize all rules in your model, and indeed use the variable passed to make descisions on which ones to add.
The advantage of doing it in the model is that you keep everything central, so if your model changes, you don't have to go looking for controllers that use it.
Since you're using a separate method in the model to construct the validation object, it's probably best to use the passed "name" to determine what rules to add the the object. Clean and transparent for your controllers.
If, like I, you use ORM validation (with the rules defined as part of the properties), then you can't define multiple rules in the model, and making changes in the controller is an option. You also need to do it in the controller if the rules depend on business logic (for example, remove the required rule and disable a field if a specific situation occurs).