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 CheckBox.
Browse files Browse the repository at this point in the history
Adds RequiredFieldValidator support for CheckBox.
  • Loading branch information
roydukkey committed Aug 16, 2013
1 parent e54d3b0 commit db6db1f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
13 changes: 9 additions & 4 deletions Dado.Validators/js/validation.dev.js
Expand Up @@ -90,11 +90,12 @@ 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
if(val.validatefor === "checkbox") {
return document.getElementById(val.controltovalidate).checked
}
// Validate for Checkbox and Radio Lists
else {
// Validate for CheckboxList
else if(val.validatefor === "checkboxlist") {
var list = document.getElementById(val.controltovalidate)
, inputs = list.getElementsByTagName("input")
, i = 0;
Expand All @@ -105,6 +106,10 @@ function RequiredFieldValidatorEvaluateIsValid(val) {

return false;
}
// Default provided validation
else {
return ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue);
}
}

function ValidateOnLoad() {
Expand Down
3 changes: 2 additions & 1 deletion Dado.Validators/js/validation.min.js

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

31 changes: 25 additions & 6 deletions Dado.Validators/validator/requiredField.cs
Expand Up @@ -25,7 +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 const string DEFAULT_CHECKBOX_ERROR_MESSAGE = "You must select this option.";
private const string DEFAULT_LIST_ERROR_MESSAGE = "Please select an option.";
private bool _isCheckBox = false;
private bool _isCheckBoxList = false;
private bool _isRadioButtonList = false;

Expand Down Expand Up @@ -69,7 +71,11 @@ public string InitialValue
/// <param name="e">A <see cref='System.EventArgs'/> that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
DefaultErrorMessage = PropertiesValid && (_isCheckBoxList || _isRadioButtonList) ? DEFAULT_LIST_ERROR_MESSAGE : DEFAULT_ERROR_MESSAGE;
DefaultErrorMessage = PropertiesValid && _isCheckBox
? DEFAULT_CHECKBOX_ERROR_MESSAGE
: (_isCheckBoxList || _isRadioButtonList)
? DEFAULT_LIST_ERROR_MESSAGE
: DEFAULT_ERROR_MESSAGE;
base.OnInit(e);
}
/// <summary>
Expand Down Expand Up @@ -104,6 +110,9 @@ protected override bool ControlPropertiesValid()
String.Format("Unable to find control id '{0}' referenced by the '{1}' property of '{2}'.", name, propertyName, ID)
);

// Add Validation for CheckBox
if (_isCheckBox = (c is WebControls.CheckBox && !(c is WebControls.RadioButton))) return;

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

Expand All @@ -130,7 +139,13 @@ protected override void AddAttributesToRender(HtmlTextWriter writer)

AddExpandoAttribute(expandoAttributeWriter, id, "evaluationfunction", "RequiredFieldValidatorEvaluateIsValid", false);
AddExpandoAttribute(expandoAttributeWriter, id, "initialvalue", InitialValue);
AddExpandoAttribute(expandoAttributeWriter, id, "ischeckboxlist", _isCheckBoxList ? "true" : "false");
AddExpandoAttribute(expandoAttributeWriter, id, "validatefor",
_isCheckBox
? "checkbox"
: _isCheckBoxList
? "checkboxlist"
: "default"
);
}
}
/// <summary>
Expand All @@ -142,12 +157,16 @@ protected override bool EvaluateIsValid()
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) {
// Provide Validation for CheckBox.
if (_isCheckBox) {
return ((WebControls.CheckBox)NamingContainer.FindControl(ControlToValidate)).Checked;
}
// Provide Validation for CheckBoxList.
else 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()))
if (item.Selected)
return true;

return false;
Expand Down
2 changes: 1 addition & 1 deletion README.md
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. Added support for CheckBoxLists.
Makes the associated input control a required field. Added support for CheckBox and CheckBoxLists.
</td>
</tr>
<tr>
Expand Down
Binary file modified Release/DadoValidatorsAssembly-v0.7.0.0.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions Web/default.aspx
Expand Up @@ -127,6 +127,14 @@
<Dado:RequiredFieldValidator runat="server" ControlToValidate="rblRadioButtonList" ValidationGroup="vlgSubmit" />
</td>
</tr>
<tr>
<th valign="top" align="right">CheckBox Validator:</th>
<td valign="top">
<asp:CheckBox ID="ckbCheckBox" runat="server" Text="I agree with the policies and terms." />
<Dado:RequiredFieldValidator runat="server" ControlToValidate="ckbCheckBox" ValidationGroup="vlgSubmit" />
</td>
<td colspan="2"></td>
</tr>
</table>

<asp:Button ID="btnSubmit" runat="server" ValidationGroup="vlgSubmit" Text="Submit" />
Expand Down
9 changes: 9 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 db6db1f

Please sign in to comment.