I have a controller where I am adding some registration values to the table,.. but in the controller I would like to know if the page validated or not, my issue right now is that when it goes to the model and validates and there is an error, it will come back to the registration page but with the password filled in with the hashed value so I need to add something there like if(validated) do this…. Here is my code
CONTROLLER
<?php
class UsersController extends AppController {
var $uses = array("User");
var $components =array('Session');
function index()
{
$this->set('users', $this->User->find('all'));
$this->layout = 'master_layout';
}
function add() {
if (!empty($this->data)) {
//HERE I WOULD LIKE TO KNOW IF THE VALIDATION FROM MY MODEL RETURNED VALID OR NOT
$this->data['User']['user_password'] = Security::hash($this->data['User']['user_password']);
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your were registered!.');
$this->redirect(array('action' => 'index'));
}
}
$this->layout = 'custom_layout';
}
}
?>
The easiest solution to this is to set the
valueattribute for your password input to empty. This is the standard way to do this.But if you want to know what the errors are and whether the model validated or not, you can also look at the
$this->ModelName->validates()method of the model.You can also use this method to get the errors in the model –
$this->ModelName->invalidFields()Cookbook link – http://book.cakephp.org/view/1182/Validating-Data-from-the-Controller
This is for CakePHP 1.3