简体   繁体   English

用于AJAX提交表单的Symfony2控制器中的访问错误

[英]Access errors in Symfony2 controller for AJAX submitted form

Problem 1 问题1

I'd like to build a registration form via ajax submission. 我想通过ajax提交建立一个注册表。 Registration works is the $form->isValid() . 注册工作是$form->isValid() However, if the form fails registration I need to return these errors via ajax. 但是,如果表单注册失败,我需要通过ajax返回这些错误。

if ($form->isValid()) {

}else{
    $errors = $form->getErrors();
    // return some json encoded errors here
}

$form->getErrors() returns an empty array even though the form did not validate (in this case I am testing with a username that is too short). $form->getErrors()返回一个空数组,即使表单没有验证(在这种情况下,我正在使用太短的用户名进行测试)。

Problem 2 问题2

The second problem I have is that if the form validates but there is still an error. 我遇到的第二个问题是,如果表单验证但仍有错误。 For example a unique field that someone tries to submit the same value for. 例如,某人尝试为其提交相同值的唯一字段。

if ($form->isValid()) {

    $em = $this->getDoctrine()->getEntityManager();
    $em->persist($form->getData());
    $em->flush();

    // error could be a username submitted more than once, username is unique field

}else{
    // ...
}

How can I catch that error and return it via json? 如何捕获该错误并通过json返回?

Problem 1 问题1

In your form builder you can use error_bubbling to move the errors into your form object. 在表单构建器中,您可以使用error_bubbling将错误移动到表单对象中。 When you specify the field, pass it in as an option like this: 指定字段时,将其作为选项传递,如下所示:

$builder->add('username','text', array('error_bubbling'=>true));

and you can access errors in your form object like this: 并且您可以像这样访问表单对象中的错误:

$form->getErrors();

Outputs something like 输出类似的东西

array (
  0 => 
  Symfony\Component\Form\FormError::__set_state(array(
     'messageTemplate' => 'Your username must have at least {{ limit }} characters.',
     'messageParameters' => 
    array (
      '{{ value }}' => '1',
      '{{ limit }}' => 2,
    ),
  )),
) [] []

fyi: If you are using Form/Type's you can't set error_bubbling as a default value, it has to be assigned to each field. fyi:如果您使用的是Form / Type,则无法将error_bubbling设置为默认值,必须将其分配给每个字段。

Useful link: http://symfony.com/doc/2.0/reference/forms/types/text.html#error-bubbling 有用的链接: http//symfony.com/doc/2.0/reference/forms/types/text.html#error-bubbling

Problem 2 问题2

http://symfony.com/doc/2.0/reference/constraints/UniqueEntity.html http://symfony.com/doc/2.0/reference/constraints/UniqueEntity.html

Problem 1 问题1

The errors aren't on the form itself. 错误不在表单本身上。 Form::getErrors would only return errors if there were any on the form object itself. 如果表单对象本身有任何形式,则Form::getErrors只会返回错误。 You need to traverse the form and check for errors on each child. 您需要遍历表单并检查每个孩子的错误。

Form::isValid on the contrary just traverses the children and check if any of them are invalid. 相反, Form::isValid只是遍历子项并检查它们中的任何一个是否无效。

Problem 2 问题2

If there still are "errors" after validation, that means that your validation isn't complete. 如果验证后仍然存在“错误”,则表示您的验证未完成。 If your application requires a non-standard constraint, you should just go ahead and write a custom constraint. 如果您的应用程序需要非标准约束,那么您应该继续编写自定义约束。 See the cookbook entry on writing custom validator constraints for more information. 有关更多信息,请参阅有关编写自定义验证器约束的cookbook条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM