Sunday, 18 August 2013

Codeigniter validation, passing form data from controller function recipient to another

Codeigniter validation, passing form data from controller function
recipient to another

Im currently, exploring the validation library that is offered by ci.. and
im currently having some trouble. ive tried to do some workarounds but to
my disappoint it further messed up my codes. So ill just ask the pro's
what i should do.
My view:
<?php echo validation_errors(); ?>
<?php echo
form_open('bookstore/validateinsert');?>
<table cellpadding='4' align='center'>
<th>Title</th>
<tr>
<td><input type="text"
name="bkname" value="<?php echo
set_value('bkname');?>"/></td>
</tr>
<th>Author</th>
<tr>
<td><input type="text"
name="bkauthor" value="<?php echo
set_value('bkauthor'); ?>"/></td>
</tr>
<th>Released Year</th>
<tr>
<td><input type="text"
name="bkyear" value="<?php echo
set_value('bkyear'); ?>"/></td>
</tr>
<th>ISBN</th>
<tr>
<td><input type="text"
name="bkisbn" value="<?php echo
set_value('bkisbn'); ?>"/></td>
</tr>
<tr>
<td><input type='submit'
value='insert'/></td>
</tr>
<?php echo form_close();?>
</table>
My controller:
public function validateinsert()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('bkname', 'Book Name',
'required|is_unique[books.book_name]');
$this->form_validation->set_rules('bkauthor', 'Book Author', 'required');
$this->form_validation->set_rules('bkyear', 'Year Published',
'required|max_length[4]');
$this->form_validation->set_rules('bkisbn', 'Book ISBN', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->insertbook();
}
else
{
$this->load->view('showbooks');
}
}
public function insertbook()
{
if($_POST)
{
$data = array(
'book_id' => null,
'book_name' => $_POST['bkname'],
'book_author' => $_POST['bkauthor'],
'book_year' => $_POST['bkyear'],
'book_isbn' => $_POST['bkisbn']
);
$duplicate = $this->_searchbook('book_name',$data['book_name']);
if(!$duplicate)
{
$insertid = $this->books_model->insert_books($data);
if($insertid)
{
$data['success'] = TRUE;
$data['message'] = "The book title was successfully
added.";
}
else
{
$data['success'] = FALSE;
$data['message'] = "An error was encountered.";
}
}
else
{
$data['success'] = FALSE;
$data['message'] = "The book title is already in the system";
}
}
$this->load->view('book_entry');
}
I think that is all there is a need to know about..
The validations work perfect, although i have a question, what is the
syntax for all numbers only as i want it included on my isbn and bkyear
rules.(P.S. I still prefer javascript with its onblur feature)
THE PROBLEM:
The validations work as i said above, but my insert does not. If there are
no rules broken, i calIed the insertbook function but it inserted nothing,
as i assumed when i first made the validationfunction as the recipient of
the form and i was right, the insertbook probably did not recieve the data
in the form. What should i do? P.S. I have an idea in mind that i should
just merge those 2 functions together but that would make a mess(or does
it matter?). Ill just consult ur opinion on this.

No comments:

Post a Comment