ASP.Net Validators and BrokenRules?

ASP.Net Validators and BrokenRules?

Old forum URL: forums.lhotka.net/forums/t/801.aspx


jas_nhs posted on Thursday, August 03, 2006

Is there a simple way to link these two so that a control's underlying business object's 'broken rule' appears as a validator error message? I believe this is straightforward with WinForms, but with a web form? (see here )

Currently, I am simply populating a list with the error text, and would rather show validation errors in the more conventional way.

TIA

James.



RockfordLhotka replied on Wednesday, August 09, 2006

No, unfortunately ASP.NET doesn't have any way to support this...

I know that some people have written UI components that reflect against business object properties to pick up attributes that trigger dynamic application of validation controls. That doesn't cover all cases, but does cover the ones you can handle with simple validation controls.

This is made harder yet in ASP.NET 2.0, because you are likely using a DetailsView control, and dynamically adding validation controls there is a bit more challenging. I think that Dino Esposito wrote an MSDN article about doing this a few months ago - it was pretty challenging, but he got it to work.

SteveChadbourne replied on Wednesday, August 09, 2006

I have managed to do this as follows:

1. I placed a validation summary control on the master page.

2. I created a class containing a dummy validator (code found on web somewhere - see below)

3. If your csla class is invalid, you can pass the broken rule collection to the following method (I placed all this code in a base page class). I also use ShowPageError() to display exceptions. 

Works very well - the same validation summary on the master page can be used by both the client side validators and the csla server side validation.

*********  base page code ****************

private ValidationSummary _customSummary = null;

protected ValidationSummary CustomSummary

{

get

{

if (_customSummary == null)

{

_customSummary = new ValidationSummary();

}

return _customSummary;

}

}

protected void ShowBrokenRules(BrokenRulesCollection brokenRulesCollection)

{

foreach (BrokenRule br in brokenRulesCollection)

{

ShowPageError(br.Description);

}

}

protected void ShowPageError(string errorDescription)

{

System.Web.UI.Page pageRef = this.Page;

CustomSummary.AddErrorMessage(errorDescription, pageRef);

}

 

*********  Dummy Validator class****************

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

namespace MyNamespace

{

/// <summary>

/// This control inherits the ASP.NET ValidationSummary control but adds

/// the ability to dynamically add error messages without requiring

/// validation controls.

/// </summary>

public class ValidationSummary : System.Web.UI.WebControls.ValidationSummary

{

/// <summary>

/// Allows the caller to place custom text messages inside the validation

/// summary control

/// </summary>

/// <param name="msg">The message you want to appear in the summary</param>

/// <param name="pageRef">The page where the message will appear</param>

public void AddErrorMessage(string msg, System.Web.UI.Page pageRef)

{

System.Web.UI.Page mypageRef = pageRef;

mypageRef.Validators.Add(new DummyValidator(msg));

}

}

/// <summary>

/// The validation summary control works by iterating over the Page.Validators

/// collection and displaying the ErrorMessage property of each validator

/// that return false for the IsValid() property. This class will act

/// like all the other validators except it always is invalid and thus the

/// ErrorMessage property will always be displayed.

/// </summary>

internal class DummyValidator : IValidator

{

private string errorMsg;

public DummyValidator(string msg)

{

errorMsg = msg;

}

public string ErrorMessage

{

get{ return errorMsg;}

set{ errorMsg = value;}

}

public bool IsValid

{

get{return false;}

set{}

}

public void Validate()

{

}

}

}

Copyright (c) Marimer LLC