Welcome Guest, Not a member yet? Register   Sign In
Codeigniter blog application bug: form is loaded without validation errors
#1

I am working on a blogging application in Codeigniter 3.1.8 and Bootstrap 4.

I have an "Edit post" form with validation.


Code:
<?php echo form_open_multipart(base_url('posts/update')); ?>
  <input type="hidden" name="id" id="pid" value="<?php echo $post->id; ?>">
  <input type="hidden" name="slug" id="slug" value="<?php echo $post->slug; ?>">
  <div class="form-group <?php if(form_error('title')) echo 'has-error';?>">
      <input type="text" name="title" id="title" class="form-control" placeholder="Title" value="<?php echo $post->title; ?>">
      <?php if(form_error('title')) echo form_error('title'); ?>
  </div>
  <div class="form-group <?php if(form_error('desc')) echo 'has-error';?>">
      <input type="text" name="desc" id="desc" class="form-control" placeholder="Short decription" value="<?php echo $post->description; ?>">
      <?php if(form_error('desc')) echo form_error('desc'); ?>
  </div>
  <div class="form-group <?php if(form_error('body')) echo 'has-error';?>">
      <textarea name="body" id="body" cols="30" rows="5" class="form-control" placeholder="Add post body"><?php echo $post->content; ?></textarea>
      <?php if(form_error('body')) echo form_error('body'); ?>
  </div>
  <div class="form-group">
      <select name="category" id="category" class="form-control">
          <?php foreach ($categories as $category): ?>
          <?php if ($category->id == $post->cat_id): ?>
          <option value="<?php echo $category->id; ?>" selected>
              <?php echo $category->name; ?>
          </option>
          <?php else: ?>
          <option value="<?php echo $category->id; ?>">
              <?php echo $category->name; ?>
          </option>
          <?php endif; ?>
          <?php endforeach; ?>
      </select>
  </div>
  <input type="hidden" name="postimage" id="postimage" value="<?php echo $post->post_image; ?>">
  <label for="postimage">Upload an image</label>
  <div class="form-group">
      <input type="file" name="userfile" id="postimage" size="20">
  </div>
  <div class="form-group">
      <input type="submit" value="Update" class="btn btn-block btn-md btn-success">
  </div>
<?php echo form_close(); ?>


If validation fails (because the Title field has been emptied, for example), the form should reload *with validation errors*.

My `update()` (lives in the Posts controller) method is wrong: it uses a redirect, so the form is reloaded **without** validation errors, to its initial state.

   
PHP Code:
public function edit($id) {
 
   // Only logged in users can edit posts
 
   if (!$this - > session - > userdata('is_logged_in')) {
 
       redirect('login');
 
   }

 
   $data $this - > Static_model - > get_static_data();
 
   $data['pages'] = $this - > Pages_model - > get_pages();
 
   $data['categories'] = $this - > Categories_model - > get_categories();
 
   $data['posts'] = $this - > Posts_model - > sidebar_posts($limit 5$offset 0);
 
   $data['post'] = $this - > Posts_model - > get_post($id);
 
   if ($this - > session - > userdata('user_id') == $data['post'] - > author_id) {
 
       $data['tagline'] = 'Edit the post "'.$data['post'] - > title.
 
       '"';
 
       $this - > load - > view('partials/header'$data);
 
       $this - > load - > view('edit-post');
 
       $this - > load - > view('partials/footer');
 
   } else {
 
       /* If the current user is not the author
        of the post do not alow edit */
 
       redirect('/'.$id);
 
   }
}

public function 
update() {
 
   // Form data validation rules
 
   $this - > form_validation - > set_rules('title''Title''required', array('required' => 'The %s field can not be empty'));
 
   $this - > form_validation - > set_rules('desc''Short description''required', array('required' => 'The %s field can not be empty'));
 
   $this - > form_validation - > set_rules('body''Body''required', array('required' => 'The %s field can not be empty'));
 
   $this - > form_validation - > set_error_delimiters('<p class="error-message">''</p>');

 
   $id $this - > input - > post('id');

 
   // Update slug (from title)
 
   if (!empty($this - > input - > post('title'))) {
 
       $slug url_title($this - > input - > post('title'), 'dash'TRUE);
 
       $slugcount $this - > Posts_model - > slug_count($slug);
 
       if ($slugcount 0) {
 
           $slug $slug.
 
           "-".$slugcount;
 
       }
 
   } else {
 
       $slug $this - > input - > post('slug');
 
   }

 
   // Upload image
 
   $config['upload_path'] = './assets/img/posts';
 
   $config['allowed_types'] = 'jpg|png';
 
   $config['max_size'] = '2048';

 
   $this - > load - > library('upload'$config);

 
   if (!$this - > upload - > do_upload()) {
 
       $errors = array('error' => $this - > upload - > display_errors());
 
       $post_image $this - > input - > post('postimage');
 
   } else {
 
       $data = array('upload_data' => $this - > upload - > data());
 
       $post_image $_FILES['userfile']['name'];
 
   }

 
   if ($this - > form_validation - > run()) {
 
       $this - > Posts_model - > update_post($id$post_image$slug);
 
       $this - > session - > set_flashdata('post_updated''Your post has been updated');
 
       redirect('/'.$slug);
 
   } else {
 
       redirect('/posts/edit/'.$slug);
 
   }


I am almost certain the problem is this line:
Code:
redirect('/posts/edit/' . $slug); 
but I have not been able to find a viable alternative.

What shall I change?
Reply
#2

You need to combine edit and update. If you look at my example below I'm saving it inside run() and loading the form inside "else".

PHP Code:
public function update$group_id )
{
    
$this->VALID_UUIDv4($group_id);
    
    
$this->data['group'] = $this->group_model->get($group_id);
    if( empty(
$this->data['group']) ) { show_404(); }
    if( 
$this->data['group']->company_id !== $this->auth_company_id ) { show_404(); }
    
    
$this->load->helper('form');
    
$this->load->library('form_validation');
    
$this->config->load('form_validation');
    
$validation_rules $this->_get_rules_update();
    
    
$this->form_validation->set_rules$validation_rules );
    if( 
$this->form_validation->run() === TRUE )
    {
        if ( 
$this->group_model->update$this->data['group'] ) === TRUE )
        {
            
redirect('admin/groups');
        }
    }
    else
    {
        
$this->load->view('admin/groups/update',$this->data);
    }

Reply
#3

Also is possible save the validation errors as session flashdata.

After the redirect, check if has errors and get them.

This: https://en.wikipedia.org/wiki/Post/Redirect/Get
Reply




Theme © iAndrew 2016 - Forum software by © MyBB