Welcome Guest, Not a member yet? Register   Sign In
jQuery.serialize() and form_validation [solved]
#1

[eluser]juanchorossi[/eluser]
Hi Guys!
After hours of reading the forum, I couldn't get the solution to my problem, similar to audun1's (jquery.serialize() codeigniter does not get value).

The problem: I pass the form variables as a string with jQuery.serialize() function, but I can't validate those fields.

The View JS:

Code:
function sendForm()
{
    var formParams = $("form").serialize();
    
    jQuery.facebox(function() {
        $.post('calendario/evento_submit/',
        { params: formParams },
        function(data) {
            $.facebox(data)
        })
    });
}

The Controller:
Code:
function evento_submit()
{    
        parse_str($this->input->post('params'));

        // $_POST['tema'] = ($tema) ? $tema : ''; Validates Ok
        
        $this->form_validation->set_rules('fechaEventoDia', 'Día','noZero|xss_clean|required');
        $this->form_validation->set_rules('fechaEventoMes','', 'noZero|xss_clean|required');
        $this->form_validation->set_rules('fechaEventoAno','', 'noZero|xss_clean|required');
        $this->form_validation->set_rules('allDay','', 'xss_clean');
        $this->form_validation->set_rules('tema','Tema', 'xss_clean|required');

...

}

The only solution I could find was to manually set $_POST keys to the parsed ones. Is there another way?
Suggestions are welcome!

Thanks in advance!
#2

[eluser]tomcode[/eluser]
parse_str() You can use if You have a (GET) query string (?a=b&c=d) which You want to transform into an array.

Also, parse_str() requires a second parameter, the array to hold the resulting array.

In Your case, can't You just use unserialize() ?

Code:
$form_params = unserialize ($this->input->post('params'));
#3

[eluser]juanchorossi[/eluser]
Thanks! I put $_POST as second parameter on the parse_str and worked fine!

Code:
parse_str($this->input->post('params'), $_POST);
#4

[eluser]tomcode[/eluser]
Ah, good to know Wink




Theme © iAndrew 2016 - Forum software by © MyBB