form validation and checking for allowed values for example in listbox - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: form validation and checking for allowed values for example in listbox (/showthread.php?tid=56175) Pages:
1
2
|
form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012 [eluser]seba22[/eluser] Hello, How check is value in form validation is inside allowed values. For example, user submit a listbox. It can have some values for example a aa aaa So i need to make sure that some_listbox have value a or aa or aaa When i dont use codeigniter i use statement: Code: $allowed = array("a", "aa", "aaa"); I can clear see, that there is no option like this in manual... so i need to create it by my own. I extend validation class and add this function: Code: function valid_array_element($str, $allowed) And i don't know how pass array Code: $allowed = array("a", "aa", "aaa"); But that's of course not working... Do You have any idea ? I think we can, serialize and base64 code, and just put this Code: valid_array_element['.base64(serialize($array)).']') form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012 [eluser]CroNiX[/eluser] You can only pass strings via the 2nd parameter, so you need to do something like: Code: $this->form_validation->set_rules('admin_title', 'Somefield', 'required|valid_array_element[' . implode(';', $array) . ']'); And then in your validation function, first line: Code: function valid_array_element($str, $allowed) form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012 [eluser]seba22[/eluser] [quote author="CroNiX" date="1354387994"]You can only pass strings via the 2nd parameter, so you need to do something like: Code: $this->form_validation->set_rules('admin_title', 'Somefield', 'required|valid_array_element[' . implode(';', $array) . ']'); And then in your validation function, first line: Code: function valid_array_element($str, $allowed) Thank You for reply. It's working quite well. What about displaying error ? Drop down is a bad example, but i would like have error showing You entered wrong value, allowed are a,aa,aaa I was trying out to figure this by myself but i don't know... I was trying to look at language file, for example here Code: $lang['min_length'] = "The %s field must be at least %s characters in length."; But how pass them separated via coma values Regards form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012 [eluser]CroNiX[/eluser] You can set the error message, and retrieve it. See the "callbacks" section of the form validation library in the user guide. Code: function valid_array_element($str, $allowed) Then in your form, where this form element is, you'd show the individual error like any other validation error. Code: echo form_error('name-of-field-this-rule-was-applied-on'); form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012 [eluser]seba22[/eluser] [quote author="CroNiX" date="1354399879"]You can set the error message, and retrieve it. See the "callbacks" section of the form validation library in the user guide. Code: function valid_array_element($str, $allowed) Then in your form, where this form element is, you'd show the individual error like any other validation error. Code: echo form_error('name-of-field-this-rule-was-applied-on'); Hello, thank You for reply. I try Your code, but without success (i mean your idea is absolute OK, it's something wrong with my extension of CI_Form_Validation) i got ISE (Internal Server Error). Inside file Code: \libraries\MY_Form_validation.php i have code Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Any idea what i do wrong? I try, $this->form_validation but also without success, that's why i change to get_instance(); but without positive result. Regards form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012 [eluser]PhilTem[/eluser] Well, I guess it's not working properly since you're extending a wrong class Code: class MY_Form_validation extends CI_Form_validation See the Form_validation is just a capitalized F not V. Then $this-> should work as well for the language line. Regarding the %s in the error fields, look at [url=http://php.net/manual/en/function.sprintf.php]sprintf[/php] form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012 [eluser]CroNiX[/eluser] Also, I showed you how to do a callback. If you extend the form validation library then it's unnecessary to do $CI =& get_instance(), because $this IS the form validation library. Try changing Code: $CI = & get_instance(); Code: $this->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed)); in addition to fixing the CI classname like PhilTem pointed out. form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012 [eluser]seba22[/eluser] Hello Friends, No success, still Internal Server Error. I corrected spell typo like PhilTem found, and make changes like You suggested. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Hmmm form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012 [eluser]CroNiX[/eluser] What do your error logs show? What's the error message? How are you loading the validation library? What is the filename of your new MY_Form_validation.php fileand it's location? How are you setting the rules? You've changed things but aren't showing the rest of the code where you changed them. form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012 [eluser]PhilTem[/eluser] I see another, possible issue: Code: function __construct($rules = array()) { Took me several hours the first time I extended the form-validation library to figure that out |