我正在尝试使用表单类添加&编辑。在添加模式下,需要iconFile
。在编辑模式下,iconFile
是可选的(以替换当前图标)。我该如何实现这个目标?Symfony 2表单验证组
我试图在构造
class ItemForm extends AbstractType {
public function __construct($mode) {
$this->mode = $mode;
}
public function getDefaultOptions(array $opts) {
if ($mode == 'add') {
return array('validation_groups' => array('Default', 'add'));
} else {
return array('validation_groups' => array('Default'));
}
}
}
// doctrine entity, data_class of form
class Item {
/**
* @Assert\NotBlank(groups={"add"})
* @Assert\Image
*/
protected $iconFile;
}
// creating the form in controller
$form = $this->createForm(new ItemForm($mode));
问题是,即使在编辑模式设置的模式,我还需要选择一个图像。 HTML5验证触发