[英]How to show a different value for a field in a Symfony FormType
我有一个像这样的 Symfony FormType
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class MyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'label' => 'Name',
'required' => true,
'disabled' => false,
]);
}
}
编辑时,此文本类型将使用数据库中的值填充。 我想根据其他一些条件更改字段中的值,我尝试了这个但没有运气:
if($someValue !== null) {
$builder
->get('name')->setData($someValue);
}
如何在表单中显示不同的值?
最简单的方法是使用“数据”选项
$builder
->add('name', TextType::class, [
'label' => 'Name',
'required' => true,
'disabled' => false,
'data' => $value, // setting the value you want
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.