CodeIgniter Forums
Validation problem - Config | Callback | Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Validation problem - Config | Callback | Controller (/showthread.php?tid=19976)

Pages: 1 2


Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]Multisnet[/eluser]
The filename was MO_form_validation.php I changed it to MO_Form_validation.php but the problem persists.


Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]Multisnet[/eluser]
Please tell me one thing please, have you ever tried to extend this class? Just to make sure that I'm not facing with some bug..


Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]TheFuzzy0ne[/eluser]
Please repost your code.


Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]Multisnet[/eluser]
libraries/MO_Form_validation.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MO_Form_Validation extends CI_Form_Validation {

    function MO_Form_Validation()
    {
        parent::CI_Form_Validation();
        echo "LOAD - >MO_Form_Validation";
    }
}

?>

controller/registo_p_ctr.php

Code:
function validacaoRegisto(){
        $this->load->library('form_validation');
        if ($this->form_validation->run('registoPaciente') == FALSE)
        {
            $this->template->write_view('reg_conteudos_centro', 'paciente_form_registo');
            $this->template->render();
        }
        else
        {
            echo("SUCESS!!!!!!!!!!!!!");
        }
    }


config/form_validation.php
Code:
$config = array(
            'registoPaciente'=>array(
                   array(
                         'field'   => 'nome',
                         'label'   => 'Nome',
                         'rules'   => 'required|alpha'
                      ),
                   array(
                         'field'   => 'apelido',
                         'label'   => 'Apelido',
                         'rules'   => 'required|alpha'
                      ),
                   array(
                         'field'   => 'email',
                         'label'   => 'Email',
                         'rules'   => 'required|callback_verificaEmail'
                      )
                  
                   (...)
)

And a view with the form and
Code:
<?php echo validation_errors(); ?>



Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]Michael Wales[/eluser]
How do you know it is not loading correctly? There isn't any code in your MO_Form_Validation class that would do anything, except for the echo which would not be displayed thanks to output buffering (if I remember correctly).


Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]Multisnet[/eluser]
I think it loads correctly. It outputs the echo at the top of the page. The problem is that the functions from CI base form validation class don't work. I think that the functions should work because I call the parent constructor.


Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]Michael Wales[/eluser]
You are passing the array with your validation rules to the run() method rather than passing it to the set_rules() method.

I don't have the libs here at work to review the library to see if passing to run() is acceptable but I've personally never seen it done like this before (and the docs don't describe that functionality).

Additionally, you must name that array $config per the docs.


Validation problem - Config | Callback | Controller - El Forum - 06-24-2009

[eluser]Multisnet[/eluser]
Michael,

I think that what I'm doing is correct, please check this from User Guide:

Creating Sets of Rules

In order to organize your rules into "sets" requires that you place them into "sub arrays". Consider the following example, showing two sets of rules. We've arbitrarily called these two rules "signup" and "email". You can name your rules anything you want:

Code:
$config = array(
                 'signup' => array(
                                    array(
                                            'field' => 'username',
                                            'label' => 'Username',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'password',
                                            'label' => 'Password',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'passconf',
                                            'label' => 'PasswordConfirmation',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'email',
                                            'label' => 'Email',
                                            'rules' => 'required'
                                         )
                                    ),
                 'email' => array(
                                    array(
                                            'field' => 'emailaddress',
                                            'label' => 'EmailAddress',
                                            'rules' => 'required|valid_email'
                                         ),
                                    array(
                                            'field' => 'name',
                                            'label' => 'Name',
                                            'rules' => 'required|alpha'
                                         ),
                                    array(
                                            'field' => 'title',
                                            'label' => 'Title',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'message',
                                            'label' => 'MessageBody',
                                            'rules' => 'required'
                                         )
                                    )                          
               );

Calling a Specific Rule Group

In order to call a specific group you will pass its name to the run() function. For example, to call the signup rule you will do this:

Code:
if ($this->form_validation->run('signup') == FALSE)
{
   $this->load->view('myform');
}
else
{
   $this->load->view('formsuccess');
}

All is working before I tried to extend the base CI class... If I remove my class extension (e.g. change the filename MO_Form_validation.php to XXMO_Form_validation.php) all works good again...


Validation problem - Config | Callback | Controller - El Forum - 06-25-2009

[eluser]Multisnet[/eluser]
I solved my problem and I would like to share the solution with you.

Extending Form_validation CI base class in the usual way won't work with the rules in a config file.

So I need to do this:

Code:
function MO_Form_validation($rules = array())
{
parent::MO_Form_validation($rules);
}

I solved my problem this way. I know, strange!!I found this simple solution, perhaps some expert could explain it.

Thank you all for your support.