Welcome Guest, Not a member yet? Register   Sign In
Form field with dynamic id validation
#6

(08-26-2016, 10:00 PM)greenarrow Wrote: How can i validate fields with names like this


Code:
<select name="mechanic_134" id="mechanic-134" class="form-control selectpicker">


mechanic_ is the general field name &  134 is a id i am getting from database. there are more fields to come like mechanic_135........mechanic_150.


i want to know how can i validate this field with dynamic id. please show it in code level. how it is done in controller & how the error output in view

Thanks.



Use foreach to check the indexes of all post data.


Controller:
PHP Code:
function validate()
{
 
   $rules = array();

 
   foreach($this->input->post() as $name => $value)
 
   {
 
       if(strpos($name, 'mechanic_') !== false)
 
       {
 
           $rules[] = array(
 
               'field' => $name,
 
               'label' => [some label],
 
               'rules' => [some rules]
 
           );
 
       }
 
   }

    
// set_rules can accept multi-dimensional array
 
   $this->form_validation->set_rules($rules);


Views:
<?php echo  form_error('mechanic_134') ?>
<select name="mechanic_134" id="mechanic-134" class="form-control selectpicker">
[Just a programmer] Cool [/Just a programmer]
Reply


Messages In This Thread
RE: Form field with dynamic id validation - by Joel Catantan - 08-29-2016, 07:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB