0

I made a custom function for my form validation in Codeigniter. Its hooked to the URL Helper, i achieved this by making a MY_url_helper.

The helper modification:

function valid_url($str) {
   $pattern = "/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
   if (preg_match($pattern, $str)) return TRUE;
   else return FALSE;
}

/* End of file MY_url_helper.php */
/* Location: ./application/helpers/MY_url_helper.php */

How i call the validation:

$this->form_validation->set_rules('image', 'Image path', 'valid_url');

The language file:

$lang['valid_url']  = "The %s field must contain a valid URL.";

/* End of file form_validation_lang.php */
/* Location: ./system/language/english/form_validation_lang.php */

But it doesn't show any error message when submitting the form. If i change the valid_url function to echo something on true and false, it will execute that. So it runs the function.

How can i make the error message appear?

1 Answer 1

0

Are you printing validation errors to your view? Add this line above your form in your view and see if it prints then.

<?php echo validation_errors(); ?>

Edit:

A few things based on your reply...I've never actually used a "helper" function as a callback for validation...I've always defined the callback in the controller where the validation occurs. Someone else may be able to elaborate on whether you can use a helper function as a callback. You may have to declare your validation function in the controller. You could also return the helper function from a callback you declare in the controller as well to keep from having to re-write your helper in two places. That may work.

I assume you are loading the 'url' helper in your controller?

Also..per CodeIgniter's documentation...I believe you are supposed to append 'callback_' to the beginning of your function name in your 'set_rules' declaration.

$this->form_validation->set_rules('image', 'Image path', 'callback_valid_url');

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks

Sign up to request clarification or add additional context in comments.

3 Comments

@andershagbard edited answer to correspond to your updated question.
valid_url is the only error not showing. The custom errors appears.
@andershagbard added more info based on your last comment to answer. Hope this helps...if not, I am at a loss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.