In a recent project I needed to add some validation to my User model. I’m using the Auth Component that is included with CakePHP. In this particular instance I wanted to allow a change password form. The form would have three fields (current_password, new_password, confirm_password). In the Model I wanted to first check if the current password was entered correctly.
Validation should always happen in the model so I created a new function that would check for the current password for the logged in user. The Auth component automatically hashes the password with SHA1 and uses the Security Salt as part of the password string to create the hash, so I needed to hash the “current_password” field from the form to check for a match. This is where I ran into the problem. I tried using the following:
There are times that you need to control the form->input helper with specific type setting and additional options. This is probably more comment when building custom form inputs that aren’t automagically being set from the model/controller.
I recently had to build a list on distinct dates, but prefer to user $this->Form->input, rather than $this->Form->select. I first built an indexed array containing my date fields.
array(
[xxxx-xx-xx] => xxxx-xx-xx
[yyyy-yy-yy] => yyyy-yy-yy
)
We’ll call this array $options. You can then build the input using something like
$this->form->input('Input Name', array(
'type' => 'select',
'options' => 'options',
'label' => 'label',
'empty' => 'No data selected'
);
A new feature of CakePHP 1.3 is to create VirtualFields. Why do you care? Typically Cake can use the $displayField variable of a model for drop down lists in your views. Suppose a User model contains two columns, firstname and lastname. There is no way to concatenate this with the $displayField variable. You can however, use $virtualFields to create a new variable that can be assigned to $displayField.
My example uses an Employee model.
class Employee extends AppModel {
var $name = 'Employee';
var $virtualFields = array('full_name' => 'CONCAT(Employee.firstname, " ", Employee.lastname)');
var $displayField = 'full_name';
}

I’ve been using CakePHP for my last few projects and recently ran into a problem that was driving me nuts. I have a few pages that don’t require any authentication. You can allow pages to be viewed by calling $this->Auth->allow(‘function_name’) in your beforeFilter() method. So, I set up my app_controller class with a before filter that looks something like this.
<?php
class AppController extends Controller {
var $helpers = array('Html', 'Form', 'Javascript');
var $components = array('Auth');
function beforeFilter() {
$this->Auth->autoRedirect = false;
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->allow('display');
}
}
?>