0

I've read somewhere that is possible to use Codeigniter's Form Validation also for my own variables and not only for form's inputs.

For example I'd like to validate a url to say if it is valid but not retrieving it using POST or GET from a form.

Do you know how or have a link?

1
  • 2
    Where did you read it? What's the point of loading a form validation library and use it with another meaning? why not using a dedicate library/helper/function/piece of code? Commented Jan 24, 2012 at 11:08

3 Answers 3

2

What you are looking for are the callbacks of the Form Validation Class in CodeIgniter - read the user guide for an in-depth explanation.

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

Comments

1

For PHP5 above version,you can do this

function validdate_urls($str) {

    if(!filter_var($str, FILTER_VALIDATE_URL))
    {
        $this->validation->set_message('validate_urls', 'URL Invalid');
        return 0;
    }else {
        return TRUE;
    }

} 

And call it in your validation rules :-

$rules['link'] = "callback_validate_urls"; 

Comments

1

Yes you can via set_data() method, Here you go.

$this->form_validation->set_data(array(
        'cartId'    =>  $cartId
));

$this->form_validation->set_rules('cartId', 'Card ID', 'trim|required|is_natural_no_zero');

if ($this->form_validation->run() == FALSE) {
    echo 'Invalid: ' . validation_errors();
} else {
    echo 'Valid';
}

Comments

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.