Fixing an issue where setting a custom property on a handler will not propagate it...
[castle.git] / Components / General / Validator / Castle.Components.Validator / Validators / RegularExpressionValidator.cs
blob5f7f8a686ea0213df8e2186ff35db23f0cc14042
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Components.Validator
17 using System;
18 using System.Collections;
19 using System.Text.RegularExpressions;
21 /// <summary>
22 /// Validate a property using regular expression
23 /// </summary>
24 [Serializable]
25 public class RegularExpressionValidator : AbstractValidator
27 private readonly Regex regexRule;
28 private readonly string expression;
30 /// <summary>
31 /// Initializes a new instance of the <see cref="RegularExpressionValidator"/> class.
32 /// </summary>
33 /// <param name="expression">The expression.</param>
34 public RegularExpressionValidator(String expression) : this(expression, RegexOptions.Compiled)
38 /// <summary>
39 /// Initializes a new instance of the <see cref="RegularExpressionValidator"/> class.
40 /// </summary>
41 /// <param name="expression">The expression.</param>
42 /// <param name="options">The regular expression options.</param>
43 public RegularExpressionValidator(String expression, RegexOptions options)
45 this.expression = expression;
46 regexRule = new Regex(expression, options);
50 /// <summary>
51 /// Gets the regular expression object.
52 /// </summary>
53 /// <value>The regular expression object.</value>
54 public Regex RegexRule
56 get { return regexRule; }
59 /// <summary>
60 /// Gets the expression.
61 /// </summary>
62 /// <value>The expression.</value>
63 public string Expression
65 get { return expression; }
68 /// <summary>
69 /// Validate that the property value match the given regex. Null or empty values are allowed.
70 /// </summary>
71 /// <param name="instance"></param>
72 /// <param name="fieldValue"></param>
73 /// <returns><c>true</c> if the field is OK</returns>
74 public override bool IsValid(object instance, object fieldValue)
76 if (fieldValue != null && fieldValue.ToString() != "")
78 string value = fieldValue.ToString();
80 if (value.Length > 0)
82 return regexRule.IsMatch(value);
86 return true;
89 /// <summary>
90 /// Gets a value indicating whether this validator supports browser validation.
91 /// </summary>
92 /// <value>
93 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
94 /// </value>
95 public override bool SupportsBrowserValidation
97 get { return true; }
100 /// <summary>
101 /// Applies the browser validation by setting up one or
102 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
103 /// </summary>
104 /// <param name="config">The config.</param>
105 /// <param name="inputType">Type of the input.</param>
106 /// <param name="generator">The generator.</param>
107 /// <param name="attributes">The attributes.</param>
108 /// <param name="target">The target.</param>
109 public override void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType,
110 IBrowserValidationGenerator generator, IDictionary attributes,
111 string target)
113 base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
115 generator.SetRegExp(target, expression, BuildErrorMessage());