Create combined client side and server side validation in Symfony2

I think it would be very useful to create client side form validation up on the symfony2 Form and Validator components.

The best way to do this would be to pass the validation constraints to the form view. With that information it would be possible to make a template that renders a form field to something like this:

<div>
    <label for="form_email">E-Mail</label>
    <input 
        id="form_email" type="text" name="form[email]" value=""
        data-validation-constraints='{"NotBlank":{},"MinLength":{"limit":6}}'
    />
</div>

The JavaScript part then would be to find all <input> elements that have the data-validation-constraints attribute and create the correct validation for them.

To pass the validation constraints to the form view i thought the best way would be to create a form type extension. That's the point of my Question: Is this the correct way? And how is this possible?

At the Moment my form type extension looks like this:

use SymfonyComponentFormFormInterface;
use SymfonyComponentFormFormView;
use SymfonyComponentFormFormBuilder;

class FieldTypeExtension extends SymfonyComponentFormAbstractTypeExtension{

    public function getExtendedType(){
        return 'field';
    }

    public function buildView(FormView $view, FormInterface $form)
    {
        // at this point i didn't find a way to get the 
        // validation constraints out of the $form
        // the `getAllValidationConstraints` here is just an example
        $view->set('validation_constraints', $form->getAllValidationConstraints());
    }

}

How can i get all validation constraints applied to one form field out of the FormInterface object?


检查相应的公开问题“[Form] JavaScript验证”,其中包含对SimpleThingsFormExtraBundle的引用(或者是该包的特定开放PR)。


这是新的Symfony 2包,它将表单类型约束转换为JavaScript验证标尺https://github.com/formapro/JsFormValidatorBundle


You can do something simplier:

The FieldType already passes a attr attribute to the form which is directly passed as attr var to view. You had better to modify this attr form's attribute in order to add your data-validation-constraints attribute because it will avoid you to be required to also customize the form theme to handle your new var.

<?php

namespace MyBundleFormType;

use SymfonyComponentFormAbstractTypeExtension;
use SymfonyComponentFormFormBuilder;

class FieldTypeJsValidationExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $attr = $builder->getAttribute('attr');
        $attr = array_merge(
            array(
                'data-validation-constraints' => $this->aMethodThatRenderTheFinalContentOfTheValidationAttribute(),
            ),
            $builder->getAttribute('attr')
        );

        $builder->setAttribute('attr', $attr);
    }

    public function getExtendedType()
    {
        return 'field';
    }

}
链接地址: http://www.djcxy.com/p/9924.html

上一篇: Rails 3.1.1测试:Ruby 1.9.3上的基准测试

下一篇: 在Symfony2中创建组合的客户端和服务器端验证