我正在尝试将页面从 CI3 重写为 CI4。 在 CI3 中,验证规则从控制器获取一个参数。但现在我已经重写了代码,该参数(projectid)将以文本“projectid”的形式出现。 如何将变量从控制器传递到 CI4 中的自定义验证规则?
在app\Config\Validation.php中:
public array $manageProjectInfo = array (
'fldPublish' => array (
'label' => 'Make project public',
'rules' => 'trim|check_publish_datasets[projectid]'
)
);
以下是将pid:projectid写入日志中:
// check that there are no unpublished datasets if unpublishing project
public function check_publish_datasets($fldPublish, $pid) {
$valid = TRUE;
$project_model = model('Project_model');
if (!$fldPublish) {
log_message('debug','pid: '.$pid);
$nDatasets = $project_model->get_projectdataavail($pid,$published=TRUE);
if (count($nDatasets) > 0) {
$valid = FALSE;
$error = "This project cannot be unpublished because it contains published datasets.";
}
}
return $valid;
}
在应用程序\控制器\管理\Project.php
public function info($projectid = -1) {
$basics['pagetitle'] = "Manage Project Information";
if ($this->request->getPost()) {
$project['fldPublish'] = $this->request->getPost('fldPublish');
}
//Define form attributes
$data['formattribs'] = array(
'class' => 'basicForm',
'id' => 'manage-project-info-form'
);
//Create array of input options, setting default values to either what they were in the database or what was entered in post data for form repopulation on error
$data['fldPublish_publish'] = array(
'name' => 'fldPublish',
'id' => 'fldPublish',
'type' => 'radio',
'value' => '1',
'checked' => ($project['fldPublish'] == "1" ? TRUE : FALSE)
);
$data['fldPublish_notpublish'] = array(
'name' => 'fldPublish',
'id' => 'fldPublish',
'type' => 'radio',
'value' => '0',
'checked' => ($project['fldPublish'] == "0" ? TRUE : FALSE)
);
//Check if form validation has already been run and passed
//Form validation rules set in form_validation config file
if (!$this->request->is('post') || !$this->validateData($this->request->getPost(), 'manageProjectInfo')) {
$this->common->render_page($basics, 'manage/project/info', $data);
}
//If successful form validation...
else {
//Collect post data for insert
$project = array(
'fldPublish' => $this->request->getPost('fldPublish')
);
$validData = $this->validator->getValidated();
foreach ($validData as $key=>$valid) {
$project[$key] = $valid;
}
//Check flag-type columns to see if they're set to one, if they aren't set values to zero.
if ($project['fldPublish'] != 1)
$project['fldPublish'] = 0;
//Convert null dates
$builder = $this->db->table('tblProject');
$builder->where('pkProjectID', $projectid);
$updateattempt = $builder->update($info_changes);
if ($updateattempt) {
//update the projectUpdated date and log a tblProjectChanges record
$this->project_model->logProjectUpdated($projectid, "project", "info");
$this->session->setFlashdata('successmessage', "Your changes were successfully submitted");
return redirect()->to('manage/project/' . $projectid . '/info');
} else {
$data['errormessage'] = "There was a problem writing your changes to the database. Please try again";
//Load the views with render_pages, passing either name of main view to load or array of main pages to load
$this->common->render_page($basics, 'manage/project/info', $data);
}
}
}
}
在 CI4 文档中,有一个示例将其他表单输入之一作为参数发送(https://codeigniter.com/user_guide/libraries/validation.html#setting-multiple-rules)。 所以我尝试创建一个隐藏输入并传递它,但它也只是将变量的文本名称打印到日志中。
我最终不得不做两件事来解决这个问题。 首先,我在 CI4 文档中发现,
“如果您的方法需要使用参数,则该函数至少需要三个参数:
https://codeigniter.com/user_guide/libraries/validation.html#setting-multiple-rules
但是,即使通过 $params 参数传递我的变量,它仍然作为变量的文本名称传递。
因此,我使用所需的变量向表单添加了隐藏输入,并通过 $data 参数访问它。
public function check_publish_datasets($fldPublish, string $args, array $data, ?string &$error = null):bool {
$pid=$data['projectID'];
$valid = TRUE;
$project_model = model('Project_model');
if (!$fldPublish) {
$nDatasets = $project_model->get_projectdataavail($pid,$published=TRUE);
if (count($nDatasets) > 0) {
$valid = FALSE;
$error = "This project cannot be unpublished because it contains published datasets.";
}
}
return $valid;
}