Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Help returning JSON
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dave  
View profile  
 More options Nov 6, 11:35 am
From: "Dave" <make.cake.b...@gmail.com>
Date: Fri, 6 Nov 2009 13:05:46 -0330
Local: Fri, Nov 6 2009 11:35 am
Subject: Help returning JSON
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]    mytestem...@mytestsite.com

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kyo  
View profile  
 More options Nov 6, 9:12 pm
From: Kyo <ready2jam2n...@gmail.com>
Date: Fri, 6 Nov 2009 18:12:37 -0800 (PST)
Local: Fri, Nov 6 2009 9:12 pm
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...

hth,

Kyo


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave  
View profile  
 More options Nov 7, 11:06 am
From: "Dave" <make.cake.b...@gmail.com>
Date: Sat, 7 Nov 2009 12:36:52 -0330
Local: Sat, Nov 7 2009 11:06 am
Subject: RE: Help returning JSON
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] = myt...@dummy.com 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Miles J  
View profile  
 More options Nov 7, 3:08 pm
From: Miles J <mileswjohn...@gmail.com>
Date: Sat, 7 Nov 2009 12:08:35 -0800 (PST)
Local: Sat, Nov 7 2009 3:08 pm
Subject: Re: Help returning JSON
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" <make.cake.b...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google