-2

i am using zf2 and i am new to ajax and zf2. i used a bootstrap modal to open the form as a modal dialog. i have set an initial status value as -1. then if the data saved then it will become 0. and if not it will return 1. so if the form is not valid it will return the -1.

my code is working but i don't know how to show the validation errors in the modal.

this is my validationAction in controller.

    public function validateinviteAction() {

    $request = $this->getRequest ();
    $data = $_POST ['textData'];

->  $dataform = array('email'=>$data); 
->  $form = new InviteForm ();
->  $validate = new Invite ();
->  $validate->exchangeArray ( $dataform);
->  $form->setInputFilter ( $validate->getInputFilter () );
->  $form->setData ( $dataform );
    $status=-1;
    if ($form->isValid ()) {
            $validate->exchangeArray ( $form->getData () );
            $row = $this->getUsersTable ()->getUserbyemail ( $validate->email );
            $reply = $this->getInviteTable ()->saveInvites ( $validate );
            $status=$reply;
        }

    $result = new JsonModel ( array (
            'status' => $status 
    ) );
    return $result;
}

This is my ajax code

$.ajax({
      type: "POST",
      url: "<?php echo $this->url('users', array('action'=>'validateinvite')); ?>",
  ->  data: emailtxt,
      success: function(resp){ 
          if(resp.status==0){
             // if saved successfully
              $("#messagesuccess").text(' The invitation sent successfully.');               
              document.getElementById("email").value = "";

          }
          else if(resp.status==1){
            //if cannot save successfully
              $("#messagefailed").text(' You have already sent an invitation to this person.');
          }
          else{
            //form is not valid // if status==-1
              $("#messagefailed").text(' The provided inputs are not valid.');
          }
      },
      error: function(resp){
          $("#messagefailed").text('Internet connection lost. Try again later.');
      }
});

Thanks in advance.

t j
  • 7,026
  • 12
  • 46
  • 66
dindeesha
  • 13
  • 4

1 Answers1

1

ok first off don't use $_POST for getting data $data = $_POST ['textData'];

$data = $this->params()->fromPost('textData');

you can get error messages from form using one of these:

$error = $form->getMessages();
//or
$error = $form->getInputFilter()->getMessages()

$result = new JsonModel ( array (
            'status' => $status ,
            'error' => $error
    ) );

and in your ajax :

  else{
    //form is not valid // if status==-1
      $("#messagefailed").text(' The provided inputs are not valid.');
      alert(resp.error.join("\n"));//or however u like
  }

UPDATE:

this line is extra and not required , the form does this itself

$validate->exchangeArray ( $dataform);

use this code to send forms data using ajax

data: $('#myForm').serialize()

first check if the request is post in your action

if($this->request->isPost())

then get post data and set to form

$form->setData ( $this->request->getPost());

and validate and if $form->isValid() then just simply get the data from form

$data = $form->getData ()
Exlord
  • 5,009
  • 4
  • 31
  • 51
  • Thank you very much for your reply.. And i have a another problem in get in to " if(!($form->isValid())){$status==-1; } " what am trying to do is get the form values to $data and validate them through my validation class. am i following the correct method sir? – dindeesha Jul 13 '14 at 05:51
  • i have doubts in arrow marked places in my coding. i got the emailtxt by var emailtext = $("#email").val(); var emailtxt = {textData:emailtext}; i dont know how to pass form data to the controller and then validate through the validation class – dindeesha Jul 13 '14 at 06:04