You can dynamically create your own $ validate rules before saving to the controller:
public function add () {
if (!empty($this->request->data) { $validate = array(); foreach($fields as $field) { $validate[$field] = array( 'required'=>array( 'rule'='notEmpty', 'message'=>'Cannot be empty' ) ); } $this->ModelName->validate = $validate; if (!$this->ModelName->save($this->request->data)) {
}
Where $ fields is an array containing a list of fields to which you want to apply validation.
Idealy, you would move the code that builds the validation array in the model, but the effect is the same
You can use the same method to allow you to have multiple validation rules for the model.
source share