From 1f5a1f4fe2c9158b90b3b59df628372e605f1469 Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Sun, 15 Jan 2023 10:04:03 +0100 Subject: [PATCH] added input validation for poll creation --- Lieb/Models/Poll/Poll.cs | 7 ++++++- Lieb/Models/Poll/PollOption.cs | 7 ++++++- Lieb/Pages/Poll/PollCreate.razor | 28 +++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Lieb/Models/Poll/Poll.cs b/Lieb/Models/Poll/Poll.cs index ae02e31..e4d83af 100644 --- a/Lieb/Models/Poll/Poll.cs +++ b/Lieb/Models/Poll/Poll.cs @@ -1,4 +1,6 @@ -namespace Lieb.Models.Poll +using System.ComponentModel.DataAnnotations; + +namespace Lieb.Models.Poll { public enum AnswerType { @@ -8,6 +10,9 @@ public class Poll { public int PollId { get; set; } + + [Required] + [StringLength(200, ErrorMessage = "Question too long (200 character limit).")] public string Question { get; set; } public ICollection Options { get; set; } = new List(); public ICollection Answers { get; set; } = new List(); diff --git a/Lieb/Models/Poll/PollOption.cs b/Lieb/Models/Poll/PollOption.cs index 5402f4d..f1af2f6 100644 --- a/Lieb/Models/Poll/PollOption.cs +++ b/Lieb/Models/Poll/PollOption.cs @@ -1,8 +1,13 @@ -namespace Lieb.Models.Poll +using System.ComponentModel.DataAnnotations; + +namespace Lieb.Models.Poll { public class PollOption { public int PollOptionId { get; set; } + + [Required] + [StringLength(100, ErrorMessage = "Option too long (100 character limit).")] public string Name { get; set;} = string.Empty; } } diff --git a/Lieb/Pages/Poll/PollCreate.razor b/Lieb/Pages/Poll/PollCreate.razor index c483d81..c21894d 100644 --- a/Lieb/Pages/Poll/PollCreate.razor +++ b/Lieb/Pages/Poll/PollCreate.razor @@ -40,7 +40,10 @@ Poll Type: - + @if(_availabeGroups.Count > 0) + { + + }

@@ -149,7 +152,11 @@ { _availabeGroups = UserService.GetUserRoles(_user.Id).Where(u => u.Type == RoleType.UserDefinedRole).ToList(); } - _chosenRole = _availabeGroups.First().LiebRoleId; + _poll.AnswerType = AnswerType.Dropdown; + if(_availabeGroups.Count > 0) + { + _chosenRole = _availabeGroups.First().LiebRoleId; + } } async Task AddOptionClicked() @@ -165,6 +172,21 @@ private async Task HandleValidSubmit() { + if(_poll.Options.Count < 2) + { + _errorMessage = "At least 2 options are needed for a Poll."; + return; + } + + foreach(PollOption option in _poll.Options) + { + if(string.IsNullOrWhiteSpace(option.Name)) + { + _errorMessage = "Options must not be empty."; + return; + } + } + if(_pollType == POLL_TYPE_RAID) { await PollService.CreatePoll(_poll, _chosenRaid); @@ -174,6 +196,6 @@ await PollService.CreatePoll(_poll, UserService.GetGroupMembers(_chosenRole).Select(u => u.Id).ToList()); } - NavigationManager.NavigateTo("raidoverview"); + NavigationManager.NavigateTo("polloverview"); } } \ No newline at end of file