added input validation for poll creation

This commit is contained in:
Sarah Faey 2023-01-15 10:04:03 +01:00
parent 2a69ccc1b6
commit 1f5a1f4fe2
3 changed files with 37 additions and 5 deletions

View file

@ -1,4 +1,6 @@
namespace Lieb.Models.Poll using System.ComponentModel.DataAnnotations;
namespace Lieb.Models.Poll
{ {
public enum AnswerType public enum AnswerType
{ {
@ -8,6 +10,9 @@
public class Poll public class Poll
{ {
public int PollId { get; set; } public int PollId { get; set; }
[Required]
[StringLength(200, ErrorMessage = "Question too long (200 character limit).")]
public string Question { get; set; } public string Question { get; set; }
public ICollection<PollOption> Options { get; set; } = new List<PollOption>(); public ICollection<PollOption> Options { get; set; } = new List<PollOption>();
public ICollection<PollAnswer> Answers { get; set; } = new List<PollAnswer>(); public ICollection<PollAnswer> Answers { get; set; } = new List<PollAnswer>();

View file

@ -1,8 +1,13 @@
namespace Lieb.Models.Poll using System.ComponentModel.DataAnnotations;
namespace Lieb.Models.Poll
{ {
public class PollOption public class PollOption
{ {
public int PollOptionId { get; set; } public int PollOptionId { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Option too long (100 character limit).")]
public string Name { get; set;} = string.Empty; public string Name { get; set;} = string.Empty;
} }
} }

View file

@ -40,7 +40,10 @@
Poll Type: Poll Type:
<InputSelect @bind-Value="_pollType"> <InputSelect @bind-Value="_pollType">
<option value="@POLL_TYPE_RAID">Raid</option> <option value="@POLL_TYPE_RAID">Raid</option>
<option value="@POLL_TYPE_GROUP">Group</option> @if(_availabeGroups.Count > 0)
{
<option value="@POLL_TYPE_GROUP">Group</option>
}
</InputSelect> </InputSelect>
</label> </label>
</p> </p>
@ -149,7 +152,11 @@
{ {
_availabeGroups = UserService.GetUserRoles(_user.Id).Where(u => u.Type == RoleType.UserDefinedRole).ToList(); _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() async Task AddOptionClicked()
@ -165,6 +172,21 @@
private async Task HandleValidSubmit() 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) if(_pollType == POLL_TYPE_RAID)
{ {
await PollService.CreatePoll(_poll, _chosenRaid); await PollService.CreatePoll(_poll, _chosenRaid);
@ -174,6 +196,6 @@
await PollService.CreatePoll(_poll, UserService.GetGroupMembers(_chosenRole).Select(u => u.Id).ToList()); await PollService.CreatePoll(_poll, UserService.GetGroupMembers(_chosenRole).Select(u => u.Id).ToList());
} }
NavigationManager.NavigateTo("raidoverview"); NavigationManager.NavigateTo("polloverview");
} }
} }