Welcome Guest, Not a member yet? Register   Sign In
Setting error messages for form validation in MY_Form_validation [SOLVED]
#1

[eluser]nuwanda[/eluser]
I have a model with validation rules in it. It does the main processing for the form.

Additionally, I have a custom rule in MY_Form_validation. I can return true or false for the validation. No worries there.

But what I would like to do is set an error message based on the false condition.

The regular $this->form_validation->set_message('rule', 'Error Message') doesn't work in MY_Form_validation.

How can I link the instance of the validation library in the model to my custom rule so that an appropriate error message is set depending on the reason for false?
#2

[eluser]stormbytes[/eluser]
your 'rule' should be the name of your Callback function, without the 'callback_' part

eg.:

rule:

Code:
required|min_length[10]|callback_my_validation_function

and the rest...
Code:
public function my_validation_function($field_input)
{
    if(whatever condition)
    {
        return TRUE;
    }
    else
    {
        $error_message = 'My custom error message';
        $this->form_validation->set_message('my_validation_function', $error_message);

        return FALSE;
    }
}

*ALWAYS* return an EXPLICIT 'false' if you want your error message to be 'digested' by the form validation class
#3

[eluser]nuwanda[/eluser]
Daniel, I'm sure I understand.

But my validation check is in MY_Form_validation, an extension of the validation class. Not a callback in my model.
#4

[eluser]stormbytes[/eluser]
[quote author="nuwanda" date="1290003251"]Daniel, I'm sure I understand.

But my validation check is in MY_Form_validation, an extension of the validation class. Not a callback in my model.[/quote]

Oh I see, sorry my bad. Didn't realize you were extending the class. Can't be much help there I'm fraid...
#5

[eluser]nuwanda[/eluser]
Ok!

Anyone else?
#6

[eluser]Dennis Rasmussen[/eluser]
Show us your code.
#7

[eluser]nuwanda[/eluser]
In my extended validation class I have:

Code:
function valid_url($url){      

    $pattern = '/' . '^(https?:\/\/)[^\s]+$' . '/';
    
    preg_match($pattern, $url, $matches);

    return (empty($matches)) ? FALSE : TRUE;
  }

That returns false if the submitted url fails a regex. It works fine.

But my goal is to let the user know *why* the url fails.

The idea is to fail the test but also to return a validation error as to why it failed. I'd like to set a validation error message.

I can't, obviously, because I can only return true or false to the rule. Setting a validation error at this point is not valid because my scope is in the validation class, not the model that holds the rules.

In my model, I have a rule:

Code:
$this->form_validation->set_rules('link_url','link url','valid_url');
#8

[eluser]Dennis Rasmussen[/eluser]
Set the error message in your valid_url function.

Code:
function valid_url($url)
{
    $this->form_validation->set_message('valid_url', 'The %s field is not a valid url');      

    $pattern = '/' . '^(https?:\/\/)[^\s]+$' . '/';
    preg_match($pattern, $url, $matches);

    return (empty($matches)) ? FALSE : TRUE;
}
#9

[eluser]nuwanda[/eluser]
Tried that, it's the scope.

"Call to a member function set_message() on a non-object in..."

$this is what?

See?

What we're after here is something like conditional error messages: if the rule failed because of this, return this error, else return this error.
#10

[eluser]Dennis Rasmussen[/eluser]
Oh sorry, my bad.

Code:
function valid_url($url)
{
    $CI =& get_instance();
    $CI->form_validation->set_message('valid_url', 'The %s field is not a valid url');      

    $pattern = '/' . '^(https?:\/\/)[^\s]+$' . '/';
    preg_match($pattern, $url, $matches);

    return (empty($matches)) ? FALSE : TRUE;
}

OR

you could just add the follow just above your rule in your model:
Code:
$this->form_validation->set_message('valid_url', 'The %s field is not a valid url');
$this->form_validation->set_rules('link_url','link url','valid_url');




Theme © iAndrew 2016 - Forum software by © MyBB