I have a text input where users submit CSV e.g. red, blue, red, yellow
If the user submits duplicate values for instance red as seen above I want to remove the duplicate. I started making a callback but I’m not sure how to complete it.
//callback rule
function _remove_dublicate($str)
{
$val = strtolower($str); //make everything lowercase
$colors = str_getcsv($val); //create array
$result = array_unique($colors); //remove duplicates
}
If there are duplicates, what should I do to submit the new string from $resultto the database?
Below is my form validation
$this->form_validation->set_rules('colors', 'Colors', 'trim|required|xss_clean|regex_match[/^[a-z, ]+$/i]|callback__remove_dublicate');
if ($this->form_validation->run() == FALSE) //if validation rules fail
{
$this->load->view('edit/form');
}
else //success
{
$data = array (
'colors' => $this->input->post('colors')
);
$this->My_model->colors_update($data);
}
EDIT
based on Cabaret’s suggestion I added this in the else statement for removing dublicates
$colors = str_getcsv($this->input->post('colors')); //create array
$result = array_unique($colors); //remove duplicates
$comma_separated = implode(",", $result); //add back CSV string
$data = array (
'colors' => $comma_separated
);
It seems to work
Despite the comments, this is a perfectly valid reason to use a callback, similar to the “prep” rules that actually change the value of the input submitted (for instance: trim, xss_clean, strtolower).
You’re on the right track, all you have to do is
return $resultin your callback and it will alter the input, but make sure you return a string. Example:Now, if you wanted to warn the user that there are duplicates instead of simply removing them, just
return FALSEif any are found, maybe withcount($colors) === count($result)while$resultis still an array. It’s up to you, but just so you know the option to return true/false or alter the input is available.You could actually write something like this into a new form validation rule within the Form_validation class itself (extend the library, or even just a global function) and not need to use callbacks, it seems like it would be a very reusable function.
This is considered a “prep” rule. From the User Guide:
So, after you’ve checked that the input is valid, you can proceed to clean it, trim, remove duplicates from a comma separated string, or anything you want. Any globally available functions are valid (including non-native PHP functions, for example: any of CI’s helper functions, as long as they are loaded and defined.), as well as any functions that belong to the Form_validation library or any extension of it you may have.
One thing to be aware of: If the function does not exist or is not available, it will be silently ignored. Always test to make sure you are getting the correct results.
Hope this clears up some confusion.