views:

198

answers:

2

Hi!

I am trying to manipulate the $this->data attribute while performing an advanced validation method. The data seems to be changing, but when it comes to saving, the changes don't apply.

My question: Am I able do edit $this->data while performing a validation method?

Best regards, Benedikt

A: 

You might need to

$this->YourModel->create()

if you're not doing so already.

CroatianSensation
For example:$this->data['User'] = $Users;Just simple manipulations.
Benedikt R.
+2  A: 

Most objects in CakePHP have a data property including the model, view and controller objects. Submitting a form from the view, means the data submitted will be available in $this->data in your controller action. When you call $this->Model->save($this->data) in your controller action, CakePHP copies the data passed in here to the data property of your model, and will then perform the validation on that array. If you are doing this, then any validation rule in this model should be able to affect the data property of the model. If this explanation doesn't help you figure out your problem, post more information so we can better understand what you're trying to do.

neilcrookes
Thank you.I am currently building a controller that provides private messages. The service should allow the user to address the message to a list of recipients. I want to validate, if the given users exist.The validation and the saving are working - and I am parsing the data correctly. But can I also pass some data to the view? I want to inform the user which user do not exist.
Benedikt R.
So, your model validation rule that checks whether the "To's" exist should add ones that don't to an array in a property of the model called nonExistentUsers. Then, back in your controller, after validation has occured, test whether $this->YourModelName-> nonExistentUsers is empty, if not, set it to be available in your view, then you can iterate through it, telling the sender which of the "To's" don't exist.
neilcrookes