对不起,我的英语不好,
我正在创建一个表单,它采用一些值来指定我的 codeigniter 项目中的报告选项。我想显示在我的回调函数中创建的错误消息。我有 3 个回调函数,如“ checkStartDate()、checkFinishDate()和checkIssueExists() ” 如果验证部分处理未在回调中设置的“必需”之类的错误,那没关系。但是当我在回调函数中设置错误消息时,它们不会出现。重要的事情;如果“必需”规则未通过,我的所有回调错误都会按应有的方式显示。但如果“必需”条件通过,则不会出现错误消息。
我有错误消息的问题,回调函数正常工作。当我给出错误的值时,它们返回 FALSE。
这是我的代码:
看法:
<div id="newIssue">
<p>
Fill the form below, add your issue to searching pool.</br>
</p>
<?php
if( isset($_GET['validationErrors']) )
echo $_GET['validationErrors'];
?>
<?=form_open("main/add-to-pool");?>
<table class="form-table">
<tr>
<td>Issue </td>
<td><?=form_input('issue', $this->input->post('issue'));?></td>
</tr>
<tr>
<td>Report timing </td>
<td>
<?php
$options = array(
'daily' => 'Every day',
'weekly' => 'Every week',
'monthly' => 'Every month',
);
echo form_dropdown('timing', $options, 'weekly');
?>
</td>
</tr>
<tr>
<td>Start Date </td>
<td>
<?=form_input(array('name' => 'startDate', 'type' => 'date'), $this->input->post('startDate'));?>
</td>
</tr>
<tr>
<td>Finish Date </td>
<td>
<?=form_input(array('name' => 'finishDate', 'type' => 'date'), $this->input->post('finishDate'));?>
</td>
</tr>
<tr>
<td>Location based </td>
<td>  
<?php
echo form_radio(array(
'name' => "location",
'class' => "radio",
'checked' => TRUE
));
?>
Yes
<?php
echo form_radio(array(
'name' => "location",
'class' => "radio",
'checked' => FALSE
));
?>
No
</td>
</tr>
<tr>
<td></td>
<td>
<div style="float:right">
<?php
echo form_submit(array(
'class' => 'btn btn-info',
'id' => 'addToPool',
'name' => 'addToPool',
'value' => 'Add to Pool'
));
?>
</div>
</td>
</tr>
</table>
<?=form_close();?>
控制器:
public function addToPool()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('issue', 'Issue', 'required|trim|xss_clean|callback_checkIssueExists');
$this->form_validation->set_rules('timing', 'Report Timing', 'required|trim|xss_clean');
$this->form_validation->set_rules('startDate', 'Start Date', 'required|trim|xss_clean|callback_checkStartDate');
$this->form_validation->set_rules('finishDate', 'Finish Date', 'required|trim|xss_clean|callback_checkFinishDate');
if ($this->form_validation->run()) {
$issueContent = preg_replace("/\s+/"," ", $this->input->post('issue') );
$startDate = date("Y-m-d H:i:s", strtotime($this->input->post('startDate')));
$finishDate = date("Y-m-d H:i:s", strtotime($this->input->post('finishDate')));
$issue = new Issue();
$issue->setContent($this->clearTurkishCharacters($issueContent));
$issue->setTrContent($issueContent);
$issue->setCreatedDate(date("Y-m-d H:i:s"));
$issue->setUpdatedDate(date("Y-m-d H:i:s"));
$issue->setStartDate($startDate);
$issue->setFinishDate($startDate);
$user = new User();
$user->setUsername($this->session->userdata('username'));
$user->dbToUser();
$issue->setUser($user);
if ($issue->issueToDb()) {
$_GET['newIssueFancyBox'] = "close";
$this->home();
} else
echo "An error occured while adding user to database!";
} else {
$_GET['validationErrors'] = validation_errors('<div class="alert alert-error">','</div>');
$_GET['newIssueFancyBox'] = "open";
$this->home();
}
}
public function checkStartDate()
{
$startDate = $this->input->post('startDate');
if (strtotime($startDate) < strtotime('-1 day')) {
$this->form_validation->set_message('checkStartDate', 'The %s field cannot take a value before today.');
return FALSE;
} else {
return TRUE;
}
}
public function checkFinishDate()
{
$startDate = $this->input->post('startDate');
$finishDate = $this->input->post('finishDate');
if (strtotime($finishDate) < strtotime($startDate) || strtotime($finishDate) < strtotime('-1 day') ) {
$this->form_validation->set_message('checkFinishDate', 'The %s field cannot take a value before start date.');
return FALSE;
} else {
return TRUE;
}
}
public function checkIssueExists()
{
$this->load->model('modelIssue');
$issueContent = preg_replace("/\s+/"," ", $this->input->post('issue') );
$issue = new Issue();
$issue->setContent($issueContent);
$user = new User();
$user->setUsername($this->session->userdata('username'));
$user->dbToUser();
$issue->setUser($user);
if( $this->modelIssue->checkIssueExists($issue) ) {
$this->form_validation->set_message('checkIssueExists', 'You have already the same issue in pool.');
return FALSE;
}
else
return TRUE;
}