Reputation: 138
Sorry for my bad english,
I'm creating a form that takes some values specifying reporting options in my codeigniter project. I want to show error messages created in my callback functions. I have 3 callback functions as "checkStartDate(), checkFinishDate() and checkIssueExists()" If validation part handles an error like "required" that doesn't set in callback, it's okay. But when i set an error message in callback function, they doesn't show up. The important thing; if "required" rule doesn't pass, my all callback errors show up as they should. But if "required" condition pass, there is no error message.
I have that problem with error messages, the callback functions work properly. They return FALSE when i give wrong values.
Here is my code:
view:
<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();?>
controller:
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;
}
Upvotes: 2
Views: 4870
Reputation: 138
I've solved the problem myself, but I've figured out what happens while i'm changing my code according to @Philip's answer. In my old code, I had callback functions, then I changed them as @Philip's answer. But I was still getting no error messages i want to show. I just realized that I can't set error message when i create an User or Issue class object in the same function.
In my old code (library/MY_Form_validation.php);
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->getContent(), $issue->getUser()->getUsername())) {
return TRUE;
} else {
$this->form_validation->set_message('checkIssueExists', 'You have already the same issue in pool.');
return FALSE;
}
}
You can see the $issue = new Issue();
and $user = new User();
lines cause my problem. But I don't understand why It's happening. I have 3 contorller class,
I need these functions, so i thought that i can use these classes when i need them. But in callback functions or MY_Form_validation.php file, i cannot use them like above.
I've changed my code (and other pages dependent on my code) as below and it works now; library/MY_Form_validation.php;
public function checkIssueExists()
{
$issueContent = preg_replace("/\s+/"," ", $this->input->post('issue') );
$username = $this->session->userdata('username');
if (!$this->modelIssue->checkIssueExists($issueContent, $username)) {
return TRUE;
} else {
$this->form_validation->set_message('checkIssueExists', 'You have already the same issue in pool.');
return FALSE;
}
}
But i have no idea why i cannot set error messages while i am using my classes in the same function.
Thanks for your help.
Upvotes: 0
Reputation: 4592
You should extend CI_Form_validation instead of using callbacks
class MY_Form_validation extends CI_Form_validation
{
public function checkIssueExists ( $modelIssue )
{
// $ci =& get_instance(); //uses __get magic function instead
//$modelIssue field is automatically passed as param, no need for $_POST
if ( something ) {
$this->form_validation->set_error ( 'checkIssueExists' ,
'Some error' ) ;
return false ;
}
else {
return true ;
}
}
/** magic function to get CI super object(object) **/
public function __get ( $object )
{
$instance = &get_instance () ;
return $instance->$object ;
}
}
- Like some else suggested use the error inline
echo form_error('field_name')
OR
foreach( validation_errors() as $error ):
...
endforeach;
I would also setup a form_validation.php config file in ./config
$config = array(
'class/method' => array(
array('field'=>'', 'label'=>'', 'rules'=>'required|checkIssueExists'),
),
);
Your now clean controller method
// If the form validation cant find any key for this class/method
// in config/form_validation.php
// optionally you can pass in a key
// ie: if($this->form_validation->run('key'))
if( !$this->form_validation->run() )
{
return $this->home();
}
//validation must have passed
Upvotes: 2