Part of the EllisLab Network
   
 
FINALLY using xajax with CI’s Form Validation!
Posted: 10 April 2009 10:37 AM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  200
Joined  07-31-2008

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!

// In the form
<input type="text" id="field1" name="field1" />
<
input type="button" value="Submit">

// 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 snake

 Signature 

Spam Helper | Html Helper | GPoll Library | IMAP Library

Profile
 
 
Posted: 10 April 2009 12:08 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2520
Joined  02-19-2009

I don’t use xajax, but this looks like it could easily be adapted for another js framework.  Thank you, Im sure I will be giving your method a try!

 Signature 
Profile
 
 
Posted: 10 April 2009 12:50 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  200
Joined  07-31-2008

For the doubters that are pm’ing me saying this won’t work, I have a working example here. Scroll down to the xajax section. The code is also shown there raspberry

 Signature 

Spam Helper | Html Helper | GPoll Library | IMAP Library

Profile
 
 
Posted: 11 May 2009 07:33 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  13
Joined  11-15-2007

works! thanks!

Profile