Cakephp: How to validate an array to make sure that it has at least five items or more -
hie guys need code. have form student selects subjects, marks , grades obtained. subjects , grades dropdown menu , in loop. want student enter @ least 5 subjects including english language. each subject referenced subject code. array of students details saved. can me this?
my controller function follows
public function add($app_id = null) { if($app_id != null){ if ($this->request->is('post')) { //saving 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(); ($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 prerequisite application 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 save.')); } } }
my model follows
public $validate = array( 'grade' => array( 'notempty ddd' => array( 'rule' => array('notempty'), //'rule' => '/^[a-z]{1}$/i', 'message' => 'enter valid grade, in capital letters!', //'allowempty' => false, //'required' => false, //'last' => false, // stop validation after rule //'on' => 'create', // limit validation '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 rule //'on' => 'create', // limit validation 'create' or 'update' operations ), ), );
currently code captures subjects , grades details without checking if student has entered required minimum of 5 sujects , grades. please me if can. thank you.
you should write own custom validation rule(s). add $validate
:
public $validate = array( 'grade' => array( 'notempty' => array( 'rule' => array('notempty'), 'message' => 'enter valid grade, in capital letters!', ), 'mycustomrule' => array( 'rule' => array('mycustomgraderule'), 'message' => 'one or more of subjects not have grades' ) ), 'olevel_subject_code' => array( 'numeric' => array( 'rule' => array('numeric'), ), ), );
where mycustomgraderule
method in same model:
public function mycustomgraderule($check) { // logic here }
this method should return true
when check validates , false
when not. can same olevel_subject_code
, if you're looking validate both @ least 5 subjects entered every subject has grade entered propose create 1 method both , put custom validation rule in 'olevel_subject_code'
in $validate
. $check parameter holds data field being validated. in (and every) custom rule can use model's $this->data
property, holds of current model data.
Comments
Post a Comment