我正在使用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';
}
}
目前,没有文件扩展名验证。我该如何验证扩展?什么是最佳做法?
我试过下一次
$builder->add('avatar', 'file', array('required' => false, 'data_class' => null, 'mimeType' => 'image/jpeg'));
但是它返回了一个错误。顺便说一下,我只想接受.png和.jpeg文件。
谢谢
答案 0 :(得分:1)
正如我之前的评论所说:
/**
* @Assert\File(
* mimeTypes = {"image/jpeg", "image/png"},
* mimeTypesMessage = "Only jpeg or png are allowed."
* )
*/
protected $avatar;