Symfony 1.1 Ajax Form Validation

Since Symfony 1.1 was released, I am working with it. I adept a existing Symfony 1.0 application to 1.1. This process is quite tricky and needs a lot of research to get the result that I want.

As I faced a problem with a form which I submit via an Ajax request, I found a pretty nice solution in doing this with Symfony 1.1.

public function executeSubmitaction($request) {
    $form   = new FoobarForm();

    // set the form to bound status with the submitted values
    $form->bind($request->getParameter('formvalues'));

    // validate all values
    if (!$form->isValid()) {
        $errors = array();
        foreach ($form->getErrorSchema() as $field => $error) {
            $errors[$field] = $error->getMessage();
        }
        $this->errors = $errors;
        return sfView::ERROR;
    }

    // do stuff if the form is valid

    return sfView::SUCCESS;
}

On the view-side I then have a javascript function which handles all the errors and I am using the serialized errors-array in the error-template to display the errors to the propriate divs.