如何在奏鸣曲管理员中显示自定义错误 - php

我有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

参考方案

解决方案1:使用ErrorElement。
只需在字段上使用error_bubbling => true

解决方案#1的注意事项:不要忘记在admin中的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 }

验证IBAN PHP - php

在设计新平台时,我们尝试集成IBAN编号。我们必须确保IBAN已经过验证,并且存储在数据库中的IBAN始终正确。那么验证数字的正确方法是什么? 参考方案 正如我在其他问题中解释的逻辑一样,我尝试自己创建一个函数。根据Wikipedia文章中解释的逻辑,在下面找到合适的功能。国家特定验证。它适合吗http://en.wikipedia.org/wiki/Int…

PHP:对数组排序 - php

请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

PHP PDO组按列名称查询结果 - php

以下PDO查询返回以下结果:$db = new PDO('....'); $sth = $db->prepare('SELECT ...'); 结果如下: name curso ABC stack CDE stack FGH stack IJK stack LMN overflow OPQ overflow RS…