tsungsoft / error-message
一个 Laravel Nova 字段。
0.0.3
2019-10-31 05:13 UTC
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2024-08-29 05:00:55 UTC
README
当抛出异常时将显示我们的实际消息,但仅在本地环境且调试为 true 时有效。如果在生产环境中使用此功能且调试为 false,则始终显示“服务器错误”。
另一方面,使用 ValidationException,将显示“提交表单时出现问题。”并且无法将我们的消息显示给用户(据我所知)。
使用此功能将在表单中添加一个新字段,但仅在存在错误消息时显示。
安装
使用 Packagist
composer require tsungsoft/error-message
使用 GitHub 仓库,在 composer.json 中添加以下内容
"require": {
"tsungsoft/error-message": "*"
}
"repositories": [
{
"type": "vcs",
"url": "https://github.com/anditsung/laravel-nova-error-message"
}
],
用法
在 Nova 资源中
use Tsungsoft\ErrorMessage\ErrorMessage;
...
public function fields(Request $request)
{
return [
...
ErrorMessage::make('Error'), // 'Error' -> must have the same value on ValidationException
...
];
}
...
在模型中
...
private static function validationError($message)
{
$messageBag = new MessageBag;
$messageBag->add('error', __($message)); // 'error' -> must have the same value when creating the field on nova resource
throw ValidationException::withMessages($messageBag->getMessages());
}
protected static function boot()
{
parent::boot();
static::creating(function($user) {
...
self::validationError("You have an error");
...
});
static::updating(function($user) {
...
self::validationError("You have an error");
...
});
}
...