我们需要帮助我的代码。我有一个表格,学生可以选择他们获得的科目,分数和成绩。主题和成绩是一个下拉菜单,它们处于循环中。我希望学生至少输入五门科目,包括英语。每个主题由主题代码引用。将保存一系列学生详细信息。你能帮帮我吗?
我的控制器功能如下
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.'));
}
}
}
我的模型如下
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
),
),
);
目前,此代码会记录主题和成绩以及详细信息,而不会检查学生是否已输入所需的最少5个项目及其成绩。如果可以,请你帮助我。谢谢。
答案 0 :(得分:0)
您应该自己编写custom validation rule(s)。为此,请将其添加到$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'),
),
),
);
mycustomgraderule
实际上是同一模型中的另一种方法:
public function mycustomgraderule($check) {
// Your logic here
}
此方法应在检查验证时返回TRUE
,而在FALSE
时不返回。
您可以对olevel_subject_code
执行相同操作,但如果您要同时验证至少输入了5个主题,以便每个主题都输入成绩,我建议您创建一个
两者的方法,并将其作为'olevel_subject_code'
$validate
中的自定义验证规则。 $ check参数保存要验证的字段的数据。在此(和每个)自定义规则中,您可以使用模型的$this->data
属性,该属性包含所有当前模型数据。