Jump to content

codeigniter - validation refresh


gibbo1715

Recommended Posts

Hi Folkes

 

This is driving me nuts, probably because it is the last thing I need to figure out before i am able to have something working in php/ code igniter for the first time

 

i have a list with an edit button that opens my edit page where i can edit items submit and all works fine.

 

Then I tried to do form_validation, and i can get the validation error messages working fine, what i cant do is get the page to remember what record it is on when the validation fails and the url refreshes without the record number (Which i guess is my problem, i.e. http://localhost/index.php/books/input/6 becomes http://localhost/index.php/books/input if the form_validation fails

 

If anyone can offer me some assistance i would be vbery greatfull

 

My view is as follows

 

<html>
<head>
<link rel="stylesheet" type="text/css" 
      href="<?php echo "$base/$css"?>">
</head>
<body>
<div id="header">
<? $this->load->view('books_header'); ?>
</div>
<div id="menu">
<? $this->load->view('books_menu'); ?>
</div>


<table border="1">
  <tr>
    <th colspan="2" ALIGN="center"><? echo heading($forminput,3) ?>
<? 
if((int)$fid['value'] > 0){
echo 'Policy: ' .$fid['value'];
}
else
{
echo 'New Entry';
}
?></th>        
  </tr>

<? echo form_open('books/input'); ?>
<? echo form_hidden('id',$fid['value'], set_value('id'));?>

<tr>
<th><? echo $title ?> </th>	
<th><? echo form_input($ftitle, set_value('title')).br();?>
<?//echo set_value('$title'); ?> 
<!--Validation from my form_validation.php file in the config folder-->
<?php echo form_error('title'); ?>
</th>					
</tr>

<tr>
<th><? echo $author ?> </th>	
<th><? echo form_input($fauthor, set_value('author')).br(); ?> 
<!--Validation from my form_validation.php file in the config folder-->
<?php echo form_error('author'); ?>
</th>					
</tr>

<tr>
<th><? echo $publisher ?> </th>	
<!--Validation from my form_validation.php file in the config folder-->
<th><? echo form_input($fpublisher, set_value('publisher')).br(); ?> 
<?php echo form_error('publisher'); ?>
</th>					
</tr>

<tr>
<th><? echo $year ?> </th>	
<th><? echo form_dropdown('year',$years, set_value($fyear['value'])).br(); ?> </th>					
</tr>

<tr>
<th><? echo $available ?> </th>	

<th><? echo form_checkbox($favailable, set_value('available')).br(); ?> </th>					
</tr>

<tr>
<th><? echo $summary ?> </th>

<th><? echo form_textarea($fsummary, set_value('summary')).br(); ?> </th>					
</tr>

<tr>
<th colspan="2" ALIGN="right">
<? 
if((int)$fid['value'] > 0){
echo form_submit('mysubmit','Update Record');
}
else
{
echo form_submit('mysubmit','Add Record');
}
?>

<? //echo form_submit('mysubmit','Submit!'); ?></th>        
</tr>

</table>

<? echo form_close(); ?>

<div id="footer">
<? $this->load->view('books_footer'); ?>
</div>

</body>
</html>

 

And my controller for that view

 

<?php
class Books extends Controller{

function Books(){
parent::Controller();
$this->load->model('books/books_model');
}

function main(){

$data = $this->global_model->general();
$data['query'] = $this->books_model->books_getall();

$this->load->view('books_main',$data);
}

function input($id = 0){

$data = $this->global_model->general();

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

   if((int)$id > 0){
    $query = $this->books_model->get($id);

	$data['fid']['value'] = $query['id'];
	$data['ftitle']['value'] = $query['title'];
	$data['fauthor']['value'] = $query['author'];
	$data['fpublisher']['value'] = $query['publisher'];
	$data['fyear']['value'] = $query['year'];
	if($query['available']=='yes'){
		$data['favailable']['checked'] = TRUE;
	}else{
		$data['favailable']['checked'] = FALSE;	  
	}
	$data['fsummary']['value'] = $query['summary'];
}


if ($this->form_validation->run('books_rules') ==FALSE)
{
	//Get the id somehow here
	//if ($this->uri->segment(3) === FALSE)

	$this->load->view('books_input',$data);	
}
else
{	
	if($this->input->post('mysubmit')){
		if($this->input->post('id')){
			$this->books_model->entry_update();	  
		}else{
			$this->books_model->entry_insert();
		}
	}
	$this->form_validation->id = $fid['value'];
}

}
  
  function del($id){

$data = $this->global_model->general();
  
if((int)$id > 0){
	  $this->books_model->delete($id);
}

$data['query'] = $this->books_model->books_getall();

$this->load->view('books_main',$data); 
   
}
  
}

 

 

Many thanks

 

Let me know if i need to put more code here

 

gibbo

Link to comment
https://forums.phpfreaks.com/topic/185079-codeigniter-validation-refresh/
Share on other sites

All

 

This works for me although feels a bit messy

 

Gibbo

 

if ($this->form_validation->run('books_rules') ==FALSE)
{
	if($this->input->post('mysubmit')){
		if($this->input->post('id')){
		$data['fid']['value'] = $this->input->post('id');	
		}
	}
	$this->load->view('books_input',$data);
}
else
{	
	if($this->input->post('mysubmit')){
		if($this->input->post('id')){
			$this->books_model->entry_update();	  
		}else{
			$this->books_model->entry_insert();
		}
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.