我的表单使用uploads
集合类型。集合的每个元素是UploadType
:
class MultiUploadType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('uploads', 'collection', array(
'type' => new UploadType(), // This should be validated
'allow_add' => true,
));
$builder->add('Save', 'submit');
}
}
使用 javascript 我可以添加新的上传,但验证不起作用。我在这里(这里、这里或这里)阅读了很多问题,但我还找不到解决方案。
这是上传类型的样子,而验证是使用 YAML 定义的,因为表单具有相应的类型实体Upload
(文件不能为空):
class UploadType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file', 'file');
$builder->add('description', 'textarea');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'required' => false,
'data_class' => 'App\Entity\Upload'
));
}
}
验证码:
App\Entity\Upload:
properties:
file:
- NotBlank:
message: Occorre selezionare un file.
- File: ~