显示所有密码规则自定义数据注释

时间:2019-03-28 09:49:57

标签: c# asp.net-mvc asp.net-core-2.0

您好,我已经通过数据注释设置了自定义密码规则。可以,但是,如果不满足密码要求,最好将所有错误消息显示给用户。

我的代码遵循此-

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string password = value.ToString();

        if (password.Length < 8)
        {
            return new ValidationResult("Password must contain at least 8 characters");
        }
        if (password.Count(c => char.IsLower(c)) == 0)
        {
            return new ValidationResult("Password must contain a lowercase character");
        }
        //other password rules removed for berevity
        else
        {
            return ValidationResult.Success;
        }
    }

那么,如果密码不包含小写字符且少于8个字符,而不是仅仅返回第一个if语句,我将如何显示两个if语句?

1 个答案:

答案 0 :(得分:1)

您可以将所有错误消息保存在一个字符串中,并在检查所有规则后将其返回。

<?xml version="1.0" encoding="UTF-8"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.0.0 http://karaf.apache.org/xmlns/features/v1.0.0"
    name="special-server-features">

    <!-- Special Server -->
    <feature name="special-server" version="1.0.0" install="auto" resolver="(obr)">

        <details>
            A feature is just a group of bundles that should all be installed together.
            When an OSGi container adds a bundle, it goes through a resolution process
            to make sure that the bundle’s dependencies are met (and that it does not
            conflict with other installed bundles). However, that resolution process
            does not include any ability to obtain any dependencies; it just checks to
            see if they are available and delays or prevents the bundle from starting
            if a required dependency is missing.

            Requirements can tell the feature resolver to
            automatically install the bundles to satisfy the requirements.

            Dependencies vs. prerequisites:
        </details>



        <!-- Required feature repositories (containing all bundles) -->
        <repository>mvn:org.apache.camel.karaf/apache-camel/${camel.version}/xml/features</repository>
        <repository>mvn:org.apache.cxf.karaf/apache-cxf/${camel.version}/xml/features</repository>

        <bundle version="${camel.version}" prerequisite="true">camel-core</bundle>
        <bundle version="${camel.version}" prerequisite="true">cxf</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-blueprint</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-jackson</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-cxf</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-http</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-jaxb</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-jsch</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-log</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-stream</bundle>
    </feature>

</features>