Welcome Guest, Not a member yet? Register   Sign In
jquery post & ci form validation
#1

[eluser]yesov[/eluser]
Hi.

situation:
in js i use
Code:
$.post( path, {a: obj, type_id: "1"}, function(data){alert(data)});
and obj is a json string
question: how i can use form validation for different obj properties after decode it in ci controller ?

any ideas ?!
#2

[eluser]Nick Husher[/eluser]
It's maybe slightly bad practice, but you'll need to use a custom validation rule that's described about halfway down the Form Validation page. In the controller method you're using to decode the ajax request, add this validation rule:

Code:
$this->form_validation->set_rules('a','JSON Object','required|callback__handle_object');

And in the same controller, add this function:
Code:
function _handle_object($json_object) {
    $json_object = json_decode($json_object);

    // json_decode returns null or a string if the json given to it is
    // invalid. see:
    // http://us2.php.net/manual/en/function.json-decode.php#86181
    $isValid = ($json_object != null && !is_string($json_object));
        
    // run validation, setting $isValid to false if anything
    // is out of place.
    return $isValid;
}

This code is untested, so it might need some massaging.
#3

[eluser]gon[/eluser]
The object is sent to the server as a POST request, so standard CI validation will work.

you just have to set field_rules and field_names for variables "a" and "type_id", as if they were sent from a form.
#4

[eluser]Nick Husher[/eluser]
I believe my example does just that. The JSON string comes in as postdata, but because it's a string it will be very difficult to run sophisticated validation operations on it. You need to design a custom validation function that will decode the JSON string and decide if it's a valid object or not. Note that within the validation callback, you can call other controller methods to make your code a little cleaner.
#5

[eluser]yesov[/eluser]
to Nick: i know about create a custom validation rules, but i want to use "standart",
for example: "trim|required|min_length[5]|max_length[12]|xss_clean"
Code:
...
$data = json_decode($this->input->post('a'));
...
and after this $data is
Code:
Array
(
    [0] => stdClass Object
        (
            [link] => http://example.com
            [title] => test web page
        )

)

and i interesting, can i use "standart" ci validation for my object properties (link, title ....) ?
and can i use this code for validate ? (and what in $fields array?)
Code:
$rules = array(
    "title" => "min_length[10]"
);
$fields = array(
    "" => ""
);
$this->validation->set_rules($rules);
$this->validation->set_fields($fields);

if ($this->validation->run()) {
    $result =  .... ?
}

sorry for my english Wink
#6

[eluser]Nick Husher[/eluser]
I don't think such a thing is possible without somehow decoding the JSON and inserting it into the $_POST associative array. The best way to do that might be by extending the Validation library with a MY_Validation class.

The code might look something like this:

Code:
//php
foreach($_POST as $key=>$property) {

    // json_decode returns null or a string if the json given to it is
    // invalid. see:
    // http://us2.php.net/manual/en/function.json-decode.php#86181
    //
    // the second argument forces json_decode to use an associative array
    // instead of a stdClass Object
    $json_object = json_decode($_POST[$property], true);
    $is_json = ($json_object != null && !is_string($json_object));

    if($is_json) {
        array_merge($_POST, $json_object);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB