Welcome Guest, Not a member yet? Register   Sign In
Hidden field not been submitted with form validation
#1

(This post was last modified: 12-27-2015, 11:49 PM by wolfgang1983.)

I have a small issue.

I am now using form helper and form validation to submit my up and down votes.

But for some reason using the form validation on the down_vote does not submit it keeps on returning false.

I am not sure why form validation does not work with hidden fields.

The var dump is working. form helper is auto loaded.


Code:
array (size=1)
 'down_vote' => string '1' (length=1)


Controller Forum

PHP Code:
<?php

class Forum extends CI_Controller {

    public function 
__construct() {
        
parent::__construct();
        
$this->load->model('model_forum');
        
$this->load->library('form_validation');
    }

    public function 
index() {
        
$data['forum_user_total_votes'] = 'Your Rep '  $this->model_forum->get_forum_user_reputation();
        
$data['total_votes'] = $this->model_forum->get_forum_question_reputation();

        
$this->load->view('common/header');
        
$this->load->view('forum/forum_question_demo'$data);
        
$this->load->view('common/footer');
    }

    public function 
down_vote() {
        

        
$this->form_validation->set_rules('down_vote''Down Vote');

        if (
$this->form_validation->run() == FALSE) {

            echo 
"Does Not Work";

            
var_dump($_POST);

            
//redirect('forum');

        
} else {

            echo 
$this->input->post('down_vote');

            
$this->model_forum->update_forum_question_down_vote();
            
$this->model_forum->update_forum_user_reputation_down_vote();
                
            
redirect('forum');
        }
    }

View

PHP Code:
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 text-center">
<
div class="well">

<?
php echo form_open_multipart('forum/up_vote');?>
<?php 
echo form_hidden('up_vote''1');?>
<button type="submit" style="background-color: transparent; border: none;"><i class="fa fa-angle-up fa-4x"></i></button>
<?php echo form_close();?>

<div class="total"><?php echo $total_votes;?></div>

<?php echo form_open_multipart('forum/down_vote');?>
<?php 
echo form_hidden('down_vote''1');?>
<button type="submit" style="background-color: transparent; border: none;"><i  class="fa fa-angle-down fa-4x"></i></button>
<?php echo form_close();?>

</div>
</div> 
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

You could try the following to debug your problem:
PHP Code:
<?php

class Forum extends CI_Controller {

 
   public function __construct() {
 
       parent::__construct();
 
       $this->load->model('model_forum');
 
       $this->load->library('form_validation');
 
   }

 
   public function index() {
 
       $data['forum_user_total_votes'] = 'Your Rep '  $this->model_forum->get_forum_user_reputation();
 
       $data['total_votes'] = $this->model_forum->get_forum_question_reputation();

 
       $this->load->view('common/header');
 
       $this->load->view('forum/forum_question_demo'$data);
 
       $this->load->view('common/footer');
 
   }

 
   public function down_vote() {
 
       

        $this
->form_validation->set_rules('down_vote''Down Vote');

 
       if ($this->form_validation->run() == FALSE) {

 
           echo "Does Not Work";

 
           var_dump($_POST);

 
           // This should give you details on why the form was rejected:
 
           var_dump(validation_errors());

 
           //redirect('forum');

 
       } else {

 
           echo $this->input->post('down_vote');

 
           $this->model_forum->update_forum_question_down_vote();
 
           $this->model_forum->update_forum_user_reputation_down_vote();
 
               
            redirect
('forum');
 
       }
 
   }

Reply
#3

The third parameter test

<?php echo form_hidden('down_vote', '1','id="down_vote"');?>
Reply
#4

(This post was last modified: 12-28-2015, 12:12 AM by wolfgang1983.)

(12-28-2015, 12:04 AM)Diederik Wrote: You could try the following to debug your problem:
PHP Code:
<?php

class Forum extends CI_Controller {

 
   public function __construct() {
 
       parent::__construct();
 
       $this->load->model('model_forum');
 
       $this->load->library('form_validation');
 
   }

 
   public function index() {
 
       $data['forum_user_total_votes'] = 'Your Rep '  $this->model_forum->get_forum_user_reputation();
 
       $data['total_votes'] = $this->model_forum->get_forum_question_reputation();

 
       $this->load->view('common/header');
 
       $this->load->view('forum/forum_question_demo'$data);
 
       $this->load->view('common/footer');
 
   }

 
   public function down_vote() {
 
       

        $this
->form_validation->set_rules('down_vote''Down Vote');

 
       if ($this->form_validation->run() == FALSE) {

 
           echo "Does Not Work";

 
           var_dump($_POST);

 
           // This should give you details on why the form was rejected:
 
           var_dump(validation_errors());

 
           //redirect('forum');

 
       } else {

 
           echo $this->input->post('down_vote');

 
           $this->model_forum->update_forum_question_down_vote();
 
           $this->model_forum->update_forum_user_reputation_down_vote();
 
               
            redirect
('forum');
 
       }
 
   }


Thank you for tip in the set_rules for some reason to make it work requires a third part i.e required and then it worked. Unsure why need that third part to make it work?

Code:
$this->form_validation->set_rules('down_vote', 'Down Vote', 'trim|required');

Will not work if just have

Code:
$this->form_validation->set_rules('down_vote', 'Down Vote');
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#5

Ah yes, sorry I didn't spot that error in my first response. You need to tell the validation library which rules needs to be applied for each POST variable. You should always make your validation rules as strict as possible. In your case you expect a value of 1 and nothing else.

Code:
$this->form_validation->set_rules('down_vote', 'Down Vote', 'trim|required|integer');

This way the form is only validated when a integer is posted and not some SQL injection hacker string for example. Not that it matters in your particular codeset because you don't use the posted value itself but it's good practice to make sure all the posted fields contain data of a type that you expect it should be before you can (safely) use that data.

Here is a list with all the build in validation rules:
https://www.codeigniter.com/userguide3/l...-reference

But you can also write your own validation rules to meet your needs.
Reply
#6

(This post was last modified: 12-28-2015, 01:00 AM by wolfgang1983.)

(12-28-2015, 12:50 AM)Diederik Wrote: Ah yes, sorry I didn't spot that error in my first response. You need to tell the validation library which rules needs to be applied for each POST variable. You should always make your validation rules as strict as possible. In your case you expect a value of 1 and nothing else.

Code:
$this->form_validation->set_rules('down_vote', 'Down Vote', 'trim|required|integer');

This way the form is only validated when a integer is posted and not some SQL injection hacker string for example. Not that it matters in your particular codeset because you don't use the posted value itself but it's good practice to make sure all the posted fields contain data of a type that you expect it should be before you can (safely) use that data.

Here is a list with all the build in validation rules:
https://www.codeigniter.com/userguide3/l...-reference

But you can also write your own validation rules to meet your needs.

That is really good to know thanks. I am having a go at making my own forum even though there are some all ready out there just good practice. With bootstrap etc and moderator & user groups etc
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB