Mvc: `[Validation]` attributes do not work for page model properties or page handler parameters

Created on 13 Feb 2018  路  15Comments  路  Source: aspnet/Mvc

For example, a page handler such as
``` c#
public IActionResult OnGet([FromHeader, Required] string hosta)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

// ...
return Page();

}
`` should always return aBadRequestResultbecause thehostaheader doesn't exist. Instead, theModelState` is valid and everything proceeds.

Notes: [Validation] attributes do work as expected for action parameters. The above problem applies to all [Validation] attributes, not just [Required]. And, the [Validation] attributes are ignored with or without additional attributes; [FromHeader] doesn't matter.

3 - Done bug

All 15 comments

I assume this is just a feature gap, where Razor Pages don't follow the same model. @danroth27 , is this something we should build up to work similar to mvc actions?

I would think so, yes.

@NTaylorMullen, can you please confirm that we indeed don't have this area covered and that this is not a bug.

Updating title because validation also produces no errors when binding a page model like the following and nothing is submitted:
c# public class IndexModel : PageModel { [BindProperty] [Required] public string Required { get; set; } }
I've tried a couple of [Validation] attributes and property types. The results are the same.

Investigated and talked with folks. Here's what I found:

  • It's an oversight that public IActionResult OnGet([FromHeader, Required] string hosta) does not change ModelState.IsValid when not provided.
  • It's a bug that
    C# [BindProperty(SupportsGet = true), Required] public string Anything { get; set; } // On the page model
    doesn't result in ModelState.IsValid being false when not provided.

@pranavkm mentioned that he's going to start working on a fix for the bug.

Hi,

Which release will include these fixes?

@sven5 the fix will be included in 2.1.0 Preview 2.

Hi,

I'm currently testing my Validation issue with preview 2.
It seems that MinLengthAttribute doesn't work.

My property is defined like this:
c# [BindProperty] [Required] [MinLength(1, ErrorMessage = "MinLengthAttribute_ValidationError")] public int[] SelectedStores { get; set; }

It doesn't result in ModelState.IsValid being false when SelectedStores.Count() == 0

Did I miss something?

Regards
Sven

@sven5 - did you set the compatibility version to 2.1? https://github.com/aspnet/Mvc/blob/dev/samples/MvcSandbox/Startup.cs#L17

New features like this one in particular introduce lots of subtle behavior changes.

@rynowak

I missed that. Now it's working. Thank you.

There still is something strange:
The ModelState.Values Enumerable now contains my invalid property but the Key and SubKey are empty. I suspect that the consequence of this is that validation error for the field is not showing with

<span asp-validation-for="SelectedStores" class="text-danger"></span>

The validation error is displayed only in validation summary.

Regards
Sven

@dougbu @pranavkm does this sound like https://github.com/aspnet/Mvc/issues/7503?

@sven5 what exactly is in the request when the error ends up with an empty ModelState key?

@rynowak maybe.

I'm reactivating this since there is something to figure out :)

Hi folks,

I figured it out now. It was a mistake on my side. Sorry for false alarm.
I had an issue with an array of hidden fields. The ModelState Value entry has been empty because the field was missing on post back.

Regards
Sven

Ok, great thanks for lettings us know. Closing this

Was this page helpful?
0 / 5 - 0 ratings