【问题标题】:how to show custom error in sonata admin如何在奏鸣曲管理员中显示自定义错误
【发布时间】:2015-12-30 10:16:24
【问题描述】:

我有MenuBundle,我想在奏鸣曲管理员中显示我的自定义错误。

管理员:MenuAdmin.php

/**
 * {@inheritdoc}
 */
public function validate( ErrorElement $errorElement, $object ) {
    //
    if ( $object->getEnabled() == false && $object->getMenuType() == 'header' ) {
        $custom_error = 'Header menu cannot be disabled, please mark enabled to checked.';
        $errorElement->with( 'enabled' )->addViolation( $custom_error )->end();
    }
}

FormMapper in admin:

protected function configureFormFields( FormMapper $formMapper ) {
        $formMapper
            ->add( 'title' )
            ->add( 'menuType', 'choice', array(
                'choices'  => array(
                    'header'        => 'Header',
                    'footer_left'   => 'Footer Left',
                    'footer_right'  => 'Footer Right',
                    'footer_bottom' => 'Footer Bottom'
                ),
                'expanded' => true,
                'multiple' => false
            ) )
            ->add( 'enabled' );
    }

验证工作正常,但没有出现自定义错误。

【问题讨论】:

  • 已经检测到错误,这就是复选框设置为红色的原因,如果您将鼠标聚焦在已启用标签上,是否会显示您的错误?你能用探查器看到有没有其他错误?
  • 是的,鼠标悬停时会显示自定义错误,但我想设置错误来代替红色条。但分析器没有任何其他错误。

标签: php symfony sonata-admin


【解决方案1】:

解决方案 #1:ErrorElement

只需在现场使用 error_bubbling => true

解决方案#1的注意事项:不要忘记在管理员下面添加use验证器服务。

使用 Sonata\AdminBundle\Validator\ErrorElement;

解决方案#2:使用Sonata - FLASH MESSAGES

我已经使用Sonata - FLASH MESSAGES 完成了它

$formMapper->add( 'enabled', null, array(
                'error_bubbling' => true
            ) );

菜单管理

/**
     * {@inheritdoc}
     */
    public function validate( ErrorElement $errorElement, $object ) {
        //
        if ( $object->getEnabled() == false && $object->getMenuType() == 'header' ) {
            $error = 'Header menu cannot be disabled, please mark enabled to checked.';
            $errorElement->with( 'enabled' )->addViolation($error)->end();
            $this->getRequest()->getSession()->getFlashBag()->add( "menu_type_check", $error );
        }

    }

YML

路径:YourBundle\Resources\config\admin.yml

sonata_core:
    flashmessage:
        error:
            #css_class: error_msg # optionally, a CSS class can be defined
            types:
                - { type: menu_type_check, domain: YourBundle }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2016-04-17
    • 2018-04-26
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多