I have a 1 Add User form that can add multiple user up to 10.
Now I need to validate only 1 field are required in form_validation rules. But, for sure what I need now is not a fixed field to validated. So, I can submit the data on the randomize field inside the form. (10 fields available, and only need 1 required field but once again not a fixed field because I was tried fixed field and really bothering me).
<form action="x" method="post" class="form-horizontal">
<?php for ($i=1; $i<=10; $i++) { ?>
<div class="form-group">
<label class="control-label col-sm-3">Username <?php echo $i; ?></label>
<div class="col-sm-9">
<input type="text" class="form-control" name="user[]">
</div>
</div>
<?php } ?>
Validation rules inside controller's function
if ( ! empty($this->input->post('user', TRUE)))
{
$this->form_validation->set_rules('user[]', 'User', 'trim|alpha_numeric|min_length[2]');
}
I'm trying to add required into user[] validation rules, its not working because all user[] fields become required.
Then im trying to add $this->form_validation->set_rules('user[0]', 'User', 'trim|required|alpha_numeric|min_length[2]'); into the form validation rules, so the code is look like this.
if ( ! empty($this->input->post('user', TRUE)))
{
$this->form_validation->set_rules('user[0]', 'User', 'trim|required|alpha_numeric|min_length[2]');
$this->form_validation->set_rules('user[]', 'User', 'trim|alpha_numeric|min_length[2]');
}
It was successfully validated for one field are required that is Username 1 field on array 0.
But sometimes it really bothering me to input only 1 new user only on Username 1.
Because sometimes I felt really easy to input on Username 4 or Username 9 because the cursor is near there.
Is there any way to validate this user[] fields for only required on 1 field no matter which fields is there. Like Username 3 maybe on Username 7.