My SF2 application provides traditional pages using forms for
creating/updating entities.  I'm also using the Neton\DirectBundle to
provide support for a rich Internet application build using Sencha's
ExtJS Javascript framework.  DirectBundle allows me to publish
server-side controller actions that can be called in Javascript from
the client using the Ext Direct protocol, essentially XHR/JSON RPCs.

I've built an SF2 controller with CRUD actions for one of my entities
and have configured DIrectBundle to expose it.  I'm looking into
coding my create and update actions to reuse the validation
constraints defined in my entity annotations and already used by the
traditional pages and forms.

My initial attempt is below.  I have at least 2 show-stopper issues
with this; I'm missing the CSRF token needed by the Form and I don't
have field names to go with the validation error messages.  I'm
wondering if anyone else is trying to accomplish something similar.  I
really don't want to have to repeat the validation constraints in my
ExtDirect controllers.

Thanks in advance for any suggestions,

Paul

/**
 * ExtDirect method for updating an entity
 *
 * The $data parameter looks like so:
 *
 *     array(
 *         'id' => 123,
 *         'name' => 'Susan',
 *         'note' => 'Nice tatas!'
 *     )
 *
 * The returned array must contain a boolean 'success' entry.
 * In the event of a validation error, the 'error' entry in the array
 * should be an array of 'fieldname'=>'error message' pairs like so:
 *
 *     array (
 *         'success' => false,
 *         'errors' = array(
 *             'name' => 'Name must be longer than 2 characters',
 *             'note' => 'Note cannot be empty.'
 *         )
 *     )
 *
 * @see Neton\DirectBundle
 * @remote
 * @param array $data
 * @return array
 */
public function updateAction($data)
{
    $manager = $this->container->get('my.manager');
    $entity = $manager->findById($data['id']);
    $form = $this->get('form.factory')
            ->create(new MyType(), $entity);
    $form->bind(array(
        // TODO: missing '_token'
        'name' => $data['name'],
        'note' => $data['note']
    ));
    if (!$form->isValid()) {
        $result = array('success' => false, 'errors' = array());
        foreach ($form->getErrors() as $error) {
            // TODO use Translator::trans(template, params, 'validators')
            // TODO missing fieldname keys
            $result['errors'][] = strtr($error->getMessageTemplate(),
                    $error->getMessageParameters());
        }
    }
    $manager->update($entity);
    return array('success' => true);
}

-- 
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

Reply via email to