Welcome Guest, Not a member yet? Register   Sign In
set_value() problem
#1

[eluser]macigniter[/eluser]
I hope anyone can help me with this form re-population issue. I have no clue why set_value() doesn't work in my view file to insert the value after validation. Can anyone help?

This is my controller:
Code:
$this->load->library("form_validation");
$this->form_validation->set_error_delimiters("<span class=\"error\">- ", "</span>");        
$this->form_validation->set_rules('user', 'User Name', 'trim|required|alpha|min_length[5]|max_length[30]|xss_clean');
$this->form_validation->set_rules('pass', 'Password', 'trim|required|alpha|min_length[6]|max_length[30]|xss_clean');

$data['form'] = $this->load->view('form/registration', '', TRUE);

if (!isset($_POST['submit']))
{    
    // form not yet submitted
    $this->template->parse_view('content', 'login/registration', $data);
}
else if ($this->form_validation->run() == FALSE)
{    
    // form submitted with errors
    $data['errors'] = validation_errors();
    $data['errors'] = $this->parser->parse('form/error', $data, TRUE);
            
    $this->template->parse_view('content', 'login/registration', $data);
}
else
{    
    // form successfully submitted
    $this->template->parse_view('content', 'login/registration', $data);
}

$this->template->render();

This is the content of my view file:
Code:
$form  = form_open("/login/register");
$form .= form_fieldset();

// USER NAME
$value = set_value('user');
$field = array(
        "name"     => "user",
        "size"    => 10,
        "class"    => "text",
        "value" => $value
);
$form .= form_input($field);
$form .= "<br />";

// PASSWORD
$form .= form_label("<b>Password</b>", "pass");
$field = array(
        "name"     => "pass",
        "size"    => 10,
        "class"    => "text"
);
$form .= form_password($field);        
$form .= "<br />";
    
// SUBMIT
$field = array(
        "name"     => "submit",
        "class"    => "button",
        "style"    => "margin-left: 150px;",
        "value" => "Login"
);        
$form .= form_submit($field);
    
$form .= form_close();

echo $form;

Why doesn't the $value in the view file contain the submitted data of the 'user' field? It works perfectly fine if I use $value = $this->input->post('user') instead.
#2

[eluser]ryeguy[/eluser]
I know, I had this same issue also. Something in the function causes it to break. The only solution I came to was going into formhelper.php and changing the definition of set_value to this:
Code:
if ( ! function_exists('set_value'))
{
    function set_value($field = '', $default = '')
    {
            if ( ! isset($_POST[$field]))
                return $default;
            else
                return form_prep($_POST[$field]);
    }
}

I don't know why the original code does not work. This does the same thing, actually works, and doesn't change functionality.
#3

[eluser]Hartimer[/eluser]
[quote author="ryeguy" date="1233268956"]I know, I had this same issue also. Something in the function causes it to break. The only solution I came to was going into formhelper.php and changing the definition of set_value to this:
Code:
if ( ! function_exists('set_value'))
{
    function set_value($field = '', $default = '')
    {
            if ( ! isset($_POST[$field]))
                return $default;
            else
                return form_prep($_POST[$field]);
    }
}

I don't know why the original code does not work. This does the same thing, actually works, and doesn't change functionality.[/quote]

I ran into this problem today too. Something wrong with the lib maybe?

Anyway, ryeguy your code does indeed work, but it is very vulnerable to xss scripting.. My suggestion, which worked to, is
Code:
function set_value($field = '', $default = '')
    {        
        if ( ! $this->CI->input->post($field, TRUE)){
                return $default;
          }
          return form_prep($this->CI->input->post($field, TRUE));
    }

You can't work with "isset" since input->post always returns a value. If $_POST is not set it returns FALSE, so it is a straight forward change..

If anyone happens to know what the problem of set_value() is PLEASE let me (us) know!
#4

[eluser]Hartimer[/eluser]
Check this thread: http://ellislab.com/forums/viewthread/103816/

Apparently, for set_value() to work you MUST set a rule for that field, an empty rule will do..
#5

[eluser]macigniter[/eluser]
I actually DID set a rule. Please see my code above.
#6

[eluser]ryeguy[/eluser]
[quote author="Hartimer" date="1233277395"][quote author="ryeguy" date="1233268956"]I know, I had this same issue also. Something in the function causes it to break. The only solution I came to was going into formhelper.php and changing the definition of set_value to this:
Code:
if ( ! function_exists('set_value'))
{
    function set_value($field = '', $default = '')
    {
            if ( ! isset($_POST[$field]))
                return $default;
            else
                return form_prep($_POST[$field]);
    }
}

I don't know why the original code does not work. This does the same thing, actually works, and doesn't change functionality.[/quote]

I ran into this problem today too. Something wrong with the lib maybe?

Anyway, ryeguy your code does indeed work, but it is very vulnerable to xss scripting.. My suggestion, which worked to, is
Code:
function set_value($field = '', $default = '')
    {        
        if ( ! $this->CI->input->post($field, TRUE)){
                return $default;
          }
          return form_prep($this->CI->input->post($field, TRUE));
    }

You can't work with "isset" since input->post always returns a value. If $_POST is not set it returns FALSE, so it is a straight forward change..

If anyone happens to know what the problem of set_value() is PLEASE let me (us) know![/quote]

Just a heads up, the TRUE part of your post() function isn't needed. post() returns FALSE if the value is not in $_POST.

Also, I didn't remove xss scripting protection. The function was vulnerable in its untouched state too then.
#7

[eluser]Hartimer[/eluser]
Anyway, add an empty rule to the form_whatever and set_value() will probably work just fine..
#8

[eluser]whobutsb[/eluser]
Hello All,
I have been looking for a solution to using Form Validation and being able to create my form dynamically with in the controller for a while now. I think I'm really close with this thread. I switched the my set_value() method with the one listed above. I was wondering if anyone could provide some guidance in doing the same for the set_radio, set_dropdown, and set_checkbox.

I have been working on specifically the set_radio() method and i'm not sure how this specific line of code works:
Code:
$OBJ =& _get_validation_object();

        if ($OBJ === FALSE)
        {

It seems to always evaluate as false. I made sure that I setup blank rules for the fields that are not required as well

Thank you for the help,
Steve
#9

[eluser]tison[/eluser]
Has anyone found a solution for this besides adding empty rules? Subscribing...




Theme © iAndrew 2016 - Forum software by © MyBB