jquery post & ci form validation |
[eluser]yesov[/eluser]
Hi. situation: in js i use Code: $.post( path, {a: obj, type_id: "1"}, function(data){alert(data)}); question: how i can use form validation for different obj properties after decode it in ci controller ? any ideas ?!
[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) { This code is untested, so it might need some massaging.
[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.
[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.
[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: ... Code: Array 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( sorry for my english
[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 |
Welcome Guest, Not a member yet? Register Sign In |