当 Sonata Admin 出现问题时会收到更详细的消息

Get more verbose message when something fails in Sonata Admin

我很好奇 Sonata Admin 处理和显示错误消息的方式,因为我需要它们更具描述性。例如,当消息来自 DB 因为约束失败时,我收到这样的消息:

An error has occurred during update of item "Media Title Test".

但是如果我将必填字段留空,错误是相同的。我想知道是否有可能以某种方式处理这个问题以便更具描述性。有什么建议或帮助吗?

您可以在您的实体中使用约束来传递验证规则和消息。

// src/AppBundle/Entity/Author.php

// ...
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
    /**
     * @Assert\Choice(
     *     choices = { "male", "female", "other" },
     *     message = "Choose a valid gender."
     * )
     */
    public $gender;

    // ...
}

Validation Constraints Reference 将为您提供 Symfony 默认情况下可用的完整约束列表。