1

UPDATED

I'm creating a codeigniter callback for validating an input where users enter programming tags for example php, js, jquery. Values are separated by commas.

I want to show a message if you enter duplicate tags for example php, jquery, php, js where php would be the duplicate.

First in my controller I set the validation rules for the 'user_tags` input

$this->form_validation->set_rules('user_tags', 'User Tags', 'callback_user_tags_dublicates', 'trim|xss_clean|max_length[100]|regex_match[/^[a-z,0-9+# ]+$/i]');

Then the callback

<?php function user_tags_dublicates($str)
        {
            $val = $str; //the input value (all the CSV)
            $tags = str_getcsv($val); //creates an array of the CSV
            if(count($tags) != count(array_unique($tags))) //if array not equal to unique array it contains duplicates
            {
                $this->form_validation->set_message('user_tags', 'The %s field can not have duplicate tags.');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        } ?>

and finally in the view I show my error.

<?php echo form_error('user_tags'); ?>

When I enter duplicate tags I get

Unable to access an error message corresponding to your field name.

I'm not sure what I'm doing wrong. I tested the function in a static page without validation rules and it works.

1
  • 1
    Try using "<?php echo validation_errors(); ?>" Commented May 31, 2011 at 1:25

2 Answers 2

2

set your error message for user_tags inside your user_tags_dublicates() function

   $this->form_validation->set_message('user_tags', 'The %s field can not have duplicate tags.');
Sign up to request clarification or add additional context in comments.

3 Comments

thanks I fixed and I get Unable to access an error message corresponding to your field name.
nvm, I actually had to add'_dublicates' when setting the error message
move set_message outside the function. for example place it under your set_rules() statements
1

This might sound stoopid but have you checked:

$tags = str_getcsv($val); //creates an array of the CSV

actually returns the tags properly?

1 Comment

yes it creates an array. I have tested the entire function without the validation rules and it works

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.