[eluser]Iverson[/eluser]
I use xajax for all my ajax stuff so when I want to do form validation, I have to sit there and create my own "if(strlen($field) < 4)..." kinda crap. Well, since CI's Form Validation looks at the global $_POST variable, all you have to do is take the array you get from xajax.getFormValues() and iterate through it, setting the $_POST variable's index to the field name and the value to the element! Simplicity is the ultimate sophistication!
Code:
// In the form
<input type="text" id="field1" name="field1" />
<input type="button" value="Submit" onclick="xajax_submit_function(xajax.getFormValues('form-id'));" />
// In the controller
function submit_function($data)
{
$this->load->library('form_validation');
// Instantiate the object
$resp = new xajaxResponse();
$this->form_validation->set_rules('field1', 'The first', 'required');
foreach($data as $id => $field)
{
// send each field to the global POST var
$_POST[$id] = $field;
}
if ($this->form_validation->run() == FALSE)
{
// By default, CI adds a paragraph tag to each error. Let's take that out...
$this->form_validation->set_error_delimiters('', '');
$resp->alert(validation_errors()); // Returns an alert box saying "The first field is required."
}
else
{
// Do something
}
return $resp;
}
Of course, this isn't all the steps you need to take to get XAJAX working, but I assume if you're trying to tackle form validation, you've passed those points already. This is going to make my life SO much easier
nake: