Flauschi
Blazor help needed. How can i Validate datalist entries in a child component
Object class:
namespace BlazorApp.Models;
public class DisplayRecipeModel
{
public string? Id { get; set; }
[Required]
public double Value { get; set; }
[Required]
public string? Category { get; set; }
public string? Payment { get; set; }
public string? Buyer { get; set; }
public DateTime PurchaseDate { get; set; }
[StringLength(20, ErrorMessage = "comment to long")]
public string? Comment { get; set; }
}
Thanks in advance
4 replies
Blazor help needed. How can i Validate datalist entries in a child component
Child:
@using BlazorApp.Models
<div class="row">
<div class="col-2">
<div>
<label>Category:</label>
</div>
<input type="search" list="categorylist" @bind-value="searchTermCat" @oninput="GetCatResult" @onfocusout="HandleReturnCatValue"/>
<datalist id="categorylist">
@foreach (var item in recipes!)
{
<option value="@item.Category" />
}
</datalist>
</div>
</div>
@code {
private string? searchTermCat = "";
private List<DisplayRecipeModel> catResult = new();
[Parameter]
public DisplayRecipeModel? recipe { get; set; }
List<DisplayRecipeModel>? recipes = new()
{
new DisplayRecipeModel
{
Category = "Fun",
Payment = "Cash"
},
new DisplayRecipeModel
{
Category = "Cloth",
Payment = "Card"
},
new DisplayRecipeModel
{
Category = "Food",
Payment = "Mastercard"
}
};
[Parameter]
public EventCallback<string> OnCategorySelected { get; set; }
private void GetCatResult()
{
if (string.IsNullOrEmpty(searchTermCat) == false)
{
recipes = catResult.Where(sq => sq.Category!.Contains(searchTermCat)).ToList();
StateHasChanged();
}
}
private void HandleReturnCatValue()
{
OnCategorySelected.InvokeAsync(searchTermCat);
StateHasChanged();
}
}
4 replies
Blazor help needed. How can i Validate datalist entries in a child component
Parent:
@page "/newentry"
@inject DataService _dataService
<h3>New Entry</h3>
<div class="container-fluid">
<div class="raw">
<div class="col-2">
<EditForm EditContext="editContext" OnValidSubmit="HandleOnSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<label>
Value:
<InputNumber @bind-Value="inputRecipe.Value"/>
<ValidationMessage For="@(() => inputRecipe.Value)" />
</label>
<ComboBoxComponent OnCategorySelected="HandleCategorySelected"></ComboBoxComponent>
<ValidationMessage For="@(() => inputRecipe.Category)" />
<label>Comment:
<InputTextArea @bind-Value="inputRecipe.Comment"/>
<ValidationMessage For="@(() => inputRecipe.Comment)" />
</label>
<button class="btn btn-outline-success" type="submit">Save</button>
</EditForm>
</div>
</div>
</div>
@code {
EditContext? editContext;
protected override void OnInitialized()
{
editContext = new(inputRecipe);
}
private string returnCatValue = "";
DisplayRecipeModel inputRecipe = new();
private void HandleOnSubmit()
{
List<DisplayRecipeModel> test = new();
inputRecipe.Category = returnCatValue;
test.Add(inputRecipe);
}
private void HandleCategorySelected(string selectedCategory)
{
returnCatValue = selectedCategory;
} }
returnCatValue = selectedCategory;
} }
4 replies