set_value() default value not working, if the rules if not set. #2394

Closed
keatliang2005 opened this Issue Apr 9, 2013 · 6 comments

Projects

None yet

3 participants

@keatliang2005

system/helpers/form_helper.php

47ea5a8

function set_value($field = '', $default = '', $is_textarea = FALSE)
{
$CI =& get_instance();

$value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field)) // the has rule call is the culprit
? $CI->form_validation->set_value($field, $default)
: $CI->input->post($field, FALSE);

return form_prep($value === NULL ? $default : $value, $is_textarea);
}

usually i pull data from db the set default value.... only set different dynamic rule based on post value.....

with this changes it makes the default value not working anymore.

is this a bug or by design ?? must set all the rules even for first load of the page are not need to run validation.

@cryode
Contributor
cryode commented Apr 9, 2013

Can you show us the code you're using?

@keatliang2005

here's my sample code on controller

<?php

$validation = array(
    array(
        'name' => 'username',
        'label' => 'Username',
        'rules' => 'required|xss_clean'
    )
)

$user = $this->user_model->get(); //return string or FALSE on fail

$user or exit;

if($this->input->post('submit'))
{
    if($this->form_validation->set_rules($validation)->run())
    {
        //do what ever after validate
    }
}

$this->load->view('the_view', array('username'=> $user));

and this is on the view

<?= form_open() ?>

Username: <?= form_input('username', set_value('username', $user)) ?>     <!-- default value not display -->

<?= form_submit('submit','submit'); ?>

<?= form_close() ?>

the problem is, when first load of the page while the rule is not set it will not work for the default value.

@cryode
Contributor
cryode commented Apr 10, 2013

You're passing the variable $username to your view, but then you're trying to access $user. Fixing that, your example works perfectly for me. Make sure you have error display turned on in your PHP.ini and in your index.php file, because it probably would've warned you about that. 😉

@keatliang2005

opps my mistake..... about the $user at the view file.

what i mean be first load of the page before post. the default value is not working ....

@cryode
Contributor
cryode commented Apr 10, 2013

Yes it does. Make sure your CI version is up-to-date, and that your code itself isn't the issue.

@keatliang2005

hym... i look up to my code again. may be it's fixed LOL

On Wed, Apr 10, 2013 at 10:42 AM, Eric Roberts notifications@github.comwrote:

Yes it does. Make sure your CI version is up-to-date, and that your code
itself isn't the issue.


Reply to this email directly or view it on GitHubhttps://github.com/EllisLab/CodeIgniter/issues/2394#issuecomment-16152385
.

by Goh Keat Liang
http://90303.blogspot.com

@narfbg narfbg closed this Apr 10, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment