Welcome Guest, Not a member yet? Register   Sign In
Implementing form validation
#1

[eluser]liri[/eluser]
Hey,

I imeplemented the form validation helper in my app and my controller looks something like this:
Code:
function __construct()
    {

        parent::Controller();
        
        // load session library
        $this->load->library('session');
        
        // load form validation library
        $this->load->library('form_validation');

        // load language data
        $language = $this->get_current_culture();
        $this->lang->load('app', $language);
    }


    function index($action = "show", $id = NULL)
    {

        
        switch ($action) {
            
            case "update":
                $this->update();
                break;
            case "show":
                $this->show();
                break;
            case "ajax":
                $this->ajax();
                break;
            default:
                $this->show();
                break;
            
        }
        
    }


    /**
     * show()
     * display page according to specific views and form data
     *
     */
    function show($pageMessage = "")
    {
        
        $this->form_validation->set_rules('my_dynamic_inputs[]', 'my_dynamic_inputs', 'required');
        
        $this->load->helper('url');
        $this->load->helper('form');
        
        $views['main'] = "dbsync/main.php";
        
        $viewData['menu']['menuCategory'] = $this->_getMenuCategory();
        
                // render pages glues up all the views (header, footer, etc) and draws the page
        parent::renderPage($views, $viewData);
        
    }


    function ajax() {
        
        $inputs = $this->input->post('my_dynamic_inputs');
        
        //var_dump($inputs);
        
        if ($this->form_validation->run() == FALSE) {
        $this->show();
        }
        else
        {
        echo "form submitted successfully!";
        }

And the view has:

Code:
<?php echo validation_errors(); ?>


My questions:
1. When I submit the form without any input element of my_dynamic_inputs or with an empty one
then $this->form_validation->run() == FALSE detects it as an error because it draws the page with
$this->show() but no errors appear (echo validation_errors() don't show anything).

2. If I move $this->load->library('form_validation'); from the __construct to the index() function then I get errors in the ajax function which the form_validation object doesn't exist



Is the implementation of the form_validation not correct?



Thanks!
#2

[eluser]mddd[/eluser]
If you call the url controller/ajax, then the index function is normally not loaded. CI loads the contructor and then starts the ajax() function.
Therefore the index() is not executed! And the validation class is not loaded if you put it in there.

Also, form_validation->set_rules() and form_validation->run() should be in the same method. In your case, if ajax() is loaded, then you try to run the validation without any rules! And that will always get a 'false' return value!
#3

[eluser]liri[/eluser]
Right about the index, I just noticed it after some trial and error tests.
I'm trying to think then, why the switch case is there for the update/ajax methods if they are
obviously called as functions directly and load up the show() function.

Ahh! Is that mentioned in the user guide, cause I was reading it over and over looking why this
would happen and couldn't figure out Smile
Thanks
#4

[eluser]mddd[/eluser]
It is explained in the tutorial on the manual page of Libraries/Form validation. Under the headline 'try it'.
But you are right, is doesn't say very clearly under the description of the actual 'run' method.




Theme © iAndrew 2016 - Forum software by © MyBB