BrowserValidationEngine.cs :  » Content-Management-Systems-CMS » Cuyahoga » Cuyahoga » Core » Validation » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Cuyahoga 
Cuyahoga » Cuyahoga » Core » Validation » BrowserValidationEngine.cs
using System;
using System.Text;
using Castle.Components.Validator;
using Cuyahoga.Core.Validation;

namespace Cuyahoga.Core.Validation{
  /// <summary>
  /// Responsible for generating client-side validation scripts.
  /// </summary>
  public class BrowserValidationEngine
  {
    private ILocalizedValidatorRegistry _validatorRegistry;
    private IBrowserValidatorProvider _browserValidatorProvider;

    /// <summary>
    /// Creates a new instance of the <see cref="BrowserValidationEngine"></see> class.
    /// </summary>
    /// <param name="validatorRegistry">The validator registry where the validators are obtained.</param>
    /// <param name="browserValidatorProvider">The provider that generates client script for the validators.</param>
    public BrowserValidationEngine(ILocalizedValidatorRegistry validatorRegistry, IBrowserValidatorProvider browserValidatorProvider)
    {
      _validatorRegistry = validatorRegistry;
      _browserValidatorProvider = browserValidatorProvider;
    }

    /// <summary>
    /// Generates a client-side validation script based on the attributes of the modelToValidate.
    /// </summary>
    /// <param name="modelToValidate">The model to validate.</param>
    /// <param name="formId">The client ID of the form.</param>
    /// <param name="propertyNameToElementId">Delegate that can translate properties of the model to form elements.</param>
    /// <returns></returns>
    public string GenerateClientScript(object modelToValidate, string formId, Func<string, string> propertyNameToElementId)
    {
      // Create a BrowserValidationConfiguration instance to store all validation rules.
      BrowserValidationConfiguration config = this._browserValidatorProvider.CreateConfiguration(null);
      // Create a ValidatorRunner that is needed to obtain the validators for the given modelToValidate.
      var validatorRunner = new ValidatorRunner(this._validatorRegistry);
      // Create a script generator and that is linked to the BrowserValidationConfiguration
      IBrowserValidationGenerator generator = 
        _browserValidatorProvider.CreateGenerator(config, InputElementType.Undefined, propertyNameToElementId);
      // Get all validators for the given modelToValidate
      IValidator[] validators = 
        _validatorRegistry.GetValidators(validatorRunner, modelToValidate.GetType(), RunWhen.Everytime);
      // Iterate the validators and call ApplyBrowserValidation to generate the validation rules and messages into the BrowserValidationConfiguration.
      foreach (var validator in validators)
      {
        validator.ApplyBrowserValidation(config, InputElementType.Undefined, generator, null, propertyNameToElementId(validator.Property.Name));
      }

      // Generate the validation script block
      var sb = new StringBuilder();
      sb.Append("<script type=\"text/javascript\">" + Environment.NewLine);
      // Call CreateBeforeFormClosed of the BrowserValidationConfiguration. This generates the client script. 
      // Note: it's called CreateBeforeFormClosed because originally (in Monorail), it was used to only 
      // generate some extra scripts for validation.
      sb.Append(config.CreateBeforeFormClosed(formId));
      sb.Append("</script>" + Environment.NewLine);
      return sb.ToString();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.