Symfony 在管理生成器中嵌入图像表单

发布于 2024-12-08 04:18:07 字数 3473 浏览 0 评论 0原文

我使用 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);


  }

但通过这种方式,我的图像形式不会以任何方式设置。

I use symfony with doctrine. So I use this tutorial (google cache). So I have category and subcategory I make next :

 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);

}

Ok so this method works. But I want to make same for but not for subcategory, I want to make it for image form. So I have next:

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);


  }

But in this way my image form unset in any way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-12-15 04:18:07

只需制作:

 if ((''== $taintedFiles['image']['image']['name']) ) {

                  unset($this->embeddedForms['image'], $taintedValues['image'],$taintedFiles['image']);

                    // pass the new form validations
                   $this->validatorSchema['image'] = new sfValidatorPass();

                }

Just make :

 if ((''== $taintedFiles['image']['image']['name']) ) {

                  unset($this->embeddedForms['image'], $taintedValues['image'],$taintedFiles['image']);

                    // pass the new form validations
                   $this->validatorSchema['image'] = new sfValidatorPass();

                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文