• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm going to cry, why is this so hard for something so simple?

#1
[eluser]emily12[/eluser]
I have spent a good two-days trying to get this to work and it just isn't working. What I'm trying to do is create a form where users may upload images while submitting a form. However there are a few issues here:

1- Images may upload even if there's a validation error (duplicate submissions for images).
2- Database entries are created even when when there's a validation error
3- I can't seem to figure out how to redirect the page somewhere after it's submitted.

Problem 1) I can get image uploading done right if it's on its own, so as long as it's not within the new form and using older code (thanks to nettuts) it won't duplicate I don't think.

Problem 2) Database entries don't duplicate if I use older code but the moment that I try to create a hybrid image upload / data insert it starts going crazy and uploads all the time.

Concept for solution) Check validation process and then if it's OK, then submit information to database. (Trying but having issues with this)

Problem 3) Whenever I redirect (anywhere) it seems to break the upload / database insert process.

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

Here's my controller:
Code:
<?
class Kreator extends CI_Controller
{
function __construct()
{
   parent::__construct();
  $this->load->database();
  $this->load->helper('url');
}
function index() {
         $this->load->model('createnews');
   $this->load->library('form_validation');
   $this->load->helper('form');
        // header information
// removed as it's not necessary for help
  
  if ($this->input->post('upload')) {
  $this->createnews->do_upload();
  }
  
  $data['images'] = $this->createnews->get_images();
  $this->load->view('news/createnews', $data);
}

}

Here's my model:
Code:
<?php

class Createnews extends CI_Model {

var $gallery_path;
var $gallery_path_url;

function Createnews() {
parent::__construct();

  $this->gallery_path = realpath(APPPATH . 'assets/Media/News/uploads');
  $this->gallery_path_url = base_url().'uploads/';
}
function do_upload() {
  $this->form_validation->set_rules('l_title', 'Title', 'required|xss_clean|min_length[10]|max_length[65]');  
  $this->form_validation->set_rules('news_text', 'Article', 'required|xss_clean|min_length[50]|max_length[2000]');  
  $this->form_validation->set_rules('l_desc', 'Description', 'required|xss_clean|min_length[10]|max_length[90]');  
  $this->form_validation->set_rules('subcat', 'Category', 'required|min_length[1]|max_length[11]');  
  $this->form_validation->set_rules('extsource', 'Source URL', 'xss_clean|valid_url|max_length[256]');
  $this->form_validation->set_rules('nurl', 'Friendly URL', 'required|xss_clean|alpha_dash|is_unique|max_length[60]');
  $this->form_validation->set_rules('napublic', 'Publish or save for later', 'required|xss_clean|is_numeric|max_length[1]');
  $this->form_validation->set_rules('author', 'Author', 'required|is_numeric|min_length[1]|max_length[11]');
  $this->form_validation->set_rules('nlocation', 'Location', 'xss_clean|max_length[11]');
  
  $this->form_validation->set_error_delimiters('<br /><span>', '</span>');

     if ($this->form_validation->run() == TRUE)
  {  
   // failed validation
   // quit here
   return false;
  }
  else
  {
   // success
   $config = array(
    'allowed_types' => 'gif|jpg|jpe|png',
    'upload_path' => $this->gallery_path,
    'max_size' => 1000,
    'max_width' => 2200,
    'max_height' => 2200,
    'min_width' => 260,
    'min_height' => 260,
    'overwrite' => FALSE,
    'max_filename' => 10,
    'remove_spaces' => TRUE,
   );

   $this->load->library('upload', $config);
   $this->upload->do_upload();
   $image_data = $this->upload->data();
   $config['master_dim'] = ($data['height'] > $data['width']) ? 'width' : 'height';
   $config = array(
    'source_image' => $image_data['full_path'],
    'new_image' => $this->gallery_path . '/thumbs',
    'maintain_ratio' => FALSE,
    'allowed_types' => 'gif|jpg|jpe|png',
    'quality' => 95,
    'width' => 260,
    'height' => 260,
   );
   $image_data['success'] = 'Thank You, Files Upladed!';
   $image_data['upload_data'][$i]  = $this->upload->data();
   $i++;

   $this->load->library('image_lib', $config);
   $this->image_lib->resize();
   // a model that deals with your image data (you have to create this)
   $this->set_database_info($image_data);
  }
}
        private function set_database_info($image_data){
            $new_photo_insert_data = array(
    'l_title'   => set_value('l_title'),
    'news_text'  => set_value('news_text'),
    'l_desc'   => set_value('l_desc'),
    'subcat'   => set_value('subcat'),
    'author'  => set_value('author'),
    'nurl'   => set_value('nurl'),
    'napublic'  => set_value('napublic'),
    'nlocation'  => set_value('nlocation'),
    'extsource'  => set_value('extsource'),
    'image_full' => set_value('image_full'),
    'l_thumb' => ("uploads/" . $image_data['orig_name']),
    'm_thumb' => ("uploads/thumbs/" . $image_data['orig_name']),
    'image_type' => $image_data['file_ext'],
    'width' => $image_data['image_width'],
    'height' => $image_data['image_height'],
    'file_size' => $image_data['file_size'],
    'raw_name' => $image_data['raw_name'],
            );
  $insert = $this->db->insert('test_news_articles', $new_photo_insert_data);
  return $insert;
  }

function get_images() {

  $files = scandir($this->gallery_path);
  $files = array_diff($files, array('.', '..', 'thumbs'));

  $images = array();

  foreach ($files as $file) {
   $images []= array (
    'url' => $this->gallery_path_url . $file,
    'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file,
   );
  }

  return $images;
}

}

There's a custom URL validator (thanks to another forum member) and the view page is pretty standard and includes the 'form_open_multipart' thing.

I don't know what to do anymore, I seemed to have lost all hope. ;_;

#2
[eluser]skunkbad[/eluser]
if($this->form_validation->run() !== FALSE)

#3
[eluser]CroNiX[/eluser]
Looks like they want to check whether it IS false there.

Code:
if($this->form_validation->run() === FALSE)
{  
   // failed validation
   // quit here
   return false;
}
else
{
   // success
}

#4
[eluser]emily12[/eluser]
That's true, I forgot to set it back but it doesn't post anything when I do, which is really weird. So both !== FALSE and == TRUE will submit something but it won't if it's == FALSE. Logic states that the code should not be within the first set of brackets but...? :/

#5
[eluser]CroNiX[/eluser]
What are you returning false to there? Are you checking for and printing out the validation errors if any exist somewhere?

#6
[eluser]emily12[/eluser]
In the view file I'm printing out the errors if something is wrong and it works fine but it still somehow runs "private function set_database_info($image_data)" regardless if there's an error or not.

Edit: Something is wrong. In both conditions I have echo'd either "failed" or "success" and even when it succeeds, it returns "failed" so there's something wrong at the root here.


In the library help file it says:

Quote: if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}

http://ellislab.com/codeigniter/user-gui...ation.html

But it's the same thing?

#7
[eluser]skunkbad[/eluser]
[quote author="emily12" date="1335937578"]In the view file I'm printing out the errors if something is wrong and it works fine but it still somehow runs "private function set_database_info($image_data)" regardless if there's an error or not.

Edit: Something is wrong. In both conditions I have echo'd either "failed" or "success" and even when it succeds, it returns "failed" so there's something wrong at the root here.[/quote]

If this is true, then it sounds like your validation rules need to be examined. Sorry about the short answer earlier. I was on my iPad, and typing on an iPad is one of the stupidest things I've ever tried to do. I don't know what all the hype is about those things. Glad I didn't pay for it. Anyways, I've experienced really weird issues when my validation rules were not right, and CI didn't give me any clues as to what was going on. Maybe the same is true for you.

#8
[eluser]InsiteFX[/eluser]
Try place your rule xxs_clean last not in the middle!
Code:
$this->form_validation->set_rules('l_title', 'Title', 'required|min_length[10]|max_length[65]|xss_clean');

#9
[eluser]emily12[/eluser]
Thanks insight, I'll keep doing what you said as a habit but a solution was found through proper debugging.

The core form_validation file had an error, and this is the original freshly downloaded from the website. No matter what happens, the validation will always be passed. You can take a look at around line 344 which begins what I'm talking about with the TRUE/FALSE bools.

Anyways, I added a debug message from that file to tell me when something is wrong so for example:
Quote:3Array ( [news_text] => The Article field is required. [l_desc] => The Description field is required. [nurl] => The Friendly URL field is required. )

As it turns out, I had taken out napbublic from the view file earlier because I wasn't going to use it yet. So no matter what, there would be an error of at least 1, so I removed.

Code:
$this->form_validation->set_rules('napublic', 'Publish or save for later', 'required|xss_clean|is_numeric|max_length[1]');

And it worked! This just has to show you that you should never assume everything works as it should and make debugging a part of your life. Printing error messages is needed, at least for me lol

#10
[eluser]TWP Marketing[/eluser]
Not sure if this is part of the problem you state, but the test of validation success/failure is done with the type specific comparitor '===' not the simple '=='

Code:
if($this->form_validation->run() === FALSE)

Also, the helper upload function expects, by default, to find a field name of 'userfile' per the User Guide.

I don't know what you called the user input field (maybe 'upload'???), but I would check this.
You can use your own fieldname, but then you must include it in the call to upload:
Code:
...
if( ! $this->upload->do_upload( 'your_field_name' ) )
...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.