I have a feature request, is it possible to add a "Required" validation option for the Blazor File Upload component? That way, a user can know visually when they try to submit a form and the required file input is empty
<EditForm Model="@employee" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
<DataAnnotationsValidator />
<div class="form-group">
<SfTextBox @bind-Value="@employee.EmpID"></SfTextBox>
<ValidationMessage For="() => employee.EmpID" />
</div>
<div class="form-group">
<SfUploader @ref="UploadObj" Multiple=false AutoUpload="false" ID="UploadFiles" Files="@employee.files">
<UploaderEvents ValueChange="OnChange" OnRemove="OnRemove" FileSelected="OnSelect"></UploaderEvents>
</SfUploader>
<ValidationMessage For="() => employee.files" />
</div>
<button type="submit" class="btn btn-primary">Register</button>
</EditForm>
@code{
SfUploader UploadObj;
public class Employee
{
[Required(ErrorMessage ="Please enter your name")]
public string EmpID { get; set; }
[Required(ErrorMessage = "Please upload a file")]
public List<UploaderUploadedFiles> files { get; set; }
}
public Employee employee = new Employee();
public void OnSelect(SelectedEventArgs args)
{
this.employee.files = new List<UploaderUploadedFiles>() { new UploaderUploadedFiles() { Name = args.FilesData[0].Name, Type = args.FilesData[0].Type, Size = args.FilesData[0].Size } };
}
public void OnChange(UploadChangeEventArgs args)
{
foreach (var file in args.Files)
{
var path = @"D:\" + file.FileInfo.Name;
FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);
file.Stream.WriteTo(filestream);
filestream.Close();
file.Stream.Close();
}
}
public void OnRemove()
{
this.employee.files = null;
}
public async Task HandleValidSubmit()
{
await this.UploadObj.Upload(); // Upload the selected file manually
}
public async Task HandleInvalidSubmit()
{
await this.UploadObj.Upload(); // Upload the selected file manually
}
}
|
The Files property is helpful, this does exactly what I needed.
Thanks!!!
What happened to property Files for SfUploader.
That was exactly I needed for validation.
I am using now Syncfusion.Blazer version 20.2.0.48, but there is no property Files.
Is there any other way to validate SfUploader?
thanks
Kleber
You can validate the File Upload component without using the Files property. Refer to the below video illustration for reference.