Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:58:14+00:00 2026-05-28T01:58:14+00:00

I am building a web application that involves forms. The framework being used is

  • 0

I am building a web application that involves forms. The framework being used is symfony2. I would like to have everything working for users without javascript, and then progressively enhance the experience for people with javascript enabled. I am planning to use JQuery as my javascript library.

I would like to have forms where it submits and displays a message using the FlashMessage component if the user does not have javascript and the request is not an ajax request.

For users with javascript support, I would like to return the message to them if submission is successful. The javascript should then display that message in a div immediately.

At the same time, I need to deal with error messages. For users without javascript, the whole form will be rerendered with the error messages in place.

For users with javascript, the ajax response should be an array containing the id of the input component and the error message. Javascript should then insert these into the form without a refresh.

Looking at the symfony2 book, I can see the following for submissions, I then modified it to check if the request is an ajax request:

public function newAction(Request $request)
{
    // just setup a fresh $task object (remove the dummy data)
    $task = new Task();

    $form = $this->createFormBuilder($task)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // perform some action, such as saving the task to the database
            if ($this->request->isXmlHttpRequest(){
                   //return data ajax requires.
            } 
            return $this->redirect($this->generateUrl('task_success'));
        }
    }

    // ...
}

The problem I find with this approach is that is isn’t very elegant. I need add that extra check every single form. Further more, this method would not work if say the request comes in via Zend_AMF.

Are there any better ways to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:58:15+00:00Added an answer on May 28, 2026 at 1:58 am

    Another option is to have your controller return something other than a response and use the kernel.view event to transform that into what you want. This would be better DRY because the logic is encapsulated in the listener. You just need to decide what you want your controllers to return.

    if ($form->isValid()) {
        return new Something($redirectUrl, $ajaxData);
    }
    

    And in your listener:

    public function onKernelView($event)
    {
        $request = $event->getRequest();
        $result = $event->getControllerResult();
    
        if ($result instanceof Something) {
            if ($request->isXmlHttpRequest()) {
                $event->setResponse(new Response(json_encode($result->getAjaxData())));
            } else {
                $event->setResponse(new RedirectResponse($result->getRedirectUrl()));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a web application that will have a fair bit of forms.
I am building a web application that have several requirements for the client: 1.
I'm building a web application that uses LAMP and the Smarty template framework. The
I have a small web application that I am building. Primarily to improve my
We're building a blog-like Pylons web application that uses CKeditor and allows to upload
I'm building a web application that will have access to PeopleSoft's database via jdbc.
I'm building an web application that I want users to have specific permissions to
I'm building a web application that involves very frequent database updates (10s-100s per second
I'm building a web application that will automatically complete user forms online for many
I'm building a web application that guides my users through the configuration and installation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.