我正在使用FOSUserBundle,但是我已经覆盖了默认控制器来创建我自己的逻辑和表单。
这是我的ProfileFormType.php
namespace UserBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
class ProfileFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('avatar', 'file', array('required' => false, 'data_class' => null));
$builder->add('phone', 'text', array('required' => false));
$builder->add('gender', 'choice', array(
'choices' => array('not specifed' => 'Не указан', 'male' => 'Мужской', 'female' => 'Женский'),
));
$builder->add('birthDate', 'date', array('years' => range(date('Y') -100, date('Y'))));
$builder->add('locationCountry', 'text', array('required' => false));
$builder->add('locationCity', 'text', array('required' => false));
}
public function getParent()
{
return 'fos_user_profile';
}
public function getName()
{
return 'app_user_profile';
}
}
此时,没有文件扩展名验证。我应该如何验证扩展?什么是最佳实践?
I have try next
$builder->add('avatar', 'file', array('required' => false, 'data_class' => null, 'mimeType' => 'image/jpeg'));
但是它返回一个错误。顺便说一下,我想只接受。png和。jpeg文件。
谢谢
正如我之前的评论所说:
/**
* @Assert'File(
* mimeTypes = {"image/jpeg", "image/png"},
* mimeTypesMessage = "Only jpeg or png are allowed."
* )
*/
protected $avatar;