CI newbie - Form Validation Helper Problem

#1
[eluser]liz217[/eluser]
I am new to CI and am stumped. I have a form that allows a user to input data. Upon submission, I am using the validation helper to check that certain fields are completed or filled in certain ways. This was working fine until I added in 2 more fields. I added them in the same manner as the others, but ever since then I can not get the form to pass validation. It doesn't highlight errors either.

I am not sure how much of the code I would need to post for someone to help. You can view this issues at http://www.speedycomks.com and use the log in test for both user and pass.

A point in the right direction would be much appreciated. I can not figure out what to try next!

Thanks - Liz

#2
[eluser]Thorpe Obazee[/eluser]
You should try posting your code for us to see.

#3
[eluser]liz217[/eluser]
Here is the body from myform_view.php

Code:
<?php // Change the css classes to suit your needs    

$attributes = array('class' => '', 'id' => '');
echo form_open('myform/sub', $attributes); ?>
<body>
<table><tbody id="tblBody"><tr><td>
        <label for="work_order_type">Work Order Type: <span class="required">*</span></label>
        &lt;?php echo form_error('work_order_type'); ?&gt;</td><td><label for="customer_account">Customer Account #: <span class="required">*</span></label>
        &lt;?php echo form_error('customer_account'); ?&gt;</td></tr>
        <tr><td>
        &lt;?php $options = array('SC Install' =>'SC Install',
         'SC Service Call'=>'SC Service Call',
         'Former Install'=>'Former Install',
         'New Install'=>'New Install',
         'Commercial Install'=>'Commercial Install',
         'Service Call'=>'Service Call',
         'Upgrade'=>'Upgrade');
        echo form_dropdown('work_order_type',$options, set_value('work_order_type')) ?&gt;
</td><td colspan=2>&lt;?php echo form_input('customer_account', set_value('customer_account'))?&gt;
</td></tr>
<tr><td>                                        
        <label for="receiver_type">Receiver Type:</label>
        &lt;?php echo form_error('receiver_type'); ?&gt;
        </td>
        <td colspan=2><label for="access_card">Access Card #:</label>
        &lt;?php echo form_error('access_card'); ?&gt;</td></tr>
        <tr><td>
        &lt;?php
        $options = array(
                  ''=>'Select One',
                  'Standard'  => 'Standard',
                  'DVR'    => 'DVR',
                  'HD'   => 'HD',
                  'HD_DVR' => 'HD DVR',
                );
                echo form_dropdown('receiver_type',$options, set_value('receiver_type')) ?&gt;</td>
       <td colspan=2>
       &lt;?php echo form_input('access_card', set_value('access_card')) ?&gt;</td>
       </tr>
       <tr><td colspan=3>&lt;input type="hidden" id="recidnum" name="recidnum" value="1" /&gt;

<div id="newfields"></div>

<div>
&lt;input name="add" type="button" class="button" id="add" value="Add Another Receiver"&gt;
</div></td></tr>
       <tr><td colspan='3'><label for="dish_type_consumed">Dish Type Consumed:</label>
        &lt;?php echo form_error('dish_type_consumed'); ?&gt;</td>
       </tr>
       <tr>
       <td colspan=3>&lt;?php $options = array(''=>'Select One','Standard/18x20'=>'Standard/18x20',
       'KAKU'=>'KAKU',
       'International/36 inch'=>'International/36 inch');
       echo form_dropdown('dish_type_consumed', $options, set_value('dish_type_consumed'));
       ?&gt;
       </td></tr>
       <tr><td colspan=3>&lt;input type="hidden" id="dishidnum" name="dishidnum" value="1" /&gt;
       <div id="newDish"></div>

<div>
&lt;input name="add" type="button" class="button" id="add" value="Add Another Dish"&gt;
</div></td></tr>
       <tr><td colspan=3><label for="multiswitch_consumed">MultiSwitch Consumed:</label>
        &lt;?php echo form_error('multiswitch_consumed'); ?&gt;</td></tr>
        <tr><td colspan=3>&lt;?php $options = array(''=>'Select One', '4x8'=>'4x8', '6x8'=>'6x8');
        echo form_dropdown('multiswitch_consumed', $options, set_value('multiswitch_consumed'))?&gt;</td>
        </tr>
        <tr><td><label for="payment_for">Payment For:</label>
        &lt;?php echo form_error('payment_for'); ?&gt;</td>
        <td><label for="amount">Amount:</label>
        &lt;?php echo form_error('amount'); ?&gt;</td>
        <td><label for="comissioned">Commissioned:</label>
        &lt;?php echo form_error('comissioned'); ?&gt;
        
        </td></tr>
        <tr><td>&lt;?php echo form_input('payment_for', set_value('payment_for')); ?&gt;</td>
        <td>&lt;?php echo form_input('amount', set_value('amount'));?&gt;</td>
        <td>&lt;?php echo form_checkbox('commissioned', '1'); ?&gt;</td>
        </tr>
        <tr><td colspan=3>&lt;input type="hidden" id="payidnum" name="payidnum" value="1" /&gt;
       <div id="newPay"></div>

<div>
&lt;input name="add" type="button" class="button" id="add" value="Add Another Payment Item"&gt;
</div></td></tr>
        <tr><td colspan=3><label for="detail">Detail any custom labor or parts:</label>
    &lt;?php echo form_error('detail'); ?&gt;</td></tr>
        <tr><td colspan=3>&lt;?php $data = array(
              'name'        => 'detail',
              'id'          => 'detail',
              'value'       => set_value('detail'),
              'rows'        => '5',
              'cols'        => '50',
              'style'       => '',
            );
            echo form_textarea($data);
        ?&gt;
        </td></tr>
        <tr><td colspan=3><label for="notes">Notes:</label>
    &lt;?php echo form_error('notes'); ?&gt;</td></tr>
        <tr><td colspan=3>&lt;?php $data = array(
              'name'        => 'notes',
              'id'          => 'notes',
              'value'       => set_value('notes'),
              'rows'        => '5',
              'cols'        => '50',
              'style'       => '',
            );
            echo form_textarea($data);
        ?&gt;
        </td></tr>
        <tr><td colspan=3>
        &lt;?php echo form_submit( 'submit', 'Submit'); ?&gt;
</td></tr></tbody>
</table>
&lt;?php echo form_close(); ?&gt;
&lt;/body&gt;

#4
[eluser]liz217[/eluser]
from controller:

Code:
function sub()
    {            
            if(!$this->session->userdata('logged_in')){
            //header('Location: /login/index');
            redirect('/login/index','location',301);
        }
        $this->form_validation->set_rules('work_order_type','Work Order Type:','required|xss_clean');            
        $this->form_validation->set_rules('customer_account','Customer Account #:','required|is_numeric');            
        $this->form_validation->set_rules('receiver_type','Receiver Type:','xss_clean');    
        $this->form_validation->set_rules('receiver_type','Receiver Type:','xss_clean');        
        $this->form_validation->set_rules('access_card','Access Card #:','is_numeric|trim');
        $this->form_validation->set_rules('serial_num','Serial #:','');    
        $this->form_validation->set_rules('serial_num_D','Serial #:','');            
        $this->form_validation->set_rules('dish_type_consumed','Dish Type Consumed:','');            
        $this->form_validation->set_rules('multiswitch_consumed','MultiSwitch Consumed:','');            
        $this->form_validation->set_rules('payment_for','Payment For:','trim|xss_clean');            
        $this->form_validation->set_rules('comissioned','Comissioned:','is_numeric');                    $this->form_validation->set_rules('amount','Amount:','is_numeric');            
        $this->form_validation->set_rules('detail','Detail any custom labor or parts:','xss_clean');            
        $this->form_validation->set_rules('notes','Notes:','required|trim|xss_clean');
            
        $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
    
        if ($this->form_validation->run() == FALSE) // validation hasn'\t been passed
        {
        $data='';
            $this->load->view('myform_view', $data);
        }
        //else // passed validation proceed to post success logic
        //{
             // build array for the model
            $db_saved=FALSE;
            $form_data='';
            $form_consumables ='';
            $form_pay ='';
            $tbl='';
            $EOD_ID='';

            $form_data = array(
                               'work_order_type' => set_value('work_order_type'),
                               'customer_account' => set_value('customer_account'),
                               'detail' => set_value('detail'),
                               'notes' => set_value('notes'),
                            'user_id' => $this->session->userdata('username')
                        );
            if ($this->myform_model->SaveForm('myform',$form_data) != FALSE) // the information has therefore been successfully saved in the db
            {
            //$this->myform_model->db->insert_id();
                $db_saved=TRUE;   // or whatever logic needs to occur
                $EOD_ID=$this->myform_model->SaveForm('myform',$form_data);
            }
                    else
            {
                $db_saved=$this->myform_model->SaveForm('myform',$form_data);
                echo '<br>db saved = ';
                echo $db_saved;
            }
            $form_receiver = array(        
                            'EOD_ID'=>$EOD_ID,                  
                            'product_cat'=>'receiver',
                            'item_name' => set_value('receiver_type'),
                               'serial' => set_value('access_card'),
                            'wb_serial' => set_value('serial_num')
            );
            if ($this->myform_model->SaveForm('eod_consumed',$form_receiver) != FALSE) // the information has therefore been successfully saved in the db
            {
                $db_saved=TRUE;   // or whatever logic needs to occur
            }
                    else
            {
                $db_saved=$this->myform_model->SaveForm('eod_consumed',$form_receiver);
                echo '<br>db saved = ';
                echo $db_saved;
            }
            $form_dish = array(
                               'EOD_ID'=>$EOD_ID,
                               'product_cat'=>'dish',
                            'item_name' => set_value('dish_type_consumed'),
                            'wb_serial' => set_value('serial_num_D')
            );
            if ($this->myform_model->SaveForm('eod_consumed',$form_dish) != FALSE) // the information has therefore been successfully saved in the db
            {
                $db_saved=TRUE;   // or whatever logic needs to occur
            }
                    else
            {
                $db_saved=$this->myform_model->SaveForm('eod_consumed',$form_dish);
                echo '<br>db saved = ';
                echo $db_saved;
            }
            $form_multi = array(
                            'EOD_ID'=>$EOD_ID,
                               'product_cat'=>'multiswitch',
                            'item_name'=> set_value('multiswitch_consumed')
                        );
            if ($this->myform_model->SaveForm('eod_consumed',$form_multi) != FALSE) // the information has therefore been successfully saved in the db
            {
                $db_saved=TRUE;   // or whatever logic needs to occur
            }
            else
            {
                $db_saved=$this->myform_model->SaveForm('eod_consumed',$f

#5
[eluser]liz217[/eluser]
myform_model:

Code:
&lt;?php

class Myform_model extends Model {

    function __construct()
    {
        parent::Model();
    }
    
    // --------------------------------------------------------------------

      /**
       * function save_form()
       *
       * insert form data
       * @param $form_data - array
       * @return Bool - TRUE or FALSE
       */
function insert_multiple($table,$data)
{
   foreach($data as $values)
   {
      $this->db->insert($table, $values);
   }
}
    function SaveForm($tbl,$form_data)
    {
        $this->db->insert($tbl, $form_data);
        
        if ($this->db->affected_rows() == '1')
        {
            $ID = $this->db->insert_id();
            return $ID;
        }
        
        return FALSE;
    }
}
?&gt;

#6
[eluser]eoinmcg[/eluser]
liz217,

please use code tags otherwise no one will bother trying to read your code.

a couple of things i'd do to try and find the problem
- what were the two fields you added? it seems to me that this is where the problem is.
- put this in your view to see what errors are bring generated
Code:
&lt;?php
if(function_exists(validation_errors))
{
echo validation_errors();
}
?&gt;
- make sure full error reporting is turned on while developing
Code:
error_reporting(E_ALL);
if you're using php5 go one better and
Code:
error_reporting(E_STRICT);

#7
[eluser]Ki[/eluser]
Just a bit off topic:
I found it much cleaner an easier to work with form data when I moved all my form validation rules into form_validation config file. This takes out unnecessary clutter out of controllers.
Also, if you use a lot of forms throughout the website, autoloading the form and setting up your error delimiters once in MY_Controller provides additional organisation
With respect to your problem:
Try to add the 2 new fields to the form rules, but make those rules so they are passed regardless if there is any user input such as:

$this->form_validation->set_rules(‘NEW FIELD',‘Dish Type Consumed:’,'trim');

#8
[eluser]Ki[/eluser]
And please, edit your previous posts and put the code inside the code tags if you really need help with this. ;-)

#9
[eluser]liz217[/eluser]
Ok. Posts have been edited. Thanks for the tip!

The 2 fields that were added were serial_num[] and serial_num_D. I also added the 2 values to existing drop boxes, Wile Blue Modem was a value added to the receivers drop down and the Wild Blue Tria value was added to the dish drop down.

#10
[eluser]liz217[/eluser]
I added this to the top of the myform_view.php file.
Code:
&lt;?php
error_reporting(E_ALL);
if(function_exists(validation_errors))
{
echo validation_errors();
}
?&gt;

I don't see any errors come back on the page or in the firefox error console.

I also set the form validation to trim the 2 new fields:

Code:
$this->form_validation->set_rules('serial_num','Serial #:','trim');    
$this->form_validation->set_rules('serial_num_D','Serial #:','trim');

I still can't get past the validation even with the trim there.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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