157 lines
6.4 KiB
C#
157 lines
6.4 KiB
C#
using Sacs.Web.Components;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Helpers;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Base.App;
|
|
using Saxonite.Net.Core.Web.Base.App;
|
|
using Saxonite.Net.Core.Web.Base.Controllers;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Saxonite.Net.Core.Web.Admin.Models
|
|
{
|
|
//=======================================================================
|
|
/// <summary>
|
|
/// Address model data properties.
|
|
/// </summary>
|
|
public class ModelData_Address : ModelData_Admin
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public ModelData_Address(SaxModel_Admin model, String controlNameSuffix) : base(model)
|
|
{
|
|
this.ControlNameSuffix = StringUtil.EmptyOnNull(controlNameSuffix) + "Address";
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
private String ControlNameSuffix { get; }
|
|
// Address properties.
|
|
public String Id { get; set; }
|
|
public String Line1 { get; set; }
|
|
public String Line2 { get; set; }
|
|
public String Line3 { get; set; }
|
|
public String Town { get; set; }
|
|
public String Province { get; set; }
|
|
public String RegionIdProvince { get; set; }
|
|
public String RegionIdCountry { get; set; }
|
|
public String PostalCode { get; set; }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Utilities.
|
|
/// <summary>Have any address values been supplied by the form.</summary>
|
|
public bool IsSpecified()
|
|
{
|
|
if (Id != null || Line1 != null || Town != null || Province != null || RegionIdCountry != null || PostalCode != null)
|
|
return true;
|
|
return false;
|
|
}
|
|
/// <summary>Have any address values been populated on the form.</summary>
|
|
public bool IsPopulated()
|
|
{
|
|
if (SysId.IsValid(SysId.ToSysId(Id)) || !StringUtil.IsEmpty(Line1) || !StringUtil.IsEmpty(Town) || !StringUtil.IsEmpty(Province) || !StringUtil.IsEmpty(RegionIdCountry) || !StringUtil.IsEmpty(PostalCode))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validation.
|
|
//-------------------------------------------------------------------
|
|
// Address.
|
|
public bool ValidateAddressData(SaxModel_Admin model, SaController.AjaxResponse_Data responseData)
|
|
{
|
|
// Are any properties populated.
|
|
bool populated = IsPopulated();
|
|
|
|
// Address Line 1 - optional.
|
|
if (Line1 != null)
|
|
{
|
|
responseData.SetControlValue(ControlNameSuffix + "Line1", Line1);
|
|
if (StringUtil.GetLength(Line1) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Line1))
|
|
responseData.SetControlError(ControlNameSuffix + "Line1", Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Line1)]);
|
|
}
|
|
|
|
// Address Line 2 - optional.
|
|
if (Line2 != null)
|
|
{
|
|
responseData.SetControlValue(ControlNameSuffix + "Line2", Line2);
|
|
if (!StringUtil.IsEmpty(Line2))
|
|
{
|
|
if (StringUtil.GetLength(Line2) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Line2))
|
|
responseData.SetControlError(ControlNameSuffix + "Line2", Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Line2)]);
|
|
}
|
|
}
|
|
|
|
// Address Line 3 - optional.
|
|
if (Line3 != null)
|
|
{
|
|
responseData.SetControlValue(ControlNameSuffix + "Line3", Line3);
|
|
if (!StringUtil.IsEmpty(Line3))
|
|
{
|
|
if (StringUtil.GetLength(Line3) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Line3))
|
|
responseData.SetControlError(ControlNameSuffix + "Line3", Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Line3)]);
|
|
}
|
|
}
|
|
|
|
// Address Town - mandatory if any value populated.
|
|
if (Town != null)
|
|
{
|
|
String controlName = ControlNameSuffix + nameof(Town);
|
|
responseData.SetControlValue(controlName, Town);
|
|
if (StringUtil.IsEmpty(Town) && populated)
|
|
responseData.SetControlError(controlName, Model.Localiser["msg.validation.mandatory.value"]);
|
|
else if (StringUtil.GetLength(Town) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Town))
|
|
responseData.SetControlError(controlName, Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Town)]);
|
|
}
|
|
|
|
// Address Province - optional.
|
|
if (Province != null)
|
|
{
|
|
String controlName = ControlNameSuffix + nameof(Province);
|
|
responseData.SetControlValue(controlName, Province);
|
|
if (!StringUtil.IsEmpty(Province))
|
|
{
|
|
if (StringUtil.GetLength(Province) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Province))
|
|
responseData.SetControlError(controlName, Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_Province)]);
|
|
}
|
|
}
|
|
|
|
// Address Country - mandatory if any value populated.
|
|
if (RegionIdCountry != null)
|
|
{
|
|
String controlName = ControlNameSuffix + nameof(RegionIdCountry);
|
|
if (!SysId.IsValid(SysId.ToSysId(RegionIdCountry)) && populated)
|
|
responseData.SetControlError(controlName, Model.Localiser["msg.validation.mandatory.value"]);
|
|
else if (SysId.IsValid(SysId.ToSysId(RegionIdCountry)))
|
|
{
|
|
SaxRegion.Data region = model.Internals.GetRegion(SysId.ToSysId(RegionIdCountry));
|
|
if (region == null || region.RegionType != Sax.RegionType.Country)
|
|
responseData.SetControlError(controlName, Model.Localiser["msg.validation.invalid.value"]);
|
|
}
|
|
}
|
|
|
|
// Address PostalCode - optional.
|
|
if (PostalCode != null)
|
|
{
|
|
responseData.SetControlValue(ControlNameSuffix + "PostalCode", PostalCode);
|
|
if (!StringUtil.IsEmpty(PostalCode))
|
|
{
|
|
if (StringUtil.GetLength(PostalCode) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_PostalCode))
|
|
responseData.SetControlError(ControlNameSuffix + "PostalCode", Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.ItemAddress_PostalCode)]);
|
|
}
|
|
}
|
|
|
|
return !responseData.HasControlErrors();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|