Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Web/Web/UI
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv31974
Modified Files:
UserControl.cs
Log Message:
SPRNET-558 - Enable Data Validation for UserControl (Partial fix)
Index: UserControl.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Web/Web/UI/UserControl.cs,v
retrieving revision 1.50
retrieving revision 1.51
diff -C2 -d -r1.50 -r1.51
*** UserControl.cs 2 Feb 2008 09:12:44 -0000 1.50
--- UserControl.cs 26 Feb 2008 19:06:11 -0000 1.51
***************
*** 39,44 ****
using Spring.DataBinding;
using Spring.Globalization;
! using Spring.Util;
using Spring.Web.Support;
#endregion
--- 39,45 ----
using Spring.DataBinding;
using Spring.Globalization;
! using Spring.Validation;
using Spring.Web.Support;
+ using IValidator = Spring.Validation.IValidator;
#endregion
***************
*** 69,72 ****
--- 70,74 ----
private IDictionary sharedState;
private IBindingContainer bindingManager;
+ private IValidationErrors validationErrors = new ValidationErrors();
private IDictionary results;
private IApplicationContext applicationContext;
***************
*** 117,120 ****
--- 119,131 ----
InitializeBindingManager();
+ if (!IsPostBack)
+ {
+ InitializeModel();
+ }
+ else
+ {
+ LoadModel(LoadModelFromPersistenceMedium());
+ }
+
base.OnInit(e);
***************
*** 236,239 ****
--- 247,256 ----
base.OnPreRender(e);
+
+ object modelToSave = SaveModel();
+ if (modelToSave != null)
+ {
+ SaveModelToPersistenceMedium(modelToSave);
+ }
}
***************
*** 291,294 ****
--- 308,376 ----
#endregion
+ #region Model Management Support
+
+ /// <summary>
+ /// Initializes data model when the page is first loaded.
+ /// </summary>
+ /// <remarks>
+ /// This method should be overriden by the developer
+ /// in order to initialize data model for the page.
+ /// </remarks>
+ protected virtual void InitializeModel()
+ {
+ }
+
+ /// <summary>
+ /// Retrieves data model from a persistence store.
+ /// </summary>
+ /// <remarks>
+ /// The default implementation uses <see cref="System.Web.UI.Page.Session"/> to store and retrieve
+ /// the model for the current <see cref="System.Web.HttpRequest.CurrentExecutionFilePath" />
+ /// </remarks>
+ protected virtual object LoadModelFromPersistenceMedium()
+ {
+ return Session[Request.CurrentExecutionFilePath + this.UniqueID + ".Model"];
+ }
+
+ /// <summary>
+ /// Saves data model to a persistence store.
+ /// </summary>
+ /// <remarks>
+ /// The default implementation uses <see cref="System.Web.UI.Page.Session"/> to store and retrieve
+ /// the model for the current <see cref="System.Web.HttpRequest.CurrentExecutionFilePath" />
+ /// </remarks>
+ protected virtual void SaveModelToPersistenceMedium(object modelToSave)
+ {
+ Session[Request.CurrentExecutionFilePath + this.UniqueID + ".Model"] = modelToSave;
+ }
+
+ /// <summary>
+ /// Loads the saved data model on postback.
+ /// </summary>
+ /// <remarks>
+ /// This method should be overriden by the developer
+ /// in order to load data model for the page.
+ /// </remarks>
+ protected virtual void LoadModel(object savedModel)
+ {
+ }
+
+ /// <summary>
+ /// Returns a model object that should be saved.
+ /// </summary>
+ /// <remarks>
+ /// This method should be overriden by the developer
+ /// in order to save data model for the page.
+ /// </remarks>
+ /// <returns>
+ /// A model object that should be saved.
+ /// </returns>
+ protected virtual object SaveModel()
+ {
+ return null;
+ }
+
+ #endregion<
+
#region Controller Support
***************
*** 466,469 ****
--- 548,626 ----
#endregion
+ #region Validation support
+
+ /// <summary>
+ /// Evaluates specified validators and returns <c>True</c> if all of them are valid.
+ /// </summary>
+ /// <remarks>
+ /// <p>
+ /// Each validator can itself represent a collection of other validators if it is
+ /// an instance of <see cref="ValidatorGroup"/> or one of its derived types.
+ /// </p>
+ /// <p>
+ /// Please see the Validation Framework section in the documentation for more info.
+ /// </p>
+ /// </remarks>
+ /// <param name="validationContext">Object to validate.</param>
+ /// <param name="validators">Validators to evaluate.</param>
+ /// <returns>
+ /// <c>True</c> if all of the specified validators are valid, <c>False</c> otherwise.
+ /// </returns>
+ public bool Validate(object validationContext, params IValidator[] validators)
+ {
+ IDictionary contextParams = CreateValidatorParameters();
+ bool result = true;
+ foreach (IValidator validator in validators)
+ {
+ if (validator == null)
+ {
+ throw new ArgumentException("Validator is not defined.");
+ }
+ result = validator.Validate(validationContext, contextParams, this.validationErrors) && result;
+ }
+
+ return result;
+ }
+
+ /// <summary>
+ /// Gets the validation errors container.
+ /// </summary>
+ /// <value>The validation errors container.</value>
+ public IValidationErrors ValidationErrors
+ {
+ get { return validationErrors; }
+ }
+
+ /// <summary>
+ /// Creates the validator parameters.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// This method can be overriden if you want to pass additional parameters
+ /// to the validation framework, but you should make sure that you call
+ /// this base implementation in order to add page, session, application,
+ /// request, response and context to the variables collection.
+ /// </para>
+ /// </remarks>
+ /// <returns>
+ /// Dictionary containing parameters that should be passed to
+ /// the data validation framework.
+ /// </returns>
+ protected virtual IDictionary CreateValidatorParameters()
+ {
+ IDictionary parameters = new ListDictionary();
+ parameters["page"] = this.Page;
+ parameters["usercontrol"] = this;
+ parameters["session"] = this.Session;
+ parameters["application"] = this.Application;
+ parameters["request"] = this.Request;
+ parameters["response"] = this.Response;
+ parameters["context"] = this.Context;
+
+ return parameters;
+ }
+
+ #endregion
+
#region Data binding support
|