Showing posts with label validation. Show all posts
Showing posts with label validation. Show all posts

Wednesday, June 10, 2015

ELMAH filtering of exceptions by type using the is-type assertion

PROBLEM: In one of our projects, validation errors are thrown as exceptions (ValidationException) - we obviously don't want to be notified about a user forgetting a mandatory field etc, and luckily ELMAH has support to filter out certain exceptions from being logged and/or emailed.  Have a read here: https://code.google.com/p/elmah/wiki/ErrorFiltering and here https://code.google.com/p/elmah/wiki/ErrorFilterExamples and here http://www.diaryofaninja.com/blog/2011/09/20/adding-filters-to-your-elmah-installation

I tried the following in the web.config and it resulted in a runtime error:

<errorFilter>
  <test>
    <is-type binding="BaseException" type="MyProject.Models.ValidationException" />
  </test>
</errorFilter>

SOLUTION:
You need to include the assembly name e.g.

<errorFilter>
  <test>
    <is-type binding="BaseException" type="MyProject.Models.ValidationException, MyProject" />
  </test>
</errorFilter>

(this isn't in the limited doco of ELMAH but the creator of ELMAH does mention it in a random forum post i.e. "In the type attribute of the is-type element, you forgot to specify the assembly where the CustomLibrary.CustomException type can be found. In absence of the assembly specification, ELMAH looks for the type in its own assembly. If your assembly name is the same as the namespace (i.e. CustomLibrary) then try changing the is-type element to read as follows: <is-type binding="Exception" type="CustomLibrary.CustomException, CustomLibrary" />"

Tuesday, October 18, 2011

ASP.NET Validation Controls and Styles

PROBLEM: RequiredFieldValidator with Display="Dynamic" places display:inline on the error text when displayed (and I want it to be display:block)

SOLUTION: Well there is no elegant solution! Sometimes the rapid development tools that are normally so helpful in .net just aren't flexible enough to allow what you want to do. The issue is discussed here: http://www.stackoverflow.com/questions/2243498.

Essentially when the validator is first displayed it sets (via .net generated javascript) style="display:none" on the span tag. This element then gets switched from "none" to "inline" when there is an error to display.

Your options?

1. Modify the automatic asp.net javascript - not nice, and could possibly break after future upgrades or browser changes etc. - but probably your best bet if you are desperate - here is how: http://caliberwebgroup.blogspot.com/2009/02/overriding-javascript-in-webresourceaxd.html

2. Roll your own validation controls - possible but then you lose all the default out of the box goodness that come with the asp.net validator controls.

3. Live with it. I went with this option. Sometimes 95% is good enough.

Wednesday, October 12, 2011

Mandatory CheckBox

Problem: The RequiredFieldValidator doesn't allow you to enforce that a CheckBox in asp.net is checked/selected.

Solution: Best approach I could find was to simply use a CustomValidator (ideally with client and server side validation but server side would be sufficient). Example code is as follows:

ASPX PAGE:


ClientValidationFunction="ValidateTermsAndConditionsAgreement"
OnServerValidate="ValidateTermsAndConditionsAgreement"
runat="server"
Text=" You must agree to the terms and conditions before entering. "
ValidationGroup="vgEntryDetails"
CssClass="error"
Display="Dynamic">

CODE BEHIND:

Since I was actually optionally displaying my checkbox I actually added the validation script optionally in Page_Load (note, it needs to be done regardless of whether Page_Load is being called as part of a postback or not otherwise if there is a postback before you click submit the javascript you've injected via the code behind won't be there and hence the client side validation will not work).

//Script to use as part of the page validation
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "TermsAndConditionsScript"))
{
string scriptText = " function ValidateTermsAndConditionsAgreement(sender, args){" +
" if (document.getElementById('" + chkAgreeToTermsAndConditions.ClientID + "').checked){" +
" args.IsValid = true;" +
" }" +
" else {" +
" args.IsValid = false;" +
" }" +
" return;" +
"}";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"TermsAndConditionsScript", scriptText, true);
}

...and the server side validation

///
/// Validate that the terms and conditions have been agreed to if they exist
///
///
///
protected void ValidateTermsAndConditionsAgreement(object source, ServerValidateEventArgs args)
{
if (chkAgreeToTermsAndConditions.Checked == false)
{
args.IsValid = false;
}
}


...and finally don't forget to check IsValid in your method called by the submit button.


protected void btnSubmitEntry_Click(object sender, EventArgs e)
{
//Is valid check is required because we have a CustomerValidator control
//for the terms and conditions checkbox - if it's not valid we don't want to proceed and the error will be
//displayed.
if (Page.IsValid)
{
//Save competition entry
....