开发者

Codeigniter form validation failing when it should succeed

开发者 https://www.devze.com 2022-12-12 07:19 出处:网络
I\'m building an admin utility for adding a bulk of images to an app I\'m working on.I also need to to log certain properties that are associated with the images and then store it all into the databas

I'm building an admin utility for adding a bulk of images to an app I'm working on. I also need to to log certain properties that are associated with the images and then store it all into the database.

So basically the script looks into a folder, compares the contents of the folder to records in the database. All of the info must be entered in order for the database record to be complete, hence the form validation.

The validation is working, when there are no values entered it prompts the entry of the missing fields. However it happens even when the fields ARE filled.

I'm doing something a bit funny which may be the reason.

Because I'm adding a bulk of images I'm creating the data within a for loop and adding the validation rules within the same for loop.

Here is the results:

http://s75151.gridserver.com/CI_staging/index.php/admin_panel/bulk_emo_update

Right now I have default test values in the form while testing validation. The submit button is way at the bottom. I'm printing POST variable for testing purposes.

Here is the code:

function bulk_emo_update() {  
    $img_folder_location = 'img/moodtracker/emos/';//set an image path

    $emo_files = $this->mood_model->get_emo_images('*.{png,jpg,jpeg,gif}',                   $img_folder_location); //grab files from folder

    $emo_records = $this->mood_model->get_all_emos(); //grab records from db

    $i=1; //sets a counter to be referenced in the form

    $temp_emo_info = array(); //temp vairable for holding emo data that will be sent to the form

 //loop through all the files in the designated folder
 foreach($emo_files as $file) {
  $file_path = $img_folder_location.$file;//builds the path out of the flder location and the file name 
  开发者_运维技巧//loops through all the database reocrds for the pupose of checking to see if the image file is preasent in the record
  foreach($emo_records as $record) {
   //compairs file paths, if they are the 
   if($record->picture_url != $file_path) {

    //FORM VALIDATION STUFF:
    $rules['segment_radio['.$i.']'] = "required";
    $rules['emo_name_text_feild['.$i.']'] = "required";

    //populating the temp array which will be used to construct the form
    $temp_emo_info[$i]['path'] = $file_path;
    $temp_emo_info[$i]['name'] = $file; 
   }
  }
  $i++;
 }

 //sets the reference to validation rules
 $this->validation->set_rules($rules);

 //checks to see if the form has all it's required fields

 if ($this->validation->run() == FALSE) { //if validation fails:
  print_r($_POST);
  //prepairs the data array to pass into the view to build the form
  $data['title'] = 'Bulk Emo Update';
  $data['intro_text'] = 'fill out all fields below. hit submit when finished';
  $data['emos_info'] = $temp_emo_info;

  $this->load->view('admin_bulk_emo_update_view',$data);  
 } else { // if it succeeds:
          //printing for test purposes
   print_r($_POST);    
         $this->load->view('form_result');
 } 
}

I'm new to codeigniter and php in general so if anything looks outrageously weird please tell me, don't worry about my feelings I've got thick skin.


if ($this->validation->run() == FALSE)

if you are calling the run() method of the validation class every time the script is run, will it ever return TRUE and run the else? Maybe a different return?

I'm a little cornfused by what's going on. Generally, if I'm having a problem like this, I will figure out a way to force the result I'm looking for. e.g. in your code, I'd force that else to run... once I get it to run, break down what happened to make it run. Rudimentary, but it has served me well.


You use array of rules in

$this->form_validation->set_rules()

wrong.

If you want to pass the rules in array you must stick to the key names like described here http://codeigniter.com/user_guide/libraries/form_validation.html#validationrulesasarray

So instead of

$rules['input_name'] = "required"

try this:

array(
  'field' => 'input_name',
  'label' => 'Name that you output in error message',
  'rules' => 'required'
)
0

精彩评论

暂无评论...
验证码 换一张
取 消