RegularExpressionValidator fails if full string is not matched [SPRNET-1122]

This commit is contained in:
bbaia
2010-09-29 08:46:15 +00:00
parent 6ee2ae5ee2
commit a4ff2a29c9
3 changed files with 38 additions and 15 deletions

View File

@@ -426,16 +426,14 @@
<para>The syntax is</para>
<programlisting language="myxml">&lt;v:regex id="id" test="valueToEvaluate" when="applicabilityCondition" parent="parentValidator"&gt;
&lt;v:property name="Expression" value="regularExpressionToMatch"/&gt;
<programlisting language="myxml">&lt;v:regex id="id" test="valueToEvaluate" expression="regularExpressionToMatch" when="applicabilityCondition" parent="parentValidator"&gt;
&lt;v:property name="Options" value="regexOptions"/&gt;
actions
&lt;/v:regex&gt;</programlisting>
<para>An example is shown below</para>
<programlisting language="myxml">&lt;v:regex test="ReturningFrom.AirportCode"&gt;
&lt;v:property name="Expression" value="[A-Z][A-Z][A-Z]"/&gt;
<programlisting language="myxml">&lt;v:regex test="ReturningFrom.AirportCode" expression="[A-Z][A-Z][A-Z]"&gt;
&lt;v:message id="error.destinationAirport.threeCharacters" providers="destinationAirportErrors, validationSummary"/&gt;
&lt;/v:regex&gt;</programlisting>
@@ -443,10 +441,18 @@
that need to conform to some predefined format, such as telephone
numbers, email addresses, URLs, etc.</para>
<para>One major difference of the regular expression validator compared
to other built-in validator types is that you need to set a required
<literal>Expression</literal> property to a regular expression to match
against.</para>
<note>
<para>
Note that current behavior limits the Regular Expression Validator to expressions
to being full matches, i.e., ^(expression)$, thus limiting functionality.
To not change this behavior in a point release, a property <literal>AllowPartialMatching</literal>
has been added in 1.3.1 to support the correct behavior.
</para>
<para>
This property will be removed for next major/minor version 2.0 and implementation
will be fixed to get the intented behavior.
</para>
</note>
</section>
<section>

View File

@@ -45,6 +45,7 @@ namespace Spring.Validation
#region Fields
private string expression = string.Empty;
private bool allowPartialMatching = false;
private RegexOptions options;
#endregion
@@ -98,6 +99,16 @@ namespace Spring.Validation
set { expression = value; }
}
/// <summary>
/// Gets or sets a value indicating whether to do a partial match instead of a full match.
/// Default is false.
/// </summary>
public bool AllowPartialMatching
{
get { return allowPartialMatching; }
set { allowPartialMatching = value; }
}
/// <summary>
/// The <see cref="RegexOptions"/> for the regular expression evaluation.
/// </summary>
@@ -143,7 +154,14 @@ namespace Spring.Validation
}
Match match = Regex.Match(text, this.Expression, this.Options);
return match.Success;
if (allowPartialMatching)
{
return match.Success;
}
else
{
return match.Success && match.Index == 0 && match.Length == text.Length;
}
}
}
}

View File

@@ -79,7 +79,7 @@ namespace Spring.Validation.Validators
[Test]
public void CaseSensitiveStringMatching()
{
RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"^[A-Z]([a-z]{1})");
RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"[A-Z][a-z]*");
Assert.IsTrue(validator.Validate("Aleksandar", new ValidationErrors()));
Assert.IsFalse(validator.Validate("ALEKSANDAR", new ValidationErrors()));
Assert.IsFalse(validator.Validate("aleksandar", new ValidationErrors()));
@@ -119,13 +119,12 @@ namespace Spring.Validation.Validators
}
[Test]
public void Test()
public void AllowPartialMatching()
{
RegularExpressionValidator validator = new RegularExpressionValidator();
validator.Expression = "^[A-Za-z]";
Assert.True(validator.Validate("hello", new ValidationErrors()));
validator.Expression = "[A-Za-z]";
validator.AllowPartialMatching = true;
Assert.True(validator.Validate("123a456", new ValidationErrors()));
}
}
}