Skip to content
This repository

Support for using regex in the $field parameter of Form_validation::set_rules(). #1812

Open
andrewryno opened this Issue · 0 comments

1 participant

Andrew Ryno
Andrew Ryno

For many projects, I've needed to validate forms which have a multi-dimensional array with a dynamic number of array rows.

For example:

Array
(
    [field1] => value1
    [field2] => Array
        (
            [1] => Array
                (
                    [subfield1] => subvalue1
                    [subfield2] => subvalue2
                )

            [2] => Array
                (
                    [subfield1] => subvalue1
                    [subfield2] => subvalue2
                )

        )

)

Where each array under field2 is created dynamically through the use of JavaScript. Right now, my solution is to check if $_POST['field2'] is set, and if so, count the elements and use a for loop to create a rule for each sub-field for that index.

if (isset($_POST['field2']))
{
    for ($i = 1; $i <= count($_POST['field2']); $i++)
    {
        $this->form_validation->set_rules('field2['.$i.'][subfield1]', 'Subfield 1', 'trim|required|xss_clean');
        $this->form_validation->set_rules('field2['.$i.'][subfield2]', 'Subfield 2', 'trim|required|xss_clean');
    }
}

What would be easier is to do something such as:

$this->form_validation->set_rules('field2[(\d+)][subfield1]', 'Subfield 1', 'trim|required');
$this->form_validation->set_rules('field2[(\d+)][subfield2]', 'Subfield 2', 'trim|required');

Or potentially just use field2[][subfield1] and it knows to check all arrays under the field2 key without the need of regex.

Thoughts? This is a rather common occurrence in many of the apps we build that require an unlimited amount of rows of data (budget tables, custom meta fields, etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.