在 Symfony2 中创建组合的客户端和服务器端验证
我认为在 symfony2 Form
和 Validator
组件上创建客户端表单验证非常有用。
执行此操作的最佳方法是将验证约束传递给表单视图。 有了这些信息,就可以制作一个模板,将表单字段呈现为如下所示:
<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>
然后,JavaScript 部分将查找所有具有 data- 的
属性并为它们创建正确的验证。 元素。 validation-constraints
要将验证约束传递给表单视图,我认为最好的方法是创建表单类型扩展。这就是我的问题的重点:这是正确的方法吗?这怎么可能?
目前,我的表单类型扩展如下所示:
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormBuilder;
class FieldTypeExtension extends \Symfony\Component\Form\AbstractTypeExtension{
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());
}
}
如何从 FormInterface 对象中获取应用于一个表单字段的所有验证约束?
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 Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormBuilder;
class FieldTypeExtension extends \Symfony\Component\Form\AbstractTypeExtension{
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查相应的开放问题 "[Form] JavaScript 验证",其中包含对 SimpleThingsFormExtraBundle 的引用 (或者更确切地说,该捆绑包的特定、开放 PR)可以做到这一点。
Check the corresponding open issue "[Form] JavaScript validation" which contains a reference to SimpleThingsFormExtraBundle (or rather a specific, open PR of that bundle) that does that.
这是新的 Symfony 2 包,它将表单类型约束转换为 JavaScript 验证标尺 https://github.com/formapro/JsFormValidatorBundle< /a>
This is new Symfony 2 bundle which converts form type constraints to JavaScript validation rulers https://github.com/formapro/JsFormValidatorBundle
您可以做一些更简单的事情:
FieldType 已经将 attr 属性传递给表单,该属性直接作为 attr var 传递给查看。您最好修改此 attr 表单的属性,以便添加您的 data-validation-constraints 属性,因为这样可以避免您需要自定义表单主题来处理你的新变量。
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.