Skip to content

Commit

Permalink
+ (Core) Fixed the LocationPicker to correctly validate content when …
Browse files Browse the repository at this point in the history
…set to required. (Fixes #5561)
  • Loading branch information
MrUpsideDown committed Nov 2, 2023
1 parent cf038d8 commit 6a5f93b
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 56 deletions.
42 changes: 35 additions & 7 deletions Rock/Web/UI/Controls/AddressControl.cs
Expand Up @@ -665,6 +665,25 @@ public ValidatorDisplay ValidationDisplay
}
}

/// <summary>
/// Gets or sets a value indicating whether validation is disabled for this control.
/// </summary>
/// <value>
/// <c>true</c> if validation is disabled; otherwise, <c>false</c>.
/// </value>
public bool ValidationIsDisabled
{
get
{
return ViewState["ValidationIsDisabled"] as bool? ?? false;
}

set
{
ViewState["ValidationIsDisabled"] = value;
}
}

/// <summary>
/// Gets or sets a value indicating if the entry must satisfy the address requirements for the selected country to be considered valid.
/// </summary>
Expand Down Expand Up @@ -826,23 +845,25 @@ protected override void CreateChildControls()
CustomValidator.ID = this.ID + "_cfv";
CustomValidator.ClientValidationFunction = "Rock.controls.addressControl.clientValidate";
CustomValidator.CssClass = "validation-error";
CustomValidator.Enabled = true;
CustomValidator.ServerValidate += _CustomValidator_ServerValidate;

// Disable required field validation if partial address input is enabled.
CustomValidator.Enabled = !this.PartialAddressIsAllowed;

Controls.Add( CustomValidator );
}

private void _CustomValidator_ServerValidate( object source, ServerValidateEventArgs args )
{
if ( this.ValidationIsDisabled )
{
return;
}
if ( !this.HasValue
&& !this.Required )
{
return;
}

var validator = source as CustomValidator;

// Get the edited Location, and include any default values to avoid incorrect validation messages for the fields
// to which they apply.
if ( this.PartialAddressIsAllowed )
Expand All @@ -858,16 +879,23 @@ private void _CustomValidator_ServerValidate( object source, ServerValidateEvent

if ( !isValid )
{
var _addressRequirementsValidator = source as CustomValidator;

_addressRequirementsValidator.ErrorMessage = validationMessage;
validator.ErrorMessage = validationMessage;

args.IsValid = false;

return;
}
}

/// <inheritdoc />
protected override void OnPreRender( EventArgs e )
{
// Disable required field validation if partial address input is enabled.
CustomValidator.Enabled = !( this.ValidationIsDisabled || this.PartialAddressIsAllowed );

base.OnPreRender( e );
}

/// <summary>
/// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter" /> object and stores tracing information about the control if tracing is enabled.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions Rock/Web/UI/Controls/Pickers/GeoPicker.cs
Expand Up @@ -217,7 +217,6 @@ public ValidatorDisplay ValidationDisplay
set { ViewState["ValidationDisplay"] = value; }
}


/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
Expand Down Expand Up @@ -635,7 +634,7 @@ protected override void CreateChildControls()
RequiredFieldValidator.ControlToValidate = _hfGeoPath.ID;

_validator = new CustomValidator();
_validator.ID = this.ID + "_CV";
_validator.ID = this.ClientID + "_CV";
_validator.Display = ValidatorDisplay.None;
_validator.CssClass = "validation-error help-inline";
_validator.EnableClientScript = false;
Expand Down
35 changes: 32 additions & 3 deletions Rock/Web/UI/Controls/Pickers/LocationAddressPicker.cs
Expand Up @@ -225,6 +225,25 @@ public string ValidationGroup
}
}

/// <summary>
/// Gets or sets a value indicating whether validation is disabled for this control.
/// </summary>
/// <value>
/// <c>true</c> if validation is disabled; otherwise, <c>false</c>.
/// </value>
public bool ValidationIsDisabled
{
get
{
return ViewState["ValidationIsDisabled"] as bool? ?? false;
}

set
{
ViewState["ValidationIsDisabled"] = value;
}
}

/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
Expand All @@ -235,7 +254,7 @@ public virtual bool IsValid
{
get
{
return ( !Required || RequiredFieldValidator == null || RequiredFieldValidator.IsValid ) && _addressRequirementsValidator.IsValid;
return _acAddress.IsValid && _addressRequirementsValidator.IsValid;
}
}

Expand Down Expand Up @@ -433,10 +452,20 @@ protected override void OnLoad( EventArgs e )

EnsureChildControls();

_acAddress.ValidationIsDisabled = this.ValidationIsDisabled;

ScriptManager.GetCurrent( this.Page ).RegisterAsyncPostBackControl( _btnSelect );
ScriptManager.GetCurrent( this.Page ).RegisterAsyncPostBackControl( _btnSelectNone );
}

/// <inheritdoc />
protected override void OnPreRender( EventArgs e )
{
base.OnPreRender( e );

_acAddress.Required = this.Required;
}

/// <summary>
/// Renders the control to the specified HTML writer.
/// </summary>
Expand Down Expand Up @@ -510,8 +539,8 @@ protected override void CreateChildControls()
_pnlPickerActions.Controls.Add( _btnCancel );

_addressRequirementsValidator.ID = ID + "_addressrequirementsvalidator";
_addressRequirementsValidator.CssClass = "validation-error help-inline";
_addressRequirementsValidator.Enabled = true;
_addressRequirementsValidator.CssClass = "validation-error";
_addressRequirementsValidator.Enabled = !this.ValidationIsDisabled;
_addressRequirementsValidator.Display = ValidatorDisplay.None;
_addressRequirementsValidator.ValidationGroup = ValidationGroup;
this.Controls.Add( _addressRequirementsValidator );
Expand Down

0 comments on commit 6a5f93b

Please sign in to comment.