Oh, for sure.

If you want just to validate a field with ajax and a controller you probably
need to grep
the EmailField value and attach it to a variable.

If you instead want to submit the whole form with JQuery and Ajax you should
want to use the .serialize() function (of JQuery + Ajax)



So the controller must looks like:

public function validateEmailAction(){

# Is the request an ajax one?
if ($this->get('request')->isXmlHttpRequest())
{
    # Lets get the email parameter's value
    $email = $this->get('request')->request->get('email');
   #if the email is correct
   if(....){
       return new Response("<b>The email is valid</b>");
    }#endif
#else if the email is incorrect
    else
    {
        return new Response("<b>We are sorry, the email is already
taken</b>");
    }#endelse

}# endif this is an ajax request

} #end of the controller.

........

The controllers route could be something like

email_validation:
    pattern:  /emailcheck
    defaults: { _controller: YourBundle:SomeController:validateEmail }

.........
and in the main template (the one that displays the email field and the
whole form) you should have something like:

<input type='hidden' name='emailCheckRoute' id='emailCheckRoute' value="{{
path('email_validation') }}" />

This one is meant to avoid hardcoding the controller's route in the
Javascript File, (there are 2 routes, the one for prod and the one for dev,
so this
aproach will prevent you from guessing what route is the correct one
depending on the enviroment) you will only
have to read  the route for the ajax request from this field. In other
cases, you may want to read the action route of the form ( <form action="{{
path('some_route') }}" > )
or maybe the route of a link (<a href="{{ path('some_route') }}" /> ) This
can be done with JQuery.

...

the script.js should read something like:


$(document).ready(function() {

    $('input#idOfTheEmailField').blur(function(e){

       var $email = $(this).val(); //this is the same that saying
$('input#idOfTheEmailField').val()
       var $url = $('input#emailCheckRoute').val(); //we are getting the
email_validation route.

       $.post($url, { email: $email },
         function(data) {
           alert("The app says: " + data);
        });

    });

});


Remember to know which is the ID property of the emailField (you must change
mine "#idOfTheEmailField" with yours).

If you are new to JQuery, you must know that "$(document).ready(function() {
..... } );" is mandatory
and every function must go inside this main function.

Also, the include of the JQuery script must go first, and your custom script
goes after :

<script type="text/javascript" src="{{ asset('js/jquery.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/yourCustomScript.js')
}}"></script>

And of course, anything can goes wrong with a js file, so if you don't have
a debugger like bugzilla,
the easiest way to get going is printing a simple alert, just to see if
JQuery main function is working:

$(document).ready(function() {
  alert('this is working, lest add more code, and try little by little");
});

Hope this get you rolling [?]


2011/4/18 symfonyMan <ezziani.cha...@gmail.com>

> Hello Oscar,
>
> thanks you for the replays..
>
> can you show me an exemple of sending an ajax request with jquery ??
>
> i tried a lot of things (.ajax(), .get()...) but il my ajaxAction,
> this test is skipped : (not an XMLHttpRequest())
> if ($this->container->get('request')->isXmlHttpRequest())
>        {
>             //some code here
>        }
>
> i want to validate the email input (onblur)
>
> thanks ;)
>
>
> On 16 avr, 19:20, oscar balladares <liebegr...@gmail.com> wrote:
> > If you just want to return a simple string you probably want to:
> >
> > # Controllers definition {
> > #some logic here
> >
> > return new Response("</b>There are some errors on this field</b>");
> >
> > ...........
> >
> > You must import the namespace for Response :
> >
> > use Symfony\Component\HttpFoundation\Response;
> >
> > 2011/4/15 symfonyMan <ezziani.cha...@gmail.com>
> >
> > > Hello,
> >
> > > thanks for all this responses.
> >
> > > the controller must return a template ?
> >
> > > Me, I need ajax just for validate email, and print "ok" next to the
> > > email field...
> >
> > > sorry i have a bad english :)
> >
> > > thnaks
> >
> > > On 15 avr, 00:12, oscar balladares <liebegr...@gmail.com> wrote:
> > > > The controller should return a template response, a common template
> > > (without
> > > > inheritance of course)
> >
> > > > 2011/4/14 oscar balladares <liebegr...@gmail.com>
> >
> > > > > With JQuery you can request Marc's controller's route. That should
> do
> > > the
> > > > > trick.
> >
> > > > > 2011/4/14 Marc MacLeod <marbe...@gmail.com>
> >
> > > > >> In your controller:
> >
> > > > >>    public function fooAction()
> > > > >>    {
> > > > >>        // Is this an ajax request?
> > > > >>        if ($this->container->get('request')->isXmlHttpRequest())
> > > > >>        {
> > > > >>            // do stuff and return ajax content
> > > > >>        }
> >
> > > > >>        // return normal response
> > > > >>     }
> >
> > > > >> On Apr 13, 5:16 am, symfonyMan <ezziani.cha...@gmail.com> wrote:
> > > > >> > hello,
> >
> > > > >> > could somebody show me an example using ajax in symfony 2 ??
> >
> > > > >> > thanks
> >
> > > > >> --
> > > > >> If you want to report a vulnerability issue on symfony, please
> send it
> > > to
> > > > >> security at symfony-project.com
> >
> > > > >> You received this message because you are subscribed to the Google
> > > > >> Groups "symfony users" group.
> > > > >> To post to this group, send email to
> symfony-users@googlegroups.com
> > > > >> To unsubscribe from this group, send email to
> > > > >> symfony-users+unsubscr...@googlegroups.com
> > > > >> For more options, visit this group at
> > > > >>http://groups.google.com/group/symfony-users?hl=en
> >
> > > --
> > > If you want to report a vulnerability issue on symfony, please send it
> to
> > > security at symfony-project.com
> >
> > > You received this message because you are subscribed to the Google
> > > Groups "symfony users" group.
> > > To post to this group, send email to symfony-users@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > symfony-users+unsubscr...@googlegroups.com
> > > For more options, visit this group at
> > >http://groups.google.com/group/symfony-users?hl=en
>
> --
> If you want to report a vulnerability issue on symfony, please send it to
> security at symfony-project.com
>
> You received this message because you are subscribed to the Google
> Groups "symfony users" group.
> To post to this group, send email to symfony-users@googlegroups.com
> To unsubscribe from this group, send email to
> symfony-users+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/symfony-users?hl=en
>

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

<<360.gif>>

Reply via email to