Welcome Guest, Not a member yet? Register   Sign In
Validation problem - Config | Callback | Controller
#1

[eluser]Multisnet[/eluser]
I need some help please..

I'm dealing with validation. After read the User Guide topic I decided to create a config file with rules for validation.

form_validation.php example:

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'
                      )
)

in the controller I use
Code:
if ($this->form_validation->run('registoPaciente') == FALSE)
        {
            (...)
        }
        else
        {
            (...)
        }

The problem is in the callback verificaEmail. In the controller I implemented this method for test
Code:
function verificaEmail($str)
    {
    if ($str == '[email protected]')
        {
[b]echo "TEST";[/b]
        $this->form_validation->set_message('email_existente', 'Error in e-mail');
        return FALSE;
        }
    else
        {
        return TRUE;
        }
    }

When I run the application the Test message is displayed, however it return this error for the email field:
Unable to access an error message corresponding to your field name.

Could you help me?

PS:Some expressions are in Portuguese but I think that I translated everything relevant.

Thanks in advance
#2

[eluser]TheFuzzy0ne[/eluser]
Try replacing this line:
Code:
$this->form_validation->set_message('email_existente', 'Error in e-mail');

with this:
Code:
$this->form_validation->set_message('verificaEmail', 'Error in e-mail');

The first parameter of the set_message method needs to be the name of your validation method.
#3

[eluser]Multisnet[/eluser]
Thank you Wink
#4

[eluser]Multisnet[/eluser]
I will use this topic to another doubt, if I extend form validation class is it possible to create my own validation rules without callback? I would like to make some generic rules like (integer < x).
#5

[eluser]TheFuzzy0ne[/eluser]
Yes, if the rule isn't prefixed with callback_, then the validation class is one of the first places the form validation library looks for the rule. You should also be able to create your own help function, and use that.
#6

[eluser]Multisnet[/eluser]
My idea is to extend default CI form validation class, so i follow the user guide instructions:

Quote:class MO_Form_Validation extends CI_Form_Validation {

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

The extended class is loaded, however, my forms doesn't display errors anymore, It seems that the new class doesn't really extend the base CI class. I'm doing anything wrong?
#7

[eluser]TheFuzzy0ne[/eluser]
I know this sounds like a silly question, but I just want to be sure - Have you changed the prefix from 'MY_' to 'MO_' in the config.php? Note the trailing underscore is very important.
#8

[eluser]Multisnet[/eluser]
Yes I changed the prefix. I extended many other classes without any problem, I'm using MO_Controller perfectly.

Code:
class MO_Controller extends Controller {
  function MO_Controller()
  {
        parent::Controller();
}
}

I realize now that the functions from form_validation don't work correctly (e.g. set_select() prints selected in every option).

I can't understand the problem.
#9

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

[eluser]pistolPete[/eluser]
This might be a capitalization issue:
What's the filename? It should be MO_Form_validation.php.




Theme © iAndrew 2016 - Forum software by © MyBB