RegularExpressionValidator fails if full string is not matched [SPRNET-1122]
This commit is contained in:
@@ -426,16 +426,14 @@
|
||||
|
||||
<para>The syntax is</para>
|
||||
|
||||
<programlisting language="myxml"><v:regex id="id" test="valueToEvaluate" when="applicabilityCondition" parent="parentValidator">
|
||||
<v:property name="Expression" value="regularExpressionToMatch"/>
|
||||
<programlisting language="myxml"><v:regex id="id" test="valueToEvaluate" expression="regularExpressionToMatch" when="applicabilityCondition" parent="parentValidator">
|
||||
<v:property name="Options" value="regexOptions"/>
|
||||
actions
|
||||
</v:regex></programlisting>
|
||||
|
||||
<para>An example is shown below</para>
|
||||
|
||||
<programlisting language="myxml"><v:regex test="ReturningFrom.AirportCode">
|
||||
<v:property name="Expression" value="[A-Z][A-Z][A-Z]"/>
|
||||
<programlisting language="myxml"><v:regex test="ReturningFrom.AirportCode" expression="[A-Z][A-Z][A-Z]">
|
||||
<v:message id="error.destinationAirport.threeCharacters" providers="destinationAirportErrors, validationSummary"/>
|
||||
</v:regex></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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user