Skip to content

属性校验

给属性添加校验,可以避免用户填写不支持的数据,减少开发不必要的错误处理逻辑

  1. 属性必填
    1. 通过标注RequiredAttitude支持属性必填
csharp
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace MyPlugin
{
    [Icon("pack://application:,,,/MyPlugin;component/Resources/Icon.png")]
    [Designer("MyPlugin.Designer.MyPluginServerCommandDesigner, MyPlugin")]
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [Required]
        public string Name { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            return new ExecuteResult();
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}
2. 效果  

1670081182002-05499efc-c2f4-44e2-9e6a-7dccfcddd5c0.png 2. 校验长度 1. 通过MaxLengthAttribute和MinLengthAttribute支持长度校验

csharp
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace MyPlugin
{
    [Icon("pack://application:,,,/MyPlugin;component/Resources/Icon.png")]
    [Designer("MyPlugin.Designer.MyPluginServerCommandDesigner, MyPlugin")]
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [MaxLength(6)]
        [MinLength(1)]
        public string Name { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            return new ExecuteResult();
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}
3. 效果  

1670081402839-d376c97b-52fb-4504-b9c4-95ee92172f05.png

更新: 2022-12-04 20:26:59
原文: https://www.yuque.com/robert-bh51n/ea8l6c/kz2drs84y1ulvou9