0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
alicemap
  • 43
  • 1
  • 10

1 Answers1

0

You should write your own custom validation rule(s). To do that add it to $validate:

public $validate = array(
'grade' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Enter a valid grade, in capital letters!',
        ),
        'mycustomrule' => array(
            'rule' => array('mycustomgraderule'),
            'message' => 'One or more of your subjects do not have grades'
        )
    ),
'olevel_subject_code' => array(
        'numeric' => array(
            'rule' => array('numeric'),
        ),
    ),
);

Where mycustomgraderule is actually another method in the same Model:

public function mycustomgraderule($check) {
//   Your logic here    
}

This method should return TRUE when the check validates and FALSE when it does not. You can do the same for olevel_subject_code, but if you're looking to validate both at least 5 subjects entered so that every subject has a grade entered I propose you create one method for both and put it as a custom validation rule in 'olevel_subject_code' in $validate. The $check parameter holds the data for the field being validated. In this (and every) custom rule you can use the Model's $this->data property, which holds all of the current Model data.

Borislav Sabev
  • 4,776
  • 1
  • 24
  • 30
  • Thank you Borislav Sabev. I have used the function as you told me above it worked but im facing a challenge that the function is not being recognised anymore when i enter wrong marks it does not validate at all. I also wanted to include a range but its giving me errors. Can you help me. I want A to only accept marks from 75-100 i tried this but its not working as well if($grade== 'A' && $mark>74 && $mark>100) { return true; } I even tried this as well but its not working, don't know if you can help. if($grade== 'A' && 74<$mark<101) { return true; } – alicemap Jul 02 '12 at 13:41
  • Basically your function should check the different conditions and (as explained) return true if **IT ALL VALIDATES**. So if you want _to validate by so many criteria_ your function should **check if all of them are met SIMULTANEOUSLY**. We can try to help, but we will need the full source code of the validation method. – Borislav Sabev Jul 02 '12 at 13:47
  • My source code is too big for the required character set. How can i give you my source code? – alicemap Jul 02 '12 at 15:19