I'm having a strange issue with my form in CakePHP. I have a series of questions that start with a check box. If the check box is selected, I want to make two other input fields required. Otherwise, if unchecked, they are optional. I wrote a custom function for validating the input fields. Here is one of them:
public function rn_number () {
if ($this->data['Education']['rn_box'] == 1) {
if($this->data['Education']['rn_number'] == '')
return false
}
return true;
}
public $validate = array(
'rn_number' => array(
'rn_number'=>array(
'rule' => 'rn_number'
),
),
);
The validation works fine. For the above rule, it checks if the checkbox with the id 'cna_box' has a value of 1, or is checked, then check if the input with id 'cna_number' is empty. If so, return false. It always required the data to be submitted if the check box is checked. My issue is that it only sometimes indicates there are errors when the required input fields like 'cna_number' are blank. On the first submit, it will always indicate that those fields are required. But if you submit the form again, it will not indicate that those fields are required, and it won't display an error message - making it confusing for the user.
I noted the check boxes had a hidden input. Afer submitting the checkbox, and returning to the pages with errors, the hidden input would have a value of 0 and the checkbox would have a value of 1. Do you think this has anything to do with the problem?
Updated:
Here is the code from my view that generates the form fields in question:
echo $this->Form->inputs(array(
'legend'=>'Certifications',
'rn_box'=>array(
'type'=>'checkbox',
'label'=>'RN',
),
'rn_number'=>array(
'label'=>'RN Number:',
'value' => $results['Education']['rn_number']
),
'rn_exp_date' => array(
'label' => 'Expiration Date:',
'dateFormat' => 'MDY',
'monthNames' => FALSE,
'minYear' => date('Y'),
'maxYear' => date('Y') + 20,
'empty' => TRUE,
'after' => '<span class="small">(MM/DD/YYYY)</span>',
'class' => 'input-mini',
'selected'=>strtotime($results['Education']['rn_exp_date'])
),
));
And the controller code:
public function pagethree() {
/**
** If user has completed this form,
** grab data for editing
**/
$this->set('results', $this->Education->find('first', array(
'conditions' => array('Education.user_id' => $this->Auth->user('id'))
)));
if ($this->request->is('post')){
$this->request->data['User']['id'] = $this->Auth->user('id');
/**
** Find if there is any data
** in this user in the Basics
** table.
**/
$existingRecordId = $this->Education->find('first', array(
'conditions' => array('Education.user_id' => $this->Auth->user('id')),
'fields' => array('Education.id')
));
/**
** If data exists, return row
** id (PK). Set this value to
** id key, so saveAll() updates
** existing row.
**/
if(sizeof($existingRecordId)>0)
$this->request->data['Education']['id'] = $existingRecordId['Education']['id'];
if ($this->User->saveAll($this->request->data)) {
$this->redirect(array('action'=>'pagefour'));
} else {
$this->Session->setFlash('Your data have not been saved');
}
}
}