SonataAdminBundle嵌入表单验证错误 [英] SonataAdminBundle embebed form validation error
问题描述
我有一个管理类,其中包括另一个管理类.
I have an admin class, which included another admin class.
- Symfony 2.7.9
- Sonata Admin软件包2.3.7
我在选项卡中构建了结构,问题是当嵌入表单的任何字段中出现验证错误时,都不会以任何方式标记它所在的选项卡.
I have it structured in tabs, and the problem is that when there is a validation error in any of the fields of the embedded form is not marked in any way the tab where it is located.
这是我的示例代码: 实体 Products.php
This is my example code: Entity Products.php
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Products
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\ProductsRepository")
*/
class Products
{
//...
/**
*
* @ORM\OneToMany(targetEntity="Modules", mappedBy="products", cascade={"persist"}, orphanRemoval=true)
* @ORM\OrderBy({"position" = "ASC"})
*/
protected $module;
/**
* Constructor
*/
public function __construct()
{
$this->module= new ArrayCollection();
}
//...
/**
* Set module
*
* @param Doctrine\ORM\PersistentCollection $module
*
*/
public function setModule(\Doctrine\ORM\PersistentCollection $module) {
if (count($module) > 0) {
foreach ($module as $m) {
$this->addModule($m);
}
}
return $this;
}
/**
* Remove module
*
* @param \AppBundle\Entity\Modules $module
*/
public function removeModule(\AppBundle\Entity\Modules $module)
{
foreach ($this->module as $k => $s) {
if ($s->getId() == $module->getId()) {
unset($this->module[$k]);
}
}
}
/**
* Get module
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getModule()
{
return $this->module;
}
/**
* Add module
*
* @param \AppBundle\Entity\Modules $module
* @return Products
*/
public function addModule(\AppBundle\Entity\Modules $module)
{
$module->setProducts($this);
$this->module[] = $module;
}
//...
}
实体 Modules.php
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Modules
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\ModulesRepository")
*/
class Modules
{
//...
/**
* @ORM\ManyToOne(targetEntity="Products", inversedBy="module")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $products;
/**
* Constructor
*/
public function __construct() {
$this->products = new ArrayCollection();
}
/**
* Set products
*
* @param \AppBundle\Entity\Products $products
* @return Modules
*/
public function setProducts(\AppBundle\Entity\Products $products = null)
{
$this->products = $products;
return $this;
}
/**
* Get products
*
* @return \AppBundle\Entity\Products
*/
public function getProducts()
{
return $this->products;
}
//...
}
实体 admin.yml
services:
sonata.admin.product.modules:
class: AppBundle\Admin\ModulesAdmin
tags:
- {name: sonata.admin, manager_type: orm, label: Products}
arguments:
- ~
- AppBundle\Entity\Modules
- 'SonataAdminBundle:CRUD'
calls:
- [ setTranslationDomain, [ProductsAdmin]]
- [ setLabelTranslatorStrategy, ["@sonata.admin.label.strategy.underscore"]]
实体 ProductsAdmin.php
<?php
namespace AppBundle\Admin;
class ProductsAdmin extends Admin {
public $supportsPreviewMode = true;
protected $formOptions = array(
'cascade_validation' => true
);
//...
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->tab('General')
->end()
->tab('Modules')
->add('module', 'sonata_type_collection', array(
'type_options' => array(
'delete' => true
)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'admin_code' => 'sonata.admin.product.modules'
))
->end()
;
}
public function prePersist($products) {
$this->preUpdate($products);
}
public function preUpdate($products) {
$products->setModule($products->getModule());
}
//...
}
实体 ModulesAdmin.php
<?php
namespace AppBundle\Admin;
class ModulesAdmin extends Admin {
public $supportsPreviewMode = true;
protected $formOptions = array(
'cascade_validation' => true
);
protected $baseRouteName = 'admin_app_product_modules';
protected $baseRoutePattern = 'app/product-modules';
//...
}
我想知道如何指示嵌入表格所在的选项卡,其字段中存在验证错误.
I would like to know how to indicate the tab where the embedded form, there is a validation error in their fields is.
推荐答案
此问题可能已经解决,但我通过以下方式解决了此问题:
This has probably already been fixed but I fixed it this way:
在管理类中,我添加了:
In my Admin Class I added:
protected $formOptions = array(
'cascade_validation' => true
);
也在我的sonata_type_collection条目中添加:
Also in my sonata_type_collection entries I added:
'cascade_validation' => true
这会导致表单上的元素在存在非验证表单的情况下添加"has-error"类(在非嵌入式表单的情况下)或"error"(在嵌入式sonata_type_collection内联表单的情况下).
This causes the element on the form to add either the class 'has-error' in case of non-embedded forms, or 'error' in case of embedded sonata_type_collection inline forms, when there was a validation error.
为了突出显示正确的选项卡,我不得不使用jQuery.默认情况下,选项卡按钮本身始终包含以下内容:
In order to highlight the correct tab I had to use jQuery. By default the tab buttons themselves always contain the following:
<i class="fa fa-exclamation-circle has-errors hide"></i>
因此,只需一点点代码就可以轻松定位此目标并删除该隐藏"类.
So it was easy enough to target this with a little bit of code and remove that 'hide' class.
// Highlight tabs if there is a validation error on an element in them
$('.tab-pane').each( function( index, element ) {
var btn = $('a[href^="#'+$(this).attr('id')+'"]');
var jumpToTab = false;
$(this).find('td, div').each(function(i, e) {
if ($(this).hasClass('error') || $(this).hasClass('has-error')) {
if (jumpToTab == false) {
btn.tab('show');
jumpToTab = true;
}
btn.find('i').removeClass('hide');
btn.addClass('error');
}
});
});
我在btn中添加了一个错误"类,因为我也想更改链接颜色,仅此而已.现在,提交有错误的表单会突出显示所有其中包含错误元素的选项卡.
I added an 'error' class to the btn because I wanted to change the link colour as well, but that's it. Submitting the form with errors now highlights all tabs that have elements in them that have errored.
这篇关于SonataAdminBundle嵌入表单验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!