CodeIgniter Forums
Form field with dynamic id validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Form field with dynamic id validation (/showthread.php?tid=66037)



Form field with dynamic id validation - greenarrow - 08-26-2016

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.


RE: Form field with dynamic id validation - ivantcholakov - 08-26-2016

You can check whether the request method is 'post'. If so, take the posted data into an array. Then scan the array for keys that begin with 'mechanic_'. For the detected such keys make the corresponding validation rules and register them. After that you can call if $this->form_validation->run() { ... } else { ... }


RE: Form field with dynamic id validation - greenarrow - 08-26-2016

(08-26-2016, 10:48 PM)ivantcholakov Wrote: You can check whether the request method is 'post'. If so, take the posted data into an array. Then scan the array for keys that begin with 'mechanic_'. For the detected such keys make the corresponding validation rules and register them. After that you can call if $this->form_validation->run() { ... } else { ... }

could you please show this in code level


RE: Form field with dynamic id validation - ivantcholakov - 08-27-2016

No, you should do this.


RE: Form field with dynamic id validation - cartalot - 08-27-2016

so i could be wrong but i think it could be something like this - say if you are doing it in a form validation array,
and somehow you get the id in there - which keep in mind could be in a hidden form field

PHP Code:
           array(
 
              $fieldname 'mechanic_' $theid 
 
               'field' =>  $fieldname,
 
               'label' => 'Mechanic',
 
               'rules' => 'trim|required|max_length[100]'
 
           ), 

also keep in mind that you can do form validation in separate methods - so for example you could check that the ID is valid before validating the mechanic field, etc


RE: Form field with dynamic id validation - Joel Catantan - 08-29-2016

(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">