추가 할 양식 클래스를 사용하려고합니다. & 편집. 추가 모드에서는 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 유효성 검사는 $this->mode
-$mode
설정되지만, 다음 getDefaultOptions()
에서 $mode
에 액세스하려고, 당신의 ItemForm
생성자에서