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

[eluser]Unknown[/eluser]
I have been trying to fix this issue for days, with no luck, so I hope someone can help me. I am using version 2.1.4, but have tried 2.1.3 and 2.1.2 as well. I have been using CodeIgniter for years and never run into anything like this.

I am using the Form_Validation class to do basic validation on my site. When going into Form_validation.php and output $this->_field_data it returns with all of my information from within set_rules and run, but if I try to output $this->_field_data in set_value it returns an empty array.

I auto load the form_validation library and the form/url helpers.

Controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Stream extends CI_Controller
{
  var $data = array();
  var $stream_template = "templates/stream";

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

  public function create($type = 'headline')
  {
    // Verify Valid Type
    if(in_array($type, array('article', 'cluster', 'headline')))
    {
      $this->data['type'] = $type;
      $this->form_validation->set_rules('headline', 'Headline', 'trim|required|xss_clean');
      if($type == 'article')
      {
        $this->form_validation->set_rules('article', 'Article', 'trim|xss_clean');
      }
      $this->form_validation->set_rules('place', 'Location', 'trim|xss_clean');
      $this->form_validation->set_rules('placeId', 'Place ID', 'trim|xss_clean');
      $this->form_validation->set_rules('categoryId[]', 'Category', 'trim|xss_clean|callback_one_checkbox_set');
      $this->form_validation->set_rules('tags', 'Tags', 'trim|xss_clean');
      if($this->form_validation->run())
      {
        // Set Variables for Database Insert
        $post = $this->input->post();
        $insert = array(
          'headline' => $post['headline'],
          'tags' => $post['tags']
        );
        // Set Location
        if($post['placeId']) { $insert['placeId'] = $post['placeId']; }
        elseif($post['place'])
        {
          // Find/Create Location
          if($place = $this->streams_model->places_get_single(array('place' => $post['place'], 'deleted' => 0))) { $insert['placeId'] = $place->placeId; }
          else { $insert['placeId'] = $this->streams_model->places_add(array('place' => $post['place'])); }
        }
        // Insert to Proper Table
        if($type == 'article')
        {
          $insert['article'] = $post['article'];
          $id = $this->streams_model->articles_add($insert);
          $this->assets_model->metadata_add_array(array($type.'Id' => $id), $post['categoryId']);
        }
        else
        {
          if($type == 'cluster') { $id = $this->streams_model->clusters_add($insert); }
          else { $id = $this->streams_model->headlines_add($insert); }
          $this->assets_model->metadata_add_array(array($type.'Id' => $id), $post['categoryId']);
          // Run Automated Compare
          $this->streams_model->streams_compare($type, $id);
        }
        redirect($type.'/'.$id);
      }
      else { $this->create_view(); }
    }
    else { redirect('stream'); }
  }
  private function create_view()
  {
    $this->data['title'] = "Create ".ucfirst($this->data['type']);
    $this->data['page'] = "stream-create-".$this->data['type'];
    $this->load->view($this->stream_template, $this->data);
  }
  function one_checkbox_set($value)
  {
    $this->form_validation->set_message('one_checkbox_set', 'Atleast one %s must be selected.');
    return false;
  }
}

/* End of file stream.php */
/* Location: ./application/controllers/stream.php */

View:
Code:
<section class="create">
  <h2>Create Headline</h2>
  &lt;?php echo form_open('headline/create'); ?&gt;
    &lt;?php echo validation_errors(); ?&gt;
    <ul>
      <li class="headline">
        &lt;?php echo form_input('headline', set_value('headline'), 'placeholder="Headline"'); ?&gt;</li>
      <li class="place">
        &lt;?php echo form_input('place', set_value('place'), 'placeholder="Location" class="place-ac"'); ?&gt;
        &lt;?php echo form_hidden('placeId', set_value('placeId')); ?&gt;</li>
      &lt;?php $tags = array('name' => 'tags', 'value' => set_value('tags'), 'placeholder' => 'Tags (comma seperated)', 'rows' => '5'); ?&gt;
      <li class="tags">&lt;?php echo form_textarea($tags); ?&gt;</li>
      <li class="categories">
        &lt;?php foreach($categories as $key => $value): ?&gt;
          &lt;?php $isset = in_array($key, set_value('categoryId[]')?set_value('categoryId[]'):array())?true:false; ?&gt;
          &lt;?php echo form_checkbox('categoryId[]', $key, $isset, 'id="category-'.$key.'"'); ?&gt;
          <label for="category-&lt;?php echo $key; ?&gt;">&lt;?php echo $value; ?&gt;</label>
        &lt;?php endforeach; ?&gt;
      </li>
      <li class="submit"><button type="submit">Submit</button></li>
    </ul>
  &lt;?php echo form_close(); ?&gt;
</section>

Also, one_checkbox_set is suppose to return false, so that I can test things.
#2

[eluser]Unknown[/eluser]
In case I didn't make it clear; set_value() and validation_errors(), return empty.




Theme © iAndrew 2016 - Forum software by © MyBB