Twig 如何强制symfony表单仅显示和接受正整数?
我有以下代码:Twig 如何强制symfony表单仅显示和接受正整数?,twig,constraints,symfony4,symfony-forms,Twig,Constraints,Symfony4,Symfony Forms,我有以下代码: use Symfony\Component\Validator\Constraints\Positive; public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('x', IntegerType::class, [ 'mapped' => false, 'required
use Symfony\Component\Validator\Constraints\Positive;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('x', IntegerType::class, [
'mapped' => false,
'required' => false,
'constraints' => [new Positive()]
])
}
小枝形式如下:
{{ form_widget(form.x, { 'attr': {'class': 'form-control'} }) }}
但是,呈现表单(HTML)仍然允许用户输入带有减号的值。
如何更改该值,以使呈现的表单禁止减号,并在箭头输入上停止在1处?您必须为其添加,您可以在表单字段的定义处添加:
使用Symfony\Component\Validator\Constraints\Positive;
公共函数buildForm(FormBuilderInterface$builder、array$options)
{
$builder
->添加('x',IntegerType::class[
“映射”=>false,
“必需”=>false,
'约束'=>[new Positive()],
'attr'=>[
“min”=>1
]
])
}
您必须为此添加,您可以在表单字段的定义中添加:
使用Symfony\Component\Validator\Constraints\Positive;
公共函数buildForm(FormBuilderInterface$builder、array$options)
{
$builder
->添加('x',IntegerType::class[
“映射”=>false,
“必需”=>false,
'约束'=>[new Positive()],
'attr'=>[
“min”=>1
]
])
}