Form validation MIN or MAX on posted array fields |
Hi,
While working on my project I found that CI does not have any rules for posted field arrays for checking if the array contains a minimum or maximum allowed selection. When using min_length or max_length CI validates each value within the array, but not the array count itself. Sometimes your form will have a multi select or check boxes. And you would like too set a minimum or maximum on that field. (Please select at least 2 values OR No more than 3 selections allowed) I added two rules by extending the Form_validation class and extended the _execute method. The added methods: min_selection PHP Code: /** max_selection PHP Code: /** The extended method _execute PHP Code: /** The extended _execute method will first check if min_selection or max_selection has been set as a rule. If they have been set, the methods are executed for validation. When found, the rule is removed from the rules array so that the remaining rules can be validated as normal. Add these lines to the form_validation_lang.php as default error messages Code: $lang['form_validation_min_selection'] = 'The {field} field must contain at least {param} selections.'; That should be it. Your CI can now validate posted field arrays based on minimum or maximum selection rules Should you find any bug or problem with the code, please let me know. – Kind regards, Martin |
Messages In This Thread |
Form validation MIN or MAX on posted array fields - by Martin7483 - 03-29-2017, 04:11 AM
RE: Form validation MIN or MAX on posted array fields - by Martin7483 - 03-29-2017, 05:33 AM
RE: Form validation MIN or MAX on posted array fields - by Martin7483 - 04-14-2017, 11:33 AM
RE: Form validation MIN or MAX on posted array fields - by PaulD - 04-15-2017, 01:30 AM
RE: Form validation MIN or MAX on posted array fields - by Martin7483 - 04-15-2017, 02:02 AM
RE: Form validation MIN or MAX on posted array fields - by PaulD - 04-15-2017, 04:48 AM
RE: Form validation MIN or MAX on posted array fields - by joao.coelho - 04-15-2017, 10:24 AM
RE: Form validation MIN or MAX on posted array fields - by Martin7483 - 04-20-2017, 03:46 AM
|