简体   繁体   中英

Form validation with a large set of input box

There is a group of input boxes, for example:

<input type="text" class="form-control my-input" name="q1" size="63" value="<?= set_value("q1"); ?>">
<input type="text" class="form-control my-input" name="q2" size="63" value="<?= set_value("q2"); ?>">
....
<input type="text" class="form-control my-input" name="q100" size="63" value="<?= set_value("q30"); ?>">

There are 30 input boxes, from q1 to q30

If I would like to validate them, I need to set

$this->form_validation->set_rules('q1', lang("q1"), 'required');

30 times, and the validation error message will show 30 times if nothing important.

Therefore, I would like to:

  1. Assume the q15 is not require, loop through the post array and check whether other input box are input or empty.

  2. Only show one error message if any one of the required input box is not input

How to achieve this in codeigniter?

Here is code sample

for($i = 1; $i <= 30; $i++){
  $this->form_validation->set_rules('q' + $i, lang('q' + $i), 'required');
}

UPDATE When names don't follow pattern:

$names = [1=>'q1', 'a1', 'd3', ...];

for($i = 1; $i <= 30; $i++){
  $this->form_validation->set_rules($names[$i], lang($names[$i]), 'required');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM