[eluser]RS71[/eluser]
Hello
I am currently building a form and I would like to how I can validate information from dropdowns (and checkboxes and radios). I was thinking of maybe having it check that the value is in a array (the same array I use to populate the dropdowns/checkboxes/radios etc).
How can I have a rule that checks whether the value is present in the array (the available options)? Can I pass an array or something to a rule?
Also,
Is there a better way to validate such information?
I set a bunch of variables and arrays in the beginning of my controller which will be used by form helpers in the view later on. Should I move this out of the controller if I'll be using it more? If so, how would I do this?
Thanks in advance for you help.
RS71
[eluser]Michael Wales[/eluser]
Code: function index() {
$this->form_validation->set_rules('dropdown', 'My Dropdown', 'callback__dropdown_check');
}
function _dropdown_check($val) {
$valid = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
if (in_array($val, $valid)) {
return TRUE;
}
$this->form_validation->set_error_message('_dropdown_check', 'You must select a valid entry in the %s field.');
return FALSE;
}
[eluser]RS71[/eluser]
Thank you for your reply Michael.
I do have one question though. Is there a way to remove the array from within the rule and just pass it through the set_rules()? So that I may only need to have one rule (called 'in_options' for example) and pass a different options array on every new field?
[eluser]Michael Wales[/eluser]
No - the callback is automatically called by the framework and is only passed one parameter (the value of the field). You could go and extend the form_validation library, but I think that is way to much work in this particular case.
I would just make it a class variable.
Code: class Posts extends Controller {
var $_valid_dropdown_values = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
function Posts() {
parent::Controller();
}
function index() {
$this->form_validation->set_rules('dropdown', 'My Dropdown', 'callback__dropdown_check');
}
function _dropdown_check($val) {
if (in_array($val, $this->_valid_dropdown_values)) {
return TRUE;
}
$this->form_validation->set_error_message('_dropdown_check', 'You must select a valid entry in the %s field.');
return FALSE;
}
}
[eluser]phazei[/eluser]
So you don't need to have a separate dropdown_check function for every dropdown, you can do this:
Code: class Posts extends Controller {
var $_valid_dropdown_values = array()
$_valid_dropdown_values['weeks'] = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$_valid_dropdown_values['months'] = array('Jan','Feb','Mar','Apr');
function Posts() {
parent::Controller();
}
function index() {
$this->form_validation->set_rules('dropdown', 'My Dropdown', 'callback__dropdown_check[weeks]');
}
function _dropdown_check($val, $list_name) {
if (in_array($val, $this->_valid_dropdown_values[$list_name])) {
return TRUE;
}
$this->form_validation->set_error_message('_dropdown_check', 'You must select a valid entry in the %s field.');
return FALSE;
}
}
[eluser]123wesweat[/eluser]
about the _call_back function
What you i do if the dropdown values come from a db table???
Should i make a db-call in the controller or move the _call_back function to the model??
And i have Code: <input type="checkbox" name="colors[]" value="red"/>
How do i make a call_back function on an array??
This seems to work on an array
Code: function _dropdown_check($valArray)
if(isset($valArray)){
foreach($valArray as $val){
if (in_array($val, $valid)) {
return TRUE;
}
}
}//.end isset
etc.
|