Welcome Guest, Not a member yet? Register   Sign In
Validation Question
#1

[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">
&lt;? $this->load->view('books_header'); ?&gt;
</div>
<div id="menu">
&lt;? $this->load->view('books_menu'); ?&gt;
</div>


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

&lt;? //echo heading($forminput,3) ?&gt;
&lt;? echo form_open('books/input'); ?&gt;
&lt;? echo form_hidden('id',$fid['value']); ?&gt;

<tr>
<th>&lt;? echo $title ?&gt; </th>    
<th>&lt;?
echo form_input($ftitle).br();
?&gt; &lt;? echo $this->validation->error_string; ?&gt;</th>                    
</tr>

<tr>
<th>&lt;? echo $author ?&gt; </th>    
<th>&lt;? echo form_input($fauthor).br(); ?&gt; </th>                    
</tr>

<tr>
<th>&lt;? echo $publisher ?&gt; </th>    
<th>&lt;? echo form_input($fpublisher).br(); ?&gt; </th>                    
</tr>

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

<tr>
<th>&lt;? echo $available ?&gt; </th>    
<th>&lt;? echo form_checkbox($favailable).br(); ?&gt; </th>                    
</tr>

<tr>
<th>&lt;? echo $summary ?&gt; </th>    
<th>&lt;? echo form_textarea($fsummary).br(); ?&gt; </th>                    
</tr>

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

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

</table>

&lt;? echo form_close(); ?&gt;

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

&lt;/body&gt;
&lt;/html&gt;

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);    
}
#2

[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
#3

[eluser]gibbo1715[/eluser]
Thankyou for your help, id only read the top half of that page of the guide so that will teach meSmile

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>&lt;? echo form_input($ftitle).br();?&gt;

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:
&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" size="50" /&gt;

I suspect i am being a bit dim but as i said, new to this

Thanks

Gibbo
#4

[eluser]gibbo1715[/eluser]
Just realised i have the issue that my &lt;? echo form_hidden('id',$fid['value']); ?&gt; becomes nothing when the validation fails as well

thanks
#5

[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.
#6

[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>&lt;? echo form_input($ftitle).br();?&gt;

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
#7

[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 &lt;?php echo form_input('user', set_value('user')); ?&gt;</p>
#8

[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 Smile

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();
            }
        }
    }
        
}
#9

[eluser]gibbo1715[/eluser]
And heres my view code

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


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

&lt;? echo form_open('books/input'); ?&gt;
&lt;? echo form_hidden('id',$fid['value'], set_value('id'));?&gt;

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

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

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

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

<tr>
<th>&lt;? echo $available ?&gt; </th>    

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

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

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

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

</table>

&lt;? echo form_close(); ?&gt;

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

&lt;/body&gt;
&lt;/html&gt;
#10

[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 Smile )

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




Theme © iAndrew 2016 - Forum software by © MyBB