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

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.
Reply
#2

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 { ... }
Reply
#3

(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
Reply
#4

No, you should do this.
Reply
#5

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
Reply
#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




Theme © iAndrew 2016 - Forum software by © MyBB