Fixing an issue where setting a custom property on a handler will not propagate it...
[castle.git] / Components / General / Validator / Castle.Components.Validator / Validators / NonEmptyValidator.cs
bloba44b835c2250021343689816d9cd7fd6803e2f02
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;
20 /// <summary>
21 /// Ensures that a property was
22 /// filled with some value
23 /// </summary>
24 [Serializable]
25 public class NonEmptyValidator : AbstractValidator
27 /// <summary>
28 /// Check that this property has a value that is not null or empty (if string)
29 /// </summary>
30 /// <param name="instance"></param>
31 /// <param name="fieldValue"></param>
32 /// <returns><c>true</c> if the field is OK</returns>
33 public override bool IsValid(object instance, object fieldValue)
35 return fieldValue != null && fieldValue.ToString().Length != 0;
38 /// <summary>
39 /// Gets a value indicating whether this validator supports browser validation.
40 /// </summary>
41 /// <value>
42 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
43 /// </value>
44 public override bool SupportsBrowserValidation
46 get { return true; }
49 /// <summary>
50 /// Applies the browser validation by setting up one or
51 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
52 /// </summary>
53 /// <param name="config">The config.</param>
54 /// <param name="inputType">Type of the input.</param>
55 /// <param name="generator">The generator.</param>
56 /// <param name="attributes">The attributes.</param>
57 /// <param name="target">The target.</param>
58 public override void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType,
59 IBrowserValidationGenerator generator, IDictionary attributes,
60 string target)
62 base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
64 generator.SetAsRequired(target, BuildErrorMessage());
67 /// <summary>
68 /// Returns the key used to internationalize error messages
69 /// </summary>
70 /// <value></value>
71 protected override string MessageKey
73 get { return MessageConstants.IsRequiredMessage; }