Welcome Guest, Not a member yet? Register   Sign In
Can I apply validation rules in a fragment of data fields with ajax calls?
#1

[eluser]chzigkol[/eluser]
Hi,

I use jQuery and ajax calls to fill/edit a form within a view.

I have several block of divs which update specific attributes in my DB.

With ajax calls I do proceed to some actions without refreshing my page (hey this is Ajax Tongue after all)

I was wondering if CI can provide me with a validation on each individual block of divs. I'm not talking about validating the whole form. I know how to do that, i suppose.

For instance i have a div that has two text fields and one "Done" button. When I clicked that button a specific function of one of my controllers is called via ajax call. But, what about validation on these two text fields?

Hmmmm....What about loading the validation library in the controller function?

Well I've tried this but whatever I've done, the following if statement

$this->form_validation->run() == FALSE

was always TRUE. Fail or something that I haven't noticed?

Thank you in advance.

Christos
#2

[eluser]rogierb[/eluser]
Seems you are doing everyting right. This is exeactly how I validate throught ajax.
So post your ajax controller, maybe there's something off. Maybe not all the fields are posted.
Please a var_dump of the POST array aswell

Edit: did you include rules in the controller?
#3

[eluser]chzigkol[/eluser]
ok I'll give it one more try.

I'll come back if my troubles still remain
#4

[eluser]chzigkol[/eluser]
I haven't find a solution yet Sad

Can you explain your way by giving a generic example?

Thanks

Christos

[quote author="rogierb" date="1259614468"]Seems you are doing everyting right. This is exeactly how I validate throught ajax.
So post your ajax controller, maybe there's something off. Maybe not all the fields are posted.
Please a var_dump of the POST array aswell

Edit: did you include rules in the controller?[/quote]
#5

[eluser]rogierb[/eluser]
in your ajax controller:

Code:
public function do_something()
{
//for test purposes:
print_r($_POST);
// end test purpose
if(isset($this->input->post(somevar)))
{
  $this->load->library('form_validation');
  $this->form_validation->set_rules('somevar',             'lang:lng_u_somevar',          'trim|required');
  $this->form_validation->set_rules('description',     'lang:lng_u_description',     'trim');
  $this->form_validation->set_rules('status',         'lang:lng_u_status',         'trim');
        
  if($this->form_validation->run() == FALSE )
  {
   $data['error'] = validation_errors();
  }
  else
  {
   //no error so go ahead
  }
} else echo "error! no post vars!"
$this->load->view('default_view',$data);
}

Just make sure you have all the post vars and all your rules defined.
#6

[eluser]chzigkol[/eluser]
Echoing the validation errors from the controller solved my problems.

Thank you

Christos




Theme © iAndrew 2016 - Forum software by © MyBB