• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hidden field not been submitted with form validation

#1
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
(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
(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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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