开发者

Callback Validation

I am having an issue trying to display the form validation error on the view using a callback validation function. If the user selects 'Yes' for the vehicle option, the form should check for other mandatory fields related to that vehicle option field.

In my controller I have a simple function that outputs the form and gets data back from the form:

function new_customer_record()
{
    if ($this->form_validation->run() == FALSE)
    {
        // data array has values to be passed to the customer_form_view
        $this->load->view('customer_form_view',$data); 
    }else{
        //get data from the post method and save it to the database
    }
}

In the config folder, I have a file called from_validation.php that has the array filled with validation rules and the callback function as shown below:

$config = array (
    'employee/new_record' => array(
        array (
          'field' => 'first_name',
          'label' => 'First Name',
          'rules' => 'required',
        ),
        array (
          'field' => 'vehicleOwn',
          'label' => 'Own a vehicle',
          'rules' => 'required|callback_checkVehicleInfo',
        ),
    ),
);


function checkVehicleInfo($str){
    if ($str == "Yes"){

        $config = array ( 'employee/new_record' => array(

            array (
                'field' => 'vehicle_model',
                'label' => 'Vehicle Model',
                'rules' => 'required'
            ),
            array (
                'field' => 'vehicle_rego',
                'label' => 'Vehicle Rego',
                'rules' => 'required'
            ),
            array (
                'field' => 'vehicle_type',
                'label' => 'Vehicle Type',
                'rules' => 'requir开发者_如何学Ced'
            ),
        ),
    );
    $this->form_validation->set_message('checkVehicleInfo','Please enter your vehicle information');
    return FALSE;
}else{
    return TRUE;
}
}

In the view, i have something similar to this for each field that has 'required' validation rule - other fields work fine except the vehicle mandatory fields (in the callback function):

<?php $vehicle_model = @field($this->validation->vehicle_model, $customer-                      >vehicle_model); ?>
<tr>
<td><label>Vehicle Model</label></td>
<td><input name="vehicle_model" type="text" value="<?php echo ($edit=== true)?     $vehicle_model :set_value('vehicle_model'); ?>" size="20" maxlength="20">
<?php echo form_error('vehicle_model'); ?></td>
</tr>

But I am not getting any error messages displayed, the validations is not working either.


Right after new_customer_record() function, you should add the callback function (not in config, but put checkVehicleInfo function in your controller).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜