Welcome Guest, Not a member yet? Register   Sign In
Problem with form_error() -function
#1

[eluser]mjstenbe[/eluser]
Hi,
Im new to CI and am currently learning the system and its capabilities. I was reading through the documentation and tried to implement a simple form with validation. The basic functionality seems to be working ok, but I cant seem to get any output from form_error() function to display field spesific errors? The $this->validation->error_string does work fine, however, and prints out all the errors at once. Whats wrong here?

Below is my code for the view.

Code:
<h1>Entering user data</h1>

   &lt;?php echo $this->validation->error_string;?&gt;
                
    &lt;?php echo form_open('validator');?&gt;
    &lt;?php echo '<p>'.form_error('firstname').'</p>'; ?&gt;
    <p>First Name &lt;input type="text" name="firstname" value="&lt;?php echo set_value('firstname');?&gt;" size="50" /&gt;&lt;/p>

    <p>&lt;input type="submit" value="Send Data" /&gt;&lt;/p>
    &lt;/form&gt;
And here's the controller:

Code:
class Validator extends Controller {

    function Validator() {

    // load controller parent

        parent::Controller();

        // load 'url' helper

        $this->load->helper('url');

        // load 'form' helper

        $this->load->helper('form');
        
        // load 'validation' class

        $this->load->library('validation');

    }

    function index() {

    // set validation rules

        $rules['firstname']="required|min_length[6]|max_length[15]|trim|xss_clean";
        $rules['lastname']="required|min_length[6]|max_length[15]|trim|xss_clean";
        $rules['email']="required|valid_email|trim|xss_clean";

        $this->validation->set_rules($rules);

        // check if form has been submitted properly

        if ($this->validation->run()==FALSE) {

        // redisplay web form

            $this->load->view('form_view');

        }

        // display success web page

        else {

            $this->load->view('success_view');

        }

    }

}

Thanks for any help.
#2

[eluser]rogierb[/eluser]
It seems you are using the old deprecated validation library. You should be using the Form Validation library (no it's not confusing, really...)

Then a lot of helpers suddenly appear to be working, among which is form_error();

Again, it's not confusing... at all! ;-)




Theme © iAndrew 2016 - Forum software by © MyBB