Hie guys i need help with my code. I have a form where a student selects subjects, marks and grades they obtained. Subjects and grades are a dropdown menu and they are in a loop. I want a student to enter at least five subjects including english language. each subject is referenced by a subject code. An array of the students details is saved. Can you help me do this?
My controller function is as follows
public function add($app_id = null) {
if($app_id != null){
if ($this->request->is('post')) {
//saving the data in variables
$applicant_id = $this->data ['ApplicantOlevelQualification']['applicants_detail_id'];
$centre_number = $this->data['ApplicantOlevelQualification']['centre_number'];
$candidate_number = $this->data['ApplicantOlevelQualification']['candidate_number'];
$exam_body_code = $this->data['ApplicantOlevelQualification']['exam_body_code'];
$year_written = $this->data['ApplicantOlevelQualification']['year_written'];
$sittings=$this->request->data['ApplicantOlevelQualification']['number_of_sittings'];
// debug($sittings);die();
for ($i = 1; $i < sizeof($this->data['ApplicantOlevelQualification']['olevel_subject_code']); $i++) {
if ($this->data['ApplicantOlevelQualification']['olevel_subject_code'][$i] != "") {
$this->ApplicantOlevelQualification->create();
$this->ApplicantOlevelQualification->id = null;
$this->ApplicantOlevelQualification->set(array(
'applicants_detail_id' => $app_id,
'olevel_subject_code' => $this->data['ApplicantOlevelQualification']['olevel_subject_code'][$i],
'grade' => $this->data['ApplicantOlevelQualification']['grade'][$i],
'centre_number'=> $centre_number,
'candidate_number'=> $candidate_number,
'exam_body_code'=> $exam_body_code,
'year_written'=> $year_written,
)
);
//$appeng = $this->ApplicantOlevelQualification->find('list',array('fields'=> array('olevel_subject_code','grade'), 'conditions'=>array('ApplicantOlevelQualification.olevel_subject_code'=>'8900',)));
//debug(($this->data->ApplicantOlevelQualification);die();
/* if($appeng !="8900"){
$this->Session->setFlash(__('English is a prerequisite for application to procceed'));
$this->redirect(array('action' => 'add',$app_id));
} */
if ($this->ApplicantOlevelQualification->save()) {
$this->Session->setFlash(__('Your O\'level Qualifications have been saved'));
} else {
$this->Session->setFlash(__('Your O\'level Qualifications failed to save.'));
}
}
}
My model is as follows
public $validate = array(
'grade' => array(
'notempty ddd' => array(
'rule' => array('notempty'),
//'rule' => '/^[A-Z]{1}$/i',
'message' => 'Enter a valid grade, in capital letters!',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'olevel_subject_code' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
Currently this code captures subjects and grades together with the details without checking if a student has entered the required minimum of 5 sujects and their grades. Please help me if you can. Thank you.