1
votes

I know to use set_value() for setting default values in a CodeIgniter form, but how can I set the default values for the validation if either the value isnt submitted or its before a form submission?

public function index($uri = NULL) 
{
    $this->load->helper('form');
    $this->load->library('form_validation'); 

    //pull from DB or Session
    $data = array( 
            'Status' => 'users_default_status',
            'Order' => 'users_default_order',
            'Asc' => 'users_default_asc'
    );

    $this->form_validation->set_data($data);

    $this->form_validation->set_rules('Status', 'Status', 'numeric|trim|required|strtolower');
    $this->form_validation->set_rules('Order', 'Order', 'trim|required');
    $this->form_validation->set_rules('Asc', 'Asc', 'trim|required|strtolower');

    if ($this->form_validation->run() === FALSE) // validation failed
    {
        // validation failed but maintain submitted values in form/feedback

    }else{
        // validation successful, do whatever
    }
}

So that if there was a form submission it uses the POST values, and if not it uses defaults (but still validated). I would like the defaults to work on a variable by variable basis, so some can be defaults some can be user submitted.

2
I think it is not available by Codeigniter. You should check value by the code or extend validation class - splash58
$this->form_validation->set_data($data); // throws error ? - Daniel Adenew

2 Answers

3
votes

I know to use set_value() for setting default values in a CodeIgniter form, but how can I set the default values for the validation if either the value isn't submitted or it's before a form submission?

Simply check if the value exists and use it as the default, otherwise it's blank (or a different default).

In the Controller, decide if you're going to load a blank form, if not, send the data for the fields....

$data['fieldname'] = "whatever"; // from the database

$this->load->view('yourpage', $data);

Then in your View, check for the existence of this data for each field. If the data was sent, use it. Otherwise, set a blank value.

<?php $value = isset($fieldname) ? $fieldname : ''; ?>

<input name="fieldname" value="<?php  echo set_value('fieldname', $value); ?>" type="text" />
  • If you do not send the data from the Controller, the field will be blank (you could also set a default)

  • If you send the data from the Controller, the field will be filled out with this data from your Controller (database).

  • If you submit the form and validation fails, set_value() function will reload the field with the data from the most recent post array.

2
votes

These are just some thoughts...

The validation rules act upon the posted data. So if you are using set_value('order',$default_order), when the form is submitted it will either take the new user entered value or the one you provided.

If the user empties a prefilled or default input, you can't have it set as "required" in the rules. What you would do is use a callback function to handle that case to check if it's empty and provide a default value and return TRUE.