Welcome Guest, Not a member yet? Register   Sign In
Form Validation help
#1

I'm here to ask for help in form_validation, I need to somehow reset the set_rules() of form_validation

the problem is actually that I realized a FOR to register several users on the bench and put a rule for each name[$i] the FOR, but if a user is not registered because of the rule, the next will also not because rule will go there ....... if anyone can help me I thank you, I looked at many places but I did't found how to solve this problem.

OBS: I'm sorry for this English of Google Translation, but i really need help
-------------------------------------------------------------------------------------------------------------------------------------

eu estou aqui para pedir ajuda no form_validation, eu preciso de alguma maneira resetar o set_rules() do form_validation

o problema na verdade é que eu realizei um FOR para cadastrar varios usuarios no banco, e coloquei uma regra para cada nome[$i] do FOR, mas se um usuário não for cadastrado devido a regra, os próximos também não serão, pois a regra irá continuar lá....... se alguém puder me ajudar eu agradeço, procurei em muitos lugares mas não achei como resolver este problema.
Reply
#2

i already do something like that.

this is my code from scratch

PHP Code:
if ($this->input->post('send')) {
            
$this->load->library('form_validation');
            
$index 0;
            for (
$index 0$index 100; ++$index) {
                if (
$this->input->post('host_'.$index.'_templateid') != '') {
                    if (
$this->input->post('host_'.$index.'_zabbix_integration')) {
                        
$this->form_validation->set_rules(
                            
'host_'.$index.'_templateid',
                            
'Choix du template ligne '.$index,
                            
'required|is_natural_no_zero'
                        
);
                        
$this->form_validation->set_rules(
                            
'host_'.$index.'_ip',
                            
'Adresse IP ligne '.$index,
                            
'required|valid_ip'
                        
);
                        
$this->form_validation->set_rules(
                            
'host_'.$index.'_suffixe',
                            
'Suffixe ligne '.$index,
                            
'required'
                        
);
                        
$this->form_validation->set_rules(
                            
'host_'.$index.'_location',
                            
'Emplacement ligne '.$index,
                            
'required'
                        
);
                    } else {
                        
$this->form_validation->set_rules(
                            
'host_'.$index.'_suffixe',
                            
'Suffixe ligne '.$index,
                            
'required'
                        
);
                        
$this->form_validation->set_rules(
                            
'host_'.$index.'_location',
                            
'Emplacement ligne '.$index,
                            
'required'
                        
);
                    }
                }
            }

            if (
$this->form_validation->run() === true) {
                    
// do you insert/update
            
}

Reply
#3

(03-03-2016, 03:45 AM)keulu Wrote: i already do something like that.

this is my code from scratch

PHP Code:
if ($this->input->post('send')) {
 
           $this->load->library('form_validation');
 
           $index 0;
 
           for ($index 0$index 100; ++$index) {
 
               if ($this->input->post('host_'.$index.'_templateid') != '') {
 
                   if ($this->input->post('host_'.$index.'_zabbix_integration')) {
 
                       $this->form_validation->set_rules(
 
                           'host_'.$index.'_templateid',
 
                           'Choix du template ligne '.$index,
 
                           'required|is_natural_no_zero'
 
                       );
 
                       $this->form_validation->set_rules(
 
                           'host_'.$index.'_ip',
 
                           'Adresse IP ligne '.$index,
 
                           'required|valid_ip'
 
                       );
 
                       $this->form_validation->set_rules(
 
                           'host_'.$index.'_suffixe',
 
                           'Suffixe ligne '.$index,
 
                           'required'
 
                       );
 
                       $this->form_validation->set_rules(
 
                           'host_'.$index.'_location',
 
                           'Emplacement ligne '.$index,
 
                           'required'
 
                       );
 
                   } else {
 
                       $this->form_validation->set_rules(
 
                           'host_'.$index.'_suffixe',
 
                           'Suffixe ligne '.$index,
 
                           'required'
 
                       );
 
                       $this->form_validation->set_rules(
 
                           'host_'.$index.'_location',
 
                           'Emplacement ligne '.$index,
 
                           'required'
 
                       );
 
                   }
 
               }
 
           }

 
           if ($this->form_validation->run() === true) {
 
                   // do you insert/update
 
           }


-------------------------------------------------------------------------------

I tried to put the code within my FOR for counting added records, i took the first and the second line of code, as I already carry a library in autoload and within that FOR I have tested if it sent, but has not worked yet. .... I did something like this:

if($this->input->post('cadastrar') === "cadastrar"){
   $registros_name    = $this->input->post('name');
   $registros_user    = $this->input->post('user');
   $registros_pass    = $this->input->post('pass');
   $this->load->model('admin/admin_model');
   $admin_model = new admin_model();
      
      for($i = 0; $i < count($registros_name); $i++){

         //Then i put your code here without the first two lines

         $this->form_validation->set_rules('name['.$i.']', 'NAME', 'required|min_length[5]|max_length[30]');
         $this->form_validation->set_rules('user['.$i.']', 'USER', 'required|min_length[5]|max_length[30]');
         $this->form_validation->set_rules('pass['.$i.']', 'PASS', 'required|min_length[5]|max_length[30]');

         if($this->form_validation->run() === TRUE){
            //The magic happens
         }else{
            //The magic does not happen
         }

      }
   //more some code
}
Reply
#4

It should work if you address the field as an array, like this:
PHP Code:
$this->form_validation->set_rules('name[]''NAME''required|min_length[5]|max_length[30]'); 
Then, there is no need to repeat it over and over again. Put this line right before the for { } loop.
Even better: use foreach ($name as $s_name) { } to loop through all elements of the posted name[] fields.
Reply
#5

(03-03-2016, 09:35 AM)Wouter60 Wrote: It should work if you address the field as an array, like this:
PHP Code:
$this->form_validation->set_rules('name[]''NAME''required|min_length[5]|max_length[30]'); 
Then, there is no need to repeat it over and over again. Put this line right before the for {  }  loop.
Even better: use foreach ($name as $s_name) {  }  to loop through all elements of the posted name[] fields.

thank you, I've tried that before too, but did not work...... I'm standing on it already a few days  ashshasah
I had not thought of using FOREACH instead of FOR, i will adapt the code after resolving it
Reply
#6

(This post was last modified: 03-04-2016, 02:34 AM by keulu.)

you check your validation in the for loop...

it's not like that.

set your rules in the For loop AND AFTER run the validation Smile
Reply
#7

(03-04-2016, 01:32 AM)keulu Wrote: you check your validation in the for loop...

it's not like that.

set your rules in the For loop AND AFTER run the validation Smile

but if I put the set_rules() in the FOR and then run the validation I'll get the same problem where I'll need to reset the rules for my website keep submitting the next records in the database ......

An example:

The user who is signing up records added forty lines on the table .... The website added the first twenty records because no rule was broken, on log twenty-one the rule is broken, the next records will not be added because the rule is valid for the next validations ..... then add the rules in the FOR and exit the FOR to run the validation after, in this case I believe it would not be favorable, because in this way will only make the website not register any records if one of the rules are not followed
Reply
#8

I did some more research and found this method to reset the set_rules():

Code:
// --------------------------------------------------------------------

/**
* MY FUNCTION TO RESET SET_RULES
*
* @param array $rules
* @return CI_Form_validation
*/
   
   public function reset($rules = array())
   {
       $this->_field_data = array();
       $this->_config_rules = array();
       $this->_error_array = array();
       $this->_error_messages = array();
       $this->_error_prefix = '<p>';
       $this->_error_suffix = '</p>';
       $this->error_string = '';
       $this->_safe_form_data = FALSE;
       
       // Validation rules can be stored in a config file.
       $this->_config_rules = $rules;
   }

I placed within the Form_validation class and is working now when I call:

Code:
$ this->form_validation->reset();


But thanks to everyone who gave me some tips to improve the code, I begin to adapt it better now
Reply
#9

Now one last question, I'm new here, how can I change the name of this thread to [SOLVED] Form validation help?   Big Grin

Sorry, but i really don't now how to do this...........
Reply




Theme © iAndrew 2016 - Forum software by © MyBB