0

I have a form like this:

<form action="" method="post">
  <input type="hidden" name="ndetails" value="<?php echo $ndetails; ?>"/>
  <?php
  for ($i=1; $i<=$ndetails; $i++)
  {
  ?>
    <textarea name="mydetails[]"></textarea>
    <?php echo form_error('mydetails[]'); ?>
  <?php
  }
  ?>
</form>

and in the controller, I use a form validation like this:

for ($i=1; $i<=$this->input->post('ndetails'); $i++)
{
  $this->form_validation->set_rules('mydetails[]', 'Day '.$i, 'trim|required');
}

the problem is when there's more than 1 textarea, the form_error('mydetails[]') only show the last error message. how can I show the errors individually after each textarea?

1
  • looking at stackoverflow.com/a/17802145/689579, it looks like you could set the array key -> <textarea name="mydetails[<?php echo $i ?>]">..., <?php echo form_error('mydetails['.$i.']'); ?>, $this->form_validation->set_rules('mydetails['.$i.']', 'Day '.$i, 'trim|required'); Commented Dec 9, 2016 at 5:21

1 Answer 1

0

Use this : Code not tested

<form action="" method="post">
  <input type="hidden" name="ndetails" value="<?php echo $ndetails; ?>"/>
  <?php
  for ($i=1; $i<=$ndetails; $i++)
  {
   $j=$i-1;
    echo "<textarea name='mydetails[".$j."]'></textarea>";
    echo form_error('mydetails['.$j.']'); 
  }
  ?>
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.