Saturday, July 19, 2008

Setting form requirements for a FuseDataController

The below code is taken from an application I'm working on where customer service representatives can track calls, which are linked to contacts. I wanted to give the representatives the ability to add a contact at the same time as a call, if they choose. The form requirements below require that a call[datetime_date] always be set, and that the contact[f_name], etc... are set *if* the form input add_new_contact has a value of 1. It also forces call[datetime_date] to be in mm/dd/YYYY format using the 'regexp' option


public $form_requirements = array(
'contact[f_name]' =>
array( 'friendly_name' => 'the contact\'s first name',
'required_if' => array('add_new_contact' => array('value' => 1, 'type' => 'textbox' )) ),

'contact[l_name]' =>
array( 'friendly_name' => 'the contact\'s last name',
'required_if' => array('add_new_contact' => array('value' => 1, 'type' => 'textbox' )) ),

'contact[address1]' =>
array( 'friendly_name' => 'the first line of the address',
'required_if' => array('add_new_contact' => array('value' => 1, 'type' => 'textbox' )) ),

'contact[city]' =>
array( 'friendly_name' => 'a city',
'required_if' => array('add_new_contact' => array('value' => 1, 'type' => 'textbox' )) ),

'contact[zip_code]' =>
array( 'friendly_name' => 'a zip code',
'required_if' => array('add_new_contact' => array('value' => 1, 'type' => 'textbox' )) ),

'call[datetime_date]' =>
array( 'friendly_name' => 'the call date',
'regexp' => '/\d{2}\/\d{2}\/\d{4}/' )
);



In my controller, I used a version of adding to multiple tables from one form to add both the contact and the call.

No comments: