Skip to content

Blazor form validation of a file field #18821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Stamo-Gochev opened this issue Feb 5, 2020 · 5 comments
Closed

Blazor form validation of a file field #18821

Stamo-Gochev opened this issue Feb 5, 2020 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components
Milestone

Comments

@Stamo-Gochev
Copy link

Stamo-Gochev commented Feb 5, 2020

What is the expected way of handling validation of a form field that is of type <input type="file">? Suppose that the form is bound to a model that has such a field and the form should display a validation message, when certain criteria is nto met, e.g. the file size exceeds a limit or the file has an invalid extension. Furthermore, what is the expected type of the model field that corresponds to <input type="file"> as things like IFormFIle


<EditForm
          OnSubmit="@HandleSubmit"
          EditContext="@_editContext">
    <DataAnnotationsValidator />
    <ValidationSummary />

	<p>
		<input type="file"  @bind-value="_starship.File" />
	</p>

	<button type="submit">Submit</button>
</EditForm>


@code {
	private Starship _starship = new Starship();

	private EditContext _editContext;

	protected override void OnInitialized()
	{
		_editContext = new EditContext(_starship);
	}


	private async Task HandleSubmit()
	{
		...
	}


	public class Starship
	{          
		// custom validation attribute
		[AllowedExtensionsAttribute(new string[] { ".txt" })]
		public IFormFile File { get; set; }
	}
}

fail with an exception (this is expected, but what should be the correct type):

The type 'Microsoft.AspNetCore.Http.IFormFile' does not have an 
associated TypeConverter that supports conversion from a string. 
Apply 'TypeConverterAttribute' to the type to register a converter.

Currently, there isn't a built in <InputFile> component and the blog post from @SteveSandersonMS doesn't mention form validation. In addition, will there be any built-in support for anti-forgery tokens as it is the case with MVC forms or should this be manually handled by the developer?

Will there be a difference whether wasm or server-side blazor is used?

Additional context

Building a custom <InputFile> component requires some type of validation when used in a form. Using a fully custom validation, e.g. manually displaying a message is possible, but this does not work with validation attributes at all.

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Feb 5, 2020
@pranavkm
Copy link
Contributor

pranavkm commented Feb 5, 2020

Hi @Stamo-Gochev, IFormFile is not not part of the Blazor programming model and is not supported in the context of a Blazor component. You'll have to use JSInterop to read the file extension and apply your own validation.

@pranavkm pranavkm added this to the Discussions milestone Feb 5, 2020
@mrpmorris
Copy link

The OnChange event of InputFile will give you an object as a parameter that will give you access to the filename etc.

@Stamo-Gochev
Copy link
Author

@mrpmorris My question was more about "built-in" support for validation attributes in model fields. Metadata about the file can be obtained manually.

@javiercn
Copy link
Member

javiercn commented Oct 9, 2020

There a new InputFile component in 5.0 that is recommended for use in this case

@javiercn javiercn closed this as completed Oct 9, 2020
@keraxel
Copy link

keraxel commented Oct 10, 2020

@javiercn Is there a way to apply validation attributes on InputFile? The component does not seem handle data binding.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

5 participants