Skip to content
This repository has been archived by the owner on Oct 23, 2018. It is now read-only.

Commit

Permalink
v0.7.0.0: Support CheckBoxList.
Browse files Browse the repository at this point in the history
Adds RequiredFieldValidator support for CheckBoxList.
  • Loading branch information
roydukkey committed Aug 16, 2013
1 parent 7b7c12d commit e54d3b0
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dado.Validators/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//
// <major>.<minor>.<bugfix>.<devlopment>
//
[assembly: AssemblyVersion("0.6.0.0")]
[assembly: AssemblyVersion("0.7.0.0")]

[assembly: WebResource("Dado.js.validation.dev.js", "text/javascript")]
[assembly: WebResource("Dado.js.validation.min.js", "text/javascript")]
18 changes: 18 additions & 0 deletions Dado.Validators/js/validation.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ function TypeValidatorEvaluateIsValid(val) { var v = ValidatorGetValue(val.contr
}
}

function RequiredFieldValidatorEvaluateIsValid(val) {
if(val.ischeckboxlist === "false") {
return ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue);
}
// Validate for Checkbox and Radio Lists
else {
var list = document.getElementById(val.controltovalidate)
, inputs = list.getElementsByTagName("input")
, i = 0;

for (; i < inputs.length;)
if (inputs[i++].checked)
return true;

return false;
}
}

function ValidateOnLoad() {
Page_InvalidControlToBeFocused = null;
if (typeof(OnLoad_Validators) == "undefined") return true;
Expand Down
5 changes: 3 additions & 2 deletions Dado.Validators/js/validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 65 additions & 2 deletions Dado.Validators/validator/requiredField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Dado.Validators
using System.Diagnostics;
using System.Drawing;
using System.Web.UI;
using WebControls = System.Web.UI.WebControls;

/// <summary>
/// Makes the associated input control a required field.
Expand All @@ -24,6 +25,9 @@ public class RequiredFieldValidator : BaseValidator
#region Fields

private const string DEFAULT_ERROR_MESSAGE = "Please enter a value.";
private const string DEFAULT_LIST_ERROR_MESSAGE = "Please select a option.";
private bool _isCheckBoxList = false;
private bool _isRadioButtonList = false;

#endregion Fields

Expand Down Expand Up @@ -65,10 +69,55 @@ public string InitialValue
/// <param name="e">A <see cref='System.EventArgs'/> that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
DefaultErrorMessage = DEFAULT_ERROR_MESSAGE;
DefaultErrorMessage = PropertiesValid && (_isCheckBoxList || _isRadioButtonList) ? DEFAULT_LIST_ERROR_MESSAGE : DEFAULT_ERROR_MESSAGE;
base.OnInit(e);
}
/// <summary>
/// Determines whether the control specified by the ControlToValidate property is a valid control.
/// </summary>
/// <returns>true if the control specified by ControlToValidate is a valid control; otherwise, false.</returns>
protected override bool ControlPropertiesValid()
{
// Check for blank control to validate
string controlToValidate = ControlToValidate;
if (controlToValidate.Length == 0)
throw new Exception(
String.Format("The {0} property of '{1}' cannot be blank.", "ControlToValidate", ID)
);

// Check that the property points to a valid control. Will throw and exception if not found
CheckControlValidationProperty(controlToValidate, "ControlToValidate");

return true;
}
/// <summary>
/// Verifies whether the specified control is on the page and contains validation properties.
/// </summary>
/// <param name="name">The control to verify.</param>
/// <param name="propertyName">Additional text to describe the source of the exception, if an exception is thrown from using this method.</param>
new protected void CheckControlValidationProperty(string name, string propertyName)
{
// get the control using the relative name
Control c = NamingContainer.FindControl(name);
if (c == null)
throw new Exception(
String.Format("Unable to find control id '{0}' referenced by the '{1}' property of '{2}'.", name, propertyName, ID)
);

// Add Validation for CheckBoxList
if (_isCheckBoxList = c is WebControls.CheckBoxList) return;

// Allows Proper Default Error Message
_isRadioButtonList = c is WebControls.RadioButtonList;

// get its validation property
PropertyDescriptor prop = GetValidationProperty(c);
if (prop == null)
throw new Exception(
String.Format("Control '{0}' referenced by the {1} property of '{2}' cannot be validated.", name, propertyName, ID)
);
}
/// <summary>
/// Adds the HTML attributes and styles that need to be rendered for the control to the specified <see cref='System.Web.UI.HtmlTextWriter'/> object.
/// </summary>
/// <param name="writer">An <see cref='System.Web.UI.HtmlTextWriter'/> that represents the output stream to render HTML content on the client.</param>
Expand All @@ -78,8 +127,10 @@ protected override void AddAttributesToRender(HtmlTextWriter writer)
if (RenderUplevel) {
string id = ClientID;
HtmlTextWriter expandoAttributeWriter = (EnableLegacyRendering) ? writer : null;

AddExpandoAttribute(expandoAttributeWriter, id, "evaluationfunction", "RequiredFieldValidatorEvaluateIsValid", false);
AddExpandoAttribute(expandoAttributeWriter, id, "initialvalue", InitialValue);
AddExpandoAttribute(expandoAttributeWriter, id, "ischeckboxlist", _isCheckBoxList ? "true" : "false");
}
}
/// <summary>
Expand All @@ -88,9 +139,21 @@ protected override void AddAttributesToRender(HtmlTextWriter writer)
/// <returns>true if the value in the input control is valid; otherwise, false.</returns>
protected override bool EvaluateIsValid()
{
// Get the control value, return true if it is not found
string controlValue = GetControlValidationValue(ControlToValidate);
if (controlValue == null) {

// Provide Validation for CheckBoxList. Value check isn't provided by JavaScript and can't be provide.
if (_isCheckBoxList) {
WebControls.CheckBoxList c = (WebControls.CheckBoxList)NamingContainer.FindControl(ControlToValidate);

foreach (WebControls.ListItem item in c.Items)
if (item.Selected && !item.Value.Trim().Equals(InitialValue.Trim()))
return true;

return false;
}

// Get the control value, return true if it is not found
Debug.Fail("Should have been caught by PropertiesValid check");
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Dado.Validators (version 0.6.0.0)
Dado.Validators (version 0.7.0.0)
=================================

Dado Validators is an extension of the System.Web.UI.WebControls.BaseValidator class provided by .NET 4.

**Download** [Solution](//github.com/roydukkey/Dado.Validators/zipball/master) | [Assembly](//github.com/roydukkey/Dado.Validators/blob/master/Release/DadoValidatorsAssembly-v0.6.0.0.zip?raw=true)
**Download** [Solution](//github.com/roydukkey/Dado.Validators/zipball/master) | [Assembly](//github.com/roydukkey/Dado.Validators/blob/master/Release/DadoValidatorsAssembly-v0.7.0.0.zip?raw=true)


## Features
Expand Down Expand Up @@ -64,7 +64,7 @@ Dado Validators is an extension of the System.Web.UI.WebControls.BaseValidator c
</td>
<td valign="top">
<strong>Require Field:</strong><br />
Makes the associated input control a required field.
Makes the associated input control a required field. Added support for CheckBoxLists.
</td>
</tr>
<tr>
Expand Down
Binary file added Release/DadoValidatorsAssembly-v0.7.0.0.zip
Binary file not shown.
24 changes: 23 additions & 1 deletion Web/default.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<tr>
<th valign="top" align="right">Phone Validator:</th>
<td valign="top">
<asp:TextBox ID="txtPhoneNumber" runat="server" Text="(555) 896-4571" />
<asp:TextBox ID="txtPhoneNumber" runat="server" Text="(555) 896-4571 x345" />
<Dado:PhoneValidator ID="vldPhoneNumber" runat="server" ControlToValidate="txtPhoneNumber" ValidationGroup="vlgSubmit" AllowExtension="true" />
</td>
<th valign="top" align="right"></th>
Expand Down Expand Up @@ -105,6 +105,28 @@
<Dado:UriValidator runat="server" ControlToValidate="txtUri" ValidationGroup="vlgSubmit" Kind="Absolute" TryToFix="true" />
</td>
</tr>
<tr>
<th valign="top" align="right">Checkbox List:</th>
<td valign="top">
<asp:CheckBoxList ID="cblCheckBoxList" runat="server" RepeatLayout="Flow">
<asp:ListItem Text="Check Box (empty)" Value="" />
<asp:ListItem Text="Check Box 1" Value="1" />
<asp:ListItem Text="Check Box 2" Value="2" />
<asp:ListItem Text="Check Box 3" Value="3" />
</asp:CheckBoxList>
<Dado:RequiredFieldValidator runat="server" ControlToValidate="cblCheckBoxList" ValidationGroup="vlgSubmit" />
</td>
<th valign="top" align="right">Radio Button List:</th>
<td valign="top">
<asp:RadioButtonList ID="rblRadioButtonList" runat="server" RepeatLayout="Flow">
<asp:ListItem Text="Radio Button (empty)" Value="" />
<asp:ListItem Text="Radio Button 1" Value="1" />
<asp:ListItem Text="Radio Button 2" Value="2" />
<asp:ListItem Text="Radio Button 3" Value="3" />
</asp:RadioButtonList>
<Dado:RequiredFieldValidator runat="server" ControlToValidate="rblRadioButtonList" ValidationGroup="vlgSubmit" />
</td>
</tr>
</table>

<asp:Button ID="btnSubmit" runat="server" ValidationGroup="vlgSubmit" Text="Submit" />
Expand Down
9 changes: 7 additions & 2 deletions Web/default.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ protected void Page_Init(object sender, EventArgs e)
{
Page.Validate("vlgSubmit");
if (Page.IsValid) {
litTest.Text += "<p>";
litTest.Text += "vldPhoneNumber.ValidatedPhoneNumber: " + vldPhoneNumber.ValidatedPhoneNumber;
litTest.Text += "<br />";
litTest.Text += vldPhoneNumber.ValidatedPhoneNumber;
litTest.Text += "vldPhoneNumber.ValidatedExtension: " + vldPhoneNumber.ValidatedExtension;
litTest.Text += "<br />";
litTest.Text += vldPhoneNumber.ValidatedExtension;
litTest.Text += "cblCheckBoxList.SelectedValue: " + cblCheckBoxList.SelectedValue;
litTest.Text += "<br />";
litTest.Text += "rblRadioButtonList.SelectedValue: " + rblRadioButtonList.SelectedValue;
litTest.Text += "</p>";
}
};

Expand Down
18 changes: 18 additions & 0 deletions Web/default.aspx.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Website Framework.v11.suo
Binary file not shown.

0 comments on commit e54d3b0

Please sign in to comment.