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

[eluser]learning_php[/eluser]
I am trying to get Form validation to display correctly and re-populate the form but for some reason all the validation errors are in a list at the stop instead of showing the errors individually?
controller
Code:
<?php
class addcontacts extends Controller{
    function index()
    {

        
        $this->form_validation->set_rules(', ', 'required');
        $this->form_validation->set_rules(', ', 'required');
        $this->form_validation->set_rules('Email', 'Email', 'required');
        $this->form_validation->set_rules(', ', 'required');
        $this->form_validation->set_rules('Town', 'Town', 'required');
        $this->form_validation->set_rules('County', 'County', 'required');
        $this->form_validation->set_rules('Postcode', 'Postcode', 'required');
        $this->form_validation->set_rules('Email', 'Email', 'required');
        $this->form_validation->set_rules(', ', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('addcontacts_view');    
        }
        else
        {
            $this->db->insert('contacts',$_POST);
              redirect('contacts','contacts');
        }
        
    }

    

}
View

Code:
<html>
<head>
<title></title>

</head>
<body>

<h1>Welcome!</h1>
    <div>
    <ul>
    <li>&lt;?=anchor('','Homepage');?&gt;</li>
    </ul>
    </div>
        &lt;?php echo validation_errors(); ?&gt;
        &lt;?= form_open('addcontacts/index') ?&gt;
    Company Name:
    &lt;?php echo form_error('); ?&gt;
    &lt;input type="text" name="CompanyName" value="&lt;?php echo set_value ('conpanyName');?&gt;" /&gt; <br/>
    <br />
    Contact Name:&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type="text" name="ContactName" id="ContactName"
    value="&lt;?php echo set_value('); ?&gt;"  /&gt; &nbsp;
    Contact Telephone Number: &lt;input type="int" name="ContactTel" id="ContactTel"
    value="&lt;?php echo set_value('); ?&gt;"  /&gt;
    &nbsp;Email: &lt;input type="text" name="Email" id="Email"
    value="&lt;?php echo set_value('Email'); ?&gt;"  size="50" /&gt;&lt;br />
    <br />
    Company Address:<br />
    &lt;input type="text" name="companyAddress" id="companyAddress"  value="&lt;?php echo set_value('); ?&gt;"  size="50"/&gt;&lt;br />
    &lt;input type="text" name="Town" id="Town"  value="&lt;?php echo set_value('Town'); ?&gt;"  size="50"/&gt;&lt;br />
        &lt;input type="text" name="County" id="County"  value="&lt;?php echo set_value('County'); ?&gt;"  size="50"/&gt;&lt;br />
        &lt;input type="text" name="Postcode" id="Postcode"  value="&lt;?php echo set_value('Postcode'); ?&gt;"  size="30"/&gt;&lt;br />
        <br />
        Company Details:<br />
        &lt;textarea name="CompanyDetails"  cols="70" rows="10" id="CompanyDetails"&gt;&lt;/textarea><br />
        <br />
        Special Item: &lt;input type="text" name="Item1" id="Item1" value="&lt;?php echo set_value('Item1'); ?&gt;" size="30"/&gt;&lt;br />
        Special Item: &lt;input type="text" name="Item2" id="Item2" value="&lt;?php echo set_value('Item2'); ?&gt;" size="30"/&gt;&lt;br />
        Special Item: &lt;input type="text" name="Item3" id="Item3" value="&lt;?php echo set_value('Item3'); ?&gt;" size="30"/&gt;&lt;br />
        Special Item: &lt;input type="text" name="Item4" id="Item4" value="&lt;?php echo set_value('Item4'); ?&gt;" size="30"/&gt;&lt;br />
        Special Item: &lt;input type="text" name="Item5" id="Item5" value="&lt;?php echo set_value('Item5'); ?&gt;" size="30"/&gt;&lt;br />
        <br />
        
        &lt;input type="submit" value="Add Contact"  /&gt;
        &lt;?=form_close() ?&gt;

&lt;/body&gt;
&lt;/html&gt;
[/code]
#2

[eluser]Mareshal[/eluser]
first of all, even you copied the text wrong, or this is your real code.

1# you don't have a constructor in your controller: http://ellislab.com/codeigniter/user-gui...nstructors

2# you didn't initialize your db library and form_validation:
http://ellislab.com/codeigniter/user-gui...mples.html
http://ellislab.com/codeigniter/user-gui...ation.html

3# to display error individually, &lt;?=form_error('you_field_name')?&gt;

4# if you don't use global XSS_CLEAN = TRUE in your config, do not trow all your post in database
$this->db->insert('contacts',$_POST); //NO

either you use after "required|xss_clean" or you use something like this:

$post['your_field_name'] = $this->input->post('your_field_name', TRUE); //TRUE is for xss_clean

good luck
#3

[eluser]tim1965[/eluser]
Go to the Userguide http://ellislab.com/codeigniter/user-gui...ation.html
and read. You will get all the answers you require




Theme © iAndrew 2016 - Forum software by © MyBB