[eluser]gibbo1715[/eluser]
All
New to codeigniter and enjoying learning but I have a question please having got stuck a bit around validation
I have a form with my view as follows
Code: <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 heading($forminput,3) ?>
<? echo form_open('books/input'); ?>
<? echo form_hidden('id',$fid['value']); ?>
<tr>
<th><? echo $title ?> </th>
<th><?
echo form_input($ftitle).br();
?> <? echo $this->validation->error_string; ?></th>
</tr>
<tr>
<th><? echo $author ?> </th>
<th><? echo form_input($fauthor).br(); ?> </th>
</tr>
<tr>
<th><? echo $publisher ?> </th>
<th><? echo form_input($fpublisher).br(); ?> </th>
</tr>
<tr>
<th><? echo $year ?> </th>
<th><? echo form_dropdown('year',$years,$fyear['value']).br(); ?> </th>
</tr>
<tr>
<th><? echo $available ?> </th>
<th><? echo form_checkbox($favailable).br(); ?> </th>
</tr>
<tr>
<th><? echo $summary ?> </th>
<th><? echo form_textarea($fsummary).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 i have a model that contains an array with all my table data as follows
Code: $data['fid']['value'] = 0;
$data['fyear']['value'] = 0;
$data['title'] = 'Title';
$data['author'] = 'Author';
$data['publisher'] = 'Publisher';
$data['year'] = 'Year';
$data['years'] = array('2007'=>'2007',
'2008'=>'2008',
'2009'=>'2009');
$data['available'] = 'Available';
$data['summary'] = 'Summary';
$data['forminput'] = 'Form Input';
//Set up the table components as I want them viewed here
$data['ftitle'] = array('name'=>'title',
'size'=>30
);
$data['fauthor'] = array('name'=>'author',
'size'=>30
);
$data['fpublisher'] = array('name'=>'publisher',
'size'=>30
);
$data['favailable'] = array('name'=>'available',
'value'=>'yes',
'checked'=>TRUE
);
$data['fsummary'] = array('name'=>'summary',
'rows'=>5,
'cols'=>60
);
Now im trying to set up the form validation (which i have working but im now stuck on how i would get an error message under each individual form entry when using an array
IS this approach of using an array in a model so I can easily change the layout of my form not the best way to go then? or is there a way to use this approach and still be able to set up the validation properly
Thanks
gibbo
P.S.
The controller code for the validation is as follows
Code: function input($id = 0){
$data = $this->global_model->general();
// Set validation rules
//My own error function (See bottom of page)
$rules['title'] = "callback_ftitle_check";
//End******************************************
//$rules['title'] = "trim|required|xss_clean";
$rules['author'] = "trim|required|xss_clean";
$rules['publisher'] = "trim|required|xss_clean";
$this->validation->set_rules($rules);
//Fields
$fields['title'] = 'Book Title Required';
$fields['author'] = 'Author Name Required';
$fields['publisher'] = 'Publisher Required';
$this->validation->set_fields($fields);
//End Validation Set Rules
//Allows me to use css to change the format of the error message
$this->validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->validation->run() ==FALSE)
{
}
else
{
if($this->input->post('mysubmit')){
if($this->input->post('id')){
$this->books_model->entry_update();
}else{
$this->books_model->entry_insert();
}
}
}
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'];
}
$this->load->view('books_input',$data);
}
[eluser]überfuzz[/eluser]
Welcome to this forum!
You need to get familiar with the user_guide. It will solve most of your future gnarls. errors displayed separately
[eluser]gibbo1715[/eluser]
Thankyou for your help, id only read the top half of that page of the guide so that will teach me
One thing i still cant figure out though
How do i get the values that are in my data field if it passes validation when i am using
Code: <th><? echo form_input($ftitle).br();?>
The guide talks abou using the set_value function (which i can get to add the entry underneath but not repopulate my text box
Code: <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
I suspect i am being a bit dim but as i said, new to this
Thanks
Gibbo
[eluser]gibbo1715[/eluser]
Just realised i have the issue that my <? echo form_hidden('id',$fid['value']); ?> becomes nothing when the validation fails as well
thanks
[eluser]überfuzz[/eluser]
Oh, I wasn't clear enough. Do not use the validation class. Use the form_validation class instead. Take a good look at it and come back if you stumble into any ?-marks.
[eluser]gibbo1715[/eluser]
HI, i listened to you and set up a file in my config called form_validation and kept all my validation as an array in that, i ve also got the validation per area all sorted the bit i cant figure out and need a hand with is how i can get my fields to re populate when the form_validation fails
I have my array which is called as follows Code: <th><? echo form_input($ftitle).br();?>
How do i now use set_values with my $ftitle array which contains all the data for the form field (As above)
thanks for your patience
gibbo
[eluser]überfuzz[/eluser]
My advice is not to have the form_validation rules in a separate file. Sooner or later you'll start adding callback functions to the rules. Then it's nice to have all rules used in a controller in the same place. Here's an example of how you can run a validation on a new user trying to create a new user profile. See the code as a study example, there might be typozillas in it.
Code: function register() //function pointed out in the form. action="www.site.com/controller/register"
{
if ($this->_validate_form_fields() === FALSE)
{
$this->index(); //back to the start, set up form info in the index() function.
}
else
{
//Go on and register.
}
}
function _check_user() //the name says it all.
{
$this->load->model('profiles'); //model communicating with sql-tables holding user info
if(!$this->profiles->check_user($user)) //a method checking if user name exists.
{
return FALSE;
}
else
{
return TRUE;
}
}
function _validate_form_fields() //a nice and tidy way of setting up rules.
{
$this->form_validation->set_rules('user', 'Username', 'trim|required|callback__check_user');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
//etc if you having more fields, like; mail, phone...
$this->form_validation->set_message('_check_user','Gnarly user name is taken. Try again fool');
return $this->form_validation->run();
}
In the form you can use this to save info that's been submitted, but rejected.
Code: <p>Username <?php echo form_input('user', set_value('user')); ?></p>
[eluser]gibbo1715[/eluser]
Thankyou, this almost for me now, i have been advised by others that putting my validation in a file is a good practice so it can be reused from one array though?
One last problem i have, how do i get the id back into the url when re loading the form?
I just cant figure this last bit out, get this cracked and its working for me
My controller looks like this, i think i know where i need to put a line of code but as yet not been able to figure it out
thanks
gibbo
Code: 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
$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();
}
}
}
}
[eluser]gibbo1715[/eluser]
And heres my view code
Code: <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>
[eluser]gibbo1715[/eluser]
All
Figured out a way of doing this eventually but not seen anything else that looks the same so not sure im doing this right (Although works fine )
I have updated in my controller as follows
Code: 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
{
etc ..................
}
I'd be really keen to know if this is an acceptable approach
thanks
Gibbo
|