Help returning JSON

4 messages Options
Embed this post
Permalink
Dave-33

Help returning JSON

Reply Threaded More More options
Print post
Permalink

I have setup a validation rule using jquery to check if a field is valid by
an ajax request.
 
The controler checks and returns valid (true or false). I watch the activity
using firebug and the request gets sent fine, and i see whats sent and comes
back in the response. But it is not returning correct.
I test with my personal valid email address or a real email address I have
in the db should come back to check because the one in the db should return
false because it exists, and the one that's not in the db should come back
validbut its always false.



POST TAB:
data[Profile][email] [hidden email]

RESPONSE TAB:
{"valid":false}

JSON TAB:
false
 

CONTROLLER FUNCTION:
function manage_validate() {
   $this->autoRender = false;
          Configure::write('debug', 0);
          if ($this->RequestHandler->isAjax()) {
   //debug($this->data);
   $this->Profile->set($this->data);
              if ($this->Profile->validates()) {
     
      header("Content-type: text/plain");
       $valid = true;
       $array = array('valid' => $valid);
       echo json_encode($array);
 
              } else {
       header("Content-type: text/plain");
       $valid = false;
       $array = array('valid' => $valid);
       echo json_encode($array);
                  //$errors = $this->Profile->invalidFields();
                      //debug($errors);
                 
              }
   
          }
      }


Ideas where I am going wrong?

Thanks
Dave


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Kyo-2

Re: Help returning JSON

Reply Threaded More More options
Print post
Permalink

You don't have to use header() and json_encode() to get JSON values
back with Cake.
Use $this->layout = 'ajax' in your controller and $javascript->object
() in your view.
Try out my way: http://jamnite.blogspot.com/2009/05/cakephp-form-validation-with-ajax-using.html

hth,

Kyo
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Dave-33

RE: Help returning JSON

Reply Threaded More More options
Print post
Permalink

Thanks for your response.

I did take a look at your tutorial. But in my situation  I don't think a lot
of what you have there will apply to me. I have everything to a point
working. Right now I am only trying to validate one field using validate:
remote option to check if a user email is already registered.

The data[User][email] = [hidden email] gets sent to Cake . If I debug
error I see

Array
(
    [email] => This email account is already registered.
)

I just need a way to get the error or success back to the js remote part of
the script...thats where I am lost.

I am not sure how to send back the message if there is an error or if valid
do nothing.

I tried using your suggestion with layout ajax but all I ended up getting
was an error messages showing {valid : false} where "This email account is
already registered." Should go.

My js validate rule looks like:

var validate_profile = {
       
        rules: {
                        'data[Profile][email]':{required: true, email:true,
remote: {url: "/manage/profiles/validate",type: "post"}},
        },
        messages: {
               
                'data[Profile][email]': {
                        required: '* JS required email address.',
                        email: '* JS email address.',
                        remote: '* this message is remote from JS.'}
        }};

And my controller:

function manage_validate() {
          Configure::write('debug', 2);
          if ($this->RequestHandler->isAjax()) {
                        $this->Profile->set($this->data);
              if ($this->Profile->validates()) {
                           //nothing to do

              } else {
                            //header("Content-type: text/plain");
    $errors = $this->Profile->invalidFields();
                        //debug($errors);

    $array = array("valid" => false);
                       
                       
    return json_encode($array);
                       
                        //$this->set('output' , $output);
    //echo json_encode($array);
               //$this->layout = 'ajax';  
               //$this->render ('/elements/errors/ajax_fields');
              }
                       
          }
      }

Ajax_fields.ctp :

<?php echo $javascript->object($output);?>


Any ideas?

Thanks again

Dave
-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf
Of Kyo
Sent: November-06-09 10:43 PM
To: CakePHP
Subject: Re: Help returning JSON


You don't have to use header() and json_encode() to get JSON values back
with Cake.
Use $this->layout = 'ajax' in your controller and $javascript->object
() in your view.
Try out my way:
http://jamnite.blogspot.com/2009/05/cakephp-form-validation-with-ajax-using.
html

hth,

Kyo


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Miles J

Re: Help returning JSON

Reply Threaded More More options
Print post
Permalink

Because your sending the wrong header, if your returning JSON it
should be the JSON header...

header("Content-type: application/json");

On Nov 7, 8:06 am, "Dave" <[hidden email]> wrote:

> Thanks for your response.
>
> I did take a look at your tutorial. But in my situation  I don't think a lot
> of what you have there will apply to me. I have everything to a point
> working. Right now I am only trying to validate one field using validate:
> remote option to check if a user email is already registered.
>
> The data[User][email] = [hidden email] gets sent to Cake . If I debug
> error I see
>
> Array
> (
>     [email] => This email account is already registered.
> )
>
> I just need a way to get the error or success back to the js remote part of
> the script...thats where I am lost.
>
> I am not sure how to send back the message if there is an error or if valid
> do nothing.
>
> I tried using your suggestion with layout ajax but all I ended up getting
> was an error messages showing {valid : false} where "This email account is
> already registered." Should go.
>
> My js validate rule looks like:
>
> var validate_profile = {
>
>         rules: {
>                         'data[Profile][email]':{required: true, email:true,
> remote: {url: "/manage/profiles/validate",type: "post"}},
>         },
>         messages: {
>
>                 'data[Profile][email]': {
>                         required: '* JS required email address.',
>                         email: '* JS email address.',
>                         remote: '* this message is remote from JS.'}
>         }};
>
> And my controller:
>
> function manage_validate() {
>           Configure::write('debug', 2);
>           if ($this->RequestHandler->isAjax()) {
>                         $this->Profile->set($this->data);
>               if ($this->Profile->validates()) {
>                            //nothing to do
>
>               } else {
>                             //header("Content-type: text/plain");
>                         $errors = $this->Profile->invalidFields();
>                         //debug($errors);
>
>                         $array = array("valid" => false);
>
>                         return json_encode($array);
>
>                         //$this->set('output' , $output);
>                         //echo json_encode($array);
>                //$this->layout = 'ajax';  
>                //$this->render ('/elements/errors/ajax_fields');
>               }
>
>           }
>       }
>
> Ajax_fields.ctp :
>
> <?php echo $javascript->object($output);?>
>
> Any ideas?
>
> Thanks again
>
> Dave
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Kyo
> Sent: November-06-09 10:43 PM
> To: CakePHP
> Subject: Re: Help returning JSON
>
> You don't have to use header() and json_encode() to get JSON values back
> with Cake.
> Use $this->layout = 'ajax' in your controller and $javascript->object
> () in your view.
> Try out my way:http://jamnite.blogspot.com/2009/05/cakephp-form-validation-with-ajax....
> html
>
> hth,
>
> Kyo
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---