【发布时间】:2018-05-05 14:34:01
【问题描述】:
我的 UserType 表单上有这个 EntityType 字段:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('country', EntityType::class, array(
'class' => Country::class,
'choice_label' => 'nicename'
));
}
如何使用验证约束来验证此类字段,因为用户只能在国家/地区表的所有行范围内选择一个值?我认为我应该使用带有回调的选择约束,并在我的 CountryRepository 类中调用 getAllCountries 函数。那么管理这种情况的最佳方法是什么?
类似这样的:
// UserEntity.php
class User {
/**
* @Assert\Choice(callback="App\Repository\CountryRepository", "getAllCountries")
* @ORM\ManyToOne(targetEntity="App\Entity\Country", inversedBy="users")
*/
protected $country;
}
但 CountryRepository 不是静态函数!!
【问题讨论】:
标签: php forms symfony validation orm