gpt4 book ai didi

php - 向 Symfony 2 表单元素添加错误

转载 作者:IT老高 更新时间:2023-10-28 11:57:02 27 4
gpt4 key购买 nike

我在我的 Controller 中检查了一些验证。我想在失败时将错误添加到表单的特定元素。我的表格:

use Symfony\Component\Form\FormError;

// ...

$config = new Config();
$form = $this->createFormBuilder($config)
->add('googleMapKey', 'text', array('label' => 'Google Map key'))
->add('locationRadius', 'text', array('label' => 'Location radius (km)'))
->getForm();

// ...

$form->addError(new FormError('error message'));

addError() 方法将错误添加到表单,而不是元素。如何向 locationRadius 元素添加错误?

最佳答案

你可以的

$form->get('locationRadius')->addError(new FormError('error message'));

因为表单元素也是 FormInterface 类型。

关于php - 向 Symfony 2 表单元素添加错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12419551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com