Welcome Guest, Not a member yet? Register   Sign In
form_validator ci3 set_value() and set_data() not returning data unless 3rd param set

(This post was last modified: 12-12-2019, 02:38 PM by antidote.)

Sorry, I accidentally deleted the thread Sad here it is again ... !

Update: Further testing has shown it's not just 'required'. If the 3rd parameter in absent, the data does not populate set_value(). I have put a 'trim' in the 3rd and the data came through. Very odd.

I've just registered so I can get a little help with this issue I have with Codeigniter 3 (love the framework btw):

Senario is; I have a form that I'm using for modifying records in a database - fairly standard. I'm using the set_value() option in the view to return the values to the form if they fail validation - all good to here. I also want to call this modify form from other controllers - again all good. 

However, the initial starting values I want to pull from the database, then each submit after I want to use the validator entries (to make sure the re-displayed values are the ones the user has entered, and not the original values from the database). So my problem is, if I pass in values from the db, the first submit is fine, but then the db values overwrite the failed validation user entry. Ok, so I can get around this by using CodeIgniter3's set_data() function passing in the db vallues for use in the first submit, allowing me to first load the db values into the form_validator on the first iteration of displaying the form. I should mention, I have 6 inputboxes on the form. So the problem arises, before the first submit, 4 of the inputboxes are auto filled and good, the other two are blank. All logic is the same for all of the inputboxes!

I ran a series of tests and found a bug in the logic (I think - please correct me if I' wrong). The 4 inputboxes that get the data okay are all 'required' in the validator rules, the other two inputboxes are not required (as in, they are optional. If the user enters data, I want that data but they dont HAVE to enter data). If I change the 2 blank inputboxes to 'required' in the rules, low and behond, that data comes through and the 2 inputboxes are populated correctly?

I've ran the test a few times with consistent results each time so I wonder if anyone else has had this issue and whether or not this is expected behaviour. The help files state the validator can be used to validate non $POST data so I would have expected this to work for all the inputboxes regardless if the validation rule is 'required' or not.

Any help appreciated,

PHP Code:
if ( ! function_exists('set_value')) {
        * Form Value
        * Grabs a value from the POST array for the specified field so you can
        * re-populate an input field or textarea. If Form Validation
        * is active it retrieves the info from the validation class
        * @param    string    $field        Field name
        * @param    string    $default    Default value
        * @param    bool    $html_escape    Whether to escape HTML special characters or not
        * @return    string
    function set_value($field$default ''$html_escape TRUE)
        $CI =& get_instance();
        $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
        isset($value) OR $value $default;
        return ($html_escape) ? html_escape($value) : $value;

Ok, so I deleted the last thread just as I found the problem !! Hopefully the chap helping will pick this up. The problem is thus:

PHP Code:
&& $CI->form_validation->has_rule($field))



Because to trigger TRUE, there has to be a rule (or the object has existence). In my case, no rule (required, trim, etc) --> it's an optional field in the form. So no rule means data will be passed from the input->post() function --> in my case, set_data() applied the data directly to the form_validation object. On the first iteration of the loading of the form, POST is empty for this field. 

I also should mention, I pass in a "dummy" validation test that will definitely fail (i.e. data ('dummy','','required'). There is no input object behind it, it's just a rule. This rule is applied after the set_data() function has been populated. This stops another issue arising; if you pass wholly valid data to a modify form, on the first load, it passes validation and exits the form! Not ideal. So the dummy rule basically ensures the form routine doesn't "run on" after setting the data.

Senario (working)
If I set the 3rd parameter in $this->form_validation->set_rules('remark', 'Remark', 'trim');  <-- can be any rule

Senario (not working)
$this->form_validation->set_rules('remark', 'Remark');  

So, the first two parameters being set for the "form_validation->set_rules" function means Has_rules will return false, therefore giving POST data which is empty.

Theme © iAndrew 2016 - Forum software by © MyBB