I've been using CakePHP 2.7 to develop a web application. I have a Staff add form where I want to check that at least one of a selection of fields has been filled in before Cake will call a SQL procedure.
I've done a little bit of research and came across the following questions: Example 1 and Example 2
However, I have tried all the variations of results that others have suggested and still have the issue where the form will still ask for firstname, lastname, jobtitle, email and telephone to all be filled in before it will process the request.
Can anyone help?
Form code:
<?php
echo $this->Form->create('Staff', array('inputDefaults' => array('div' => true)));
echo $this->Form->input('Salutation', array('label' => 'Salutation'));
echo $this->Form->input('FirstName', array('label' => 'First name *'));
echo $this->Form->input('LastName', array('label' => 'Last name *'));
echo $this->Form->input('Qualifications', array('label' => 'Qualifications'));
echo $this->Form->input('JobTitle', array('label' => 'Job Title *'));
echo $this->Form->input('Email', array('label' => 'Email *'));
echo $this->Form->input('Telephone', array('label' => 'Telephone *'));
echo $this->Form->input('NTLogon', array('label' => 'NT logon *'));
echo $this->Form->end('Save');
?>
Model code:
<?php
class Staff extends AppModel {
var $useTable = false;
public $validate = array(
'FirstName' => array(
'check_details' => array(
'rule' => 'hasDetails',
'message' => 'Name or job title are required.'
)
),
'LastName' => array(
'check_details' => array(
'rule' => 'hasDetails',
'message' => 'Name or job title are required.'
)
),
'JobTitle' => array(
'check_details' => array(
'rule' => 'hasDetails',
'message' => 'Name or job title are required.'
)
),
'Email' => array(
'check_contact' => array(
'rule' => 'hasContact',
'message' => 'Email or telephone is required.'
)
),
'Telephone' => array(
'check_contact' => array(
'rule' => 'hasContact',
'message' => 'Email or telephone is required.'
)
)
);
function hasDetails(){
if ((!empty($this->data['Staff']['FirstName']) && !empty($this->data['Staff']['LastName'])) || !empty($this->data[$this->name]['JobTitle'])) {
return true;
} else {
return false;
}
}
function hasContact(){
if (!empty($this->data['Staff']['Email']) || !empty($this->data['Staff']['Telephone'])) {
return true;
} else {
return false;
}
}
}
?>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire