0
我使用symfony和doctrine。所以我用this教程(google cache)。所以我有我的下一個類別和子類別:Symfony在管理生成器中嵌入圖像形式
if (!$this->isNew()) {
// embed all subcategory forms
foreach ($this->getObject()->getSubcategory() as $subcategory) {
// create a new subcategory form for the current subcategory model object
$subcategory_form = new SubcategoryForm($subcategory);
// embed the subcategory form in the main category form
$this->embedForm('subcategory'.$subcategory->getId(), $subcategory_form);
// set a custom label for the embedded form
$this->widgetSchema['subcategory'.$subcategory->getId()]->setLabel('Subcategory:'. $subcategory->getName());
}
$subcategory_form = new SubcategoryForm();
// embed the subcategory form in the main category form
$this->embedForm('subcategory', $subcategory_form);
public function bind(array $taintedValues = null, array $taintedFiles = null) {
$this->languages = sfConfig::get('app_cultures_enabled');
$langs = array_keys($this->languages);
foreach($this->languages as $lang => $label)
{
// remove the embedded new form if the name field was not provided
if (is_null($taintedValues['subcategory'][$lang]['name']) || strlen($taintedValues ['subcategory'][$lang]['name']) === 0) {
unset($this->embeddedForms['subcategory'], $taintedValues['subcategory']);
// pass the new form validations
$this->validatorSchema['subcategory'] = new sfValidatorPass();
} else {
// set the category of the new subcategory form object
$this->embeddedForms['subcategory']->getObject()->
setCategory($this->getObject());
}
}
// call parent bind method
parent::bind($taintedValues, $taintedFiles);
}
好,所以這種方法的工作原理。但是我想爲了子類而不是做同樣的事,我想把它做成圖像形式。所以我有下一個:
if (!$this->isNew()) {
// Image
foreach ($this->getObject()->getImage() as $image) {
$image_form = new ImageForm($image);
$this->embedForm('image'.$image->getId(), $image_form);
}
$image_form = new ImageForm();
$this->embedForm('image', $image_form);
public function bind(array $taintedValues = null, array $taintedFiles = null) {
if (is_null($taintedValues['image']['image'])|| strlen($taintedValues['image']['image']) === 0) {
unset($this->embeddedForms['image'], $taintedValues['image']);
// pass the new form validations
$this->validatorSchema['image'] = new sfValidatorPass();
} else {
// set the category of the new subcategory form object
$this->embeddedForms['image']->getObject()->
setProduct($this->getObject());
}
parent::bind($taintedValues, $taintedFiles);
}
但這樣我的圖像形式以任何方式未設置。
我不知道我理解我的最後一句...你應該的var_dump爲了調試 – greg0ire
謝謝污染值和被感染的文件!我做print_r,並找到答案) – denys281