【发布时间】:2011-12-22 07:14:24
【问题描述】:
我正在尝试使用表单类进行添加和编辑。在添加模式下,iconFile 是必需的。在编辑模式下,iconFile 是可选的(用于替换当前图标)。我怎样才能做到这一点?
我尝试在构造函数中设置模式
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 验证触发器
【问题讨论】:
标签: forms validation symfony