Add "Required" validation option to File Upload Component

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


6 Replies

PM Ponmani Murugaiyan Syncfusion Team November 8, 2021 12:36 PM UTC

Hi Martin, 

Thanks for contacting Syncfusion support. 

Currently we are checking the reported requirement. We will update further details in 2 business days (November 10, 2021).  We appreciate your patience until then. 

Regards, 
Ponmani M 



BC Berly Christopher Syncfusion Team November 10, 2021 03:33 PM UTC

Hi Martin, 
  
Thanks for the patience. 
  
We can achieve the requested requirement with help of using Files property by adding and removing the files manually inside the EditForm as mentioned in the below code example. 
  
    <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  
    }  
    }  
 
 
  
Please find the sample from the below link. 
  
Regards, 
Berly B.C 



MA Martin November 10, 2021 10:29 PM UTC

The Files property is helpful, this does exactly what I needed.


Thanks!!!



BC Berly Christopher Syncfusion Team November 11, 2021 07:33 AM UTC

Hi Martin, 
  
Most welcome. Please let us know if you need further assistance on this. 
  
Regards, 
Berly B.C 



KB Kleber Bueno replied to Berly Christopher November 7, 2022 04:35 PM UTC

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



DM Deenadayalan Mohan Syncfusion Team November 8, 2022 12:59 PM UTC

You can validate the File Upload component without using the Files property. Refer to the below video illustration for reference.




Loader.
Up arrow icon