Welcome Guest, Not a member yet? Register   Sign In
Validating multiple fields
#1

[eluser]ReyPM[/eluser]
Hi, I have a form with multiple fields lets say:
Code:
input_name1, input_lastname1, input_email1
input_name2, input_lastname2, input_email2
input_name3, input_lastname3, input_email3
input_namen, input_lastnamen, input_emailn
All of them are in the same form but they are dynamic so "n" can take any value maybe is just one and one set of fields exists or maybe is two and two sets exists or maybe is 20 and 20 sets exists. See a set as a row or something like that. My question is: it's possible to validate all of them without know how many are?
#2

[eluser]Mirge[/eluser]
Just off the top of my head, something like this could work (untested):

Code:
for($i = 1; $i <= 100; $i++) /* don't want to go TOO far, surely 100 is enough? adjust if not. */
{
if($this->input->post('input_name' . $i))
{
  $this->form_validation->set_rules('input_name' . $i, "First Name $i", 'trim|required');
  $this->form_validation->set_rules('input_lastname' . $i, "Last Name $i", 'trim|required');
  $this->form_validation->set_rules('input_email' . $i, "Email $i", 'trim|required|valid_email');
}
else
{
  break;
}
}
#3

[eluser]PhilTem[/eluser]
If you don't know how many you have, but want to know, you can use
Code:
$count_fields = count($this->input->post())/3;

assuming you only have fields submitted that are input_name<n>, input_lastname<n>, input_email<n>. and nothing else, i.e. every count of $_POST is a multiple of 3.

On the other hand: Why don't you just use textareas for your purpose? This way you can get how many items you have by
Code:
$count_fields = count(explode("\n", $this->input->post('textarea_names')));

assuming you count every line separately (could also be items separated by comma or any other delimiter)

PS: Don't make your code and coding life more difficult than it has to be Wink
#4

[eluser]Mirge[/eluser]
Well, if we have OPTIONS.... change it to use arrays instead.

Example markup:

Code:
<ul>
  <li>
    &lt;input type="text" name="input_name[]" /&gt;
    &lt;input type="text" name="input_lastname[]" /&gt;
    &lt;input type="text" name="input_email[]" /&gt;
  </li>

  <li>
    &lt;input type="text" name="input_name[]" /&gt;
    &lt;input type="text" name="input_lastname[]" /&gt;
    &lt;input type="text" name="input_email[]" /&gt;
  </li>

  <li>
    &lt;input type="text" name="input_name[]" /&gt;
    &lt;input type="text" name="input_lastname[]" /&gt;
    &lt;input type="text" name="input_email[]" /&gt;
  </li>

  <li>
    &lt;input type="text" name="input_name[]" /&gt;
    &lt;input type="text" name="input_lastname[]" /&gt;
    &lt;input type="text" name="input_email[]" /&gt;
  </li>

  <li>
    &lt;input type="text" name="input_name[]" /&gt;
    &lt;input type="text" name="input_lastname[]" /&gt;
    &lt;input type="text" name="input_email[]" /&gt;
  </li>
</ul>

When iterating through input_name, use the same index to access the corresponding values in other fields.




Theme © iAndrew 2016 - Forum software by © MyBB