From 348dbd71073508e7c233326792fecd24cb640f56 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Fri, 17 Mar 2023 06:37:37 +0100 Subject: [PATCH] Support Form Select option (#4726) * Support Form Select option * Add country select --- BTCPayServer/Forms/FormDataExtensions.cs | 1 + BTCPayServer/Forms/FormDataService.cs | 10 +++++- BTCPayServer/Forms/HtmlSelectFormProvider.cs | 32 +++++++++++++++++++ .../Views/Shared/Forms/SelectElement.cshtml | 28 ++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 BTCPayServer/Forms/HtmlSelectFormProvider.cs create mode 100644 BTCPayServer/Views/Shared/Forms/SelectElement.cshtml diff --git a/BTCPayServer/Forms/FormDataExtensions.cs b/BTCPayServer/Forms/FormDataExtensions.cs index 5fc6b48bb..eb53ce732 100644 --- a/BTCPayServer/Forms/FormDataExtensions.cs +++ b/BTCPayServer/Forms/FormDataExtensions.cs @@ -14,6 +14,7 @@ public static class FormDataExtensions serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); } public static JObject Deserialize(this FormData form) diff --git a/BTCPayServer/Forms/FormDataService.cs b/BTCPayServer/Forms/FormDataService.cs index fd6e0e77c..bc1b4753a 100644 --- a/BTCPayServer/Forms/FormDataService.cs +++ b/BTCPayServer/Forms/FormDataService.cs @@ -44,7 +44,15 @@ public class FormDataService Field.Create("City", "buyerCity", null, true, null), Field.Create("Postcode", "buyerZip", null, false, null), Field.Create("State", "buyerState", null, false, null), - Field.Create("Country", "buyerCountry", null, true, null) + new SelectField() + { + Name = "buyerCountry", + Label = "Country", + Required = true, + Type = "select", + Options = "Afghanistan, Albania, Algeria, Andorra, Angola, Antigua and Barbuda, Argentina, Armenia, Australia, Austria, Azerbaijan, The Bahamas, Bahrain, Bangladesh, Barbados, Belarus, Belgium, Belize, Benin, Bhutan, Bolivia, Bosnia and Herzegovina, Botswana, Brazil, Brunei, Bulgaria, Burkina Faso, Burundi, Cabo Verde, Cambodia, Cameroon, Canada, Central African Republic (CAR), Chad, Chile, China, Colombia, Comoros, Democratic Republic of the Congo, Republic of the Congo, Costa Rica, Cote d'Ivoire, Croatia, Cuba, Cyprus, Czech Republic, Denmark, Djibouti, Dominica, Dominican Republic, Ecuador, Egypt, El Salvador, Equatorial Guinea, Eritrea, Estonia, Eswatini (formerly Swaziland), Ethiopia, Fiji, Finland, France, Gabon, The Gambia, Georgia, Germany, Ghana, Greece, Grenada, Guatemala, Guinea, Guinea-Bissau, Guyana, Haiti, Honduras, Hungary, Iceland, India, Indonesia, Iran, Iraq, Ireland, Israel, Italy, Jamaica, Japan, Jordan, Kazakhstan, Kenya, Kiribati, Kosovo, Kuwait, Kyrgyzstan, Laos, Latvia, Lebanon, Lesotho, Liberia, Libya, Liechtenstein, Lithuania, Luxembourg, Madagascar, Malawi, Malaysia, Maldives, Mali, Malta, Marshall Islands, Mauritania, Mauritius, Mexico, Micronesia, Moldova, Monaco, Mongolia, Montenegro, Morocco, Mozambique, Myanmar (formerly Burma), Namibia, Nauru, Nepal, Netherlands, New Zealand, Nicaragua, Niger, Nigeria, North Korea, North Macedonia (formerly Macedonia), Norway, Oman, Pakistan, Palau, Palestine, Panama, Papua New Guinea, Paraguay, Peru, Philippines, Poland, Portugal, Qatar, Romania, Russia, Rwanda, Saint Kitts and Nevis, Saint Lucia, Saint Vincent and the Grenadines, Samoa, San Marino, Sao Tome and Principe, Saudi Arabia, Senegal, Serbia, Seychelles, Sierra Leone, Singapore, Slovakia, Slovenia, Solomon Islands, Somalia, South Africa, South Korea, South Sudan, Spain, Sri Lanka, Sudan, Suriname, Sweden, Switzerland, Syria, Taiwan, Tajikistan, Tanzania, Thailand, Timor-Leste (formerly East Timor), Togo, Tonga, Trinidad and Tobago, Tunisia, Turkey, Turkmenistan, Tuvalu, Uganda, Ukraine, United Arab Emirates (UAE), United Kingdom (UK), United States of America (USA), Uruguay, Uzbekistan, Vanuatu, Vatican City (Holy See), Venezuela, Vietnam, Yemen, Zambia, Zimbabwe.".Split(',').Select(s => new SelectListItem(s,s)).ToList() + + } } }; diff --git a/BTCPayServer/Forms/HtmlSelectFormProvider.cs b/BTCPayServer/Forms/HtmlSelectFormProvider.cs new file mode 100644 index 000000000..ed5ba2bed --- /dev/null +++ b/BTCPayServer/Forms/HtmlSelectFormProvider.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using BTCPayServer.Abstractions.Form; +using BTCPayServer.Validation; +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace BTCPayServer.Forms; + +public class HtmlSelectFormProvider : FormComponentProviderBase +{ + public override void Register(Dictionary typeToComponentProvider) + { + foreach (var t in new[] { + "select"}) + typeToComponentProvider.Add(t, this); + } + public override string View => "Forms/SelectElement"; + + public override void Validate(Form form, Field field) + { + if (field.Required) + { + ValidateField(field); + } + } +} + +public class SelectField: Field +{ + public List Options { get; set; } +} diff --git a/BTCPayServer/Views/Shared/Forms/SelectElement.cshtml b/BTCPayServer/Views/Shared/Forms/SelectElement.cshtml new file mode 100644 index 000000000..acd7f92a5 --- /dev/null +++ b/BTCPayServer/Views/Shared/Forms/SelectElement.cshtml @@ -0,0 +1,28 @@ +@using BTCPayServer.Forms +@using Newtonsoft.Json.Linq +@model BTCPayServer.Abstractions.Form.Field + +@{ + var selectField = (SelectField) (Model is not SelectField ? JObject.FromObject(Model).ToObject() : Model); + selectField.Options ??= new List(); + selectField.Options.ForEach(item => { item.Selected = item.Value == Model.Value; }); + + var isInvalid = ViewContext.ModelState[Model.Name]?.ValidationState is Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid; + var errors = isInvalid ? ViewContext.ModelState[Model.Name].Errors : null; +} +
+ + + + + @(isInvalid && errors.Any() ? errors.First().ErrorMessage : string.Empty) + @if (!string.IsNullOrEmpty(selectField.HelpText)) + { +
@selectField.HelpText
+ } +