question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Data Annotations in Nested Data Models not validating

See original GitHub issue

Hi there I’m currently stucked with a problem I don’t understand why it’s happening. We’re using the Cofoundry Web Admin 0.6.1 NuGet Package which has been published on April 13th 2019.

The following is a code snippet from our nested DataModel of a product slider we’re building for a book webshop.

public class ProductPickerSlideDataModel : INestedDataModel
{
	[PreviewTitle]
	[Required(ErrorMessageResourceName = "Required_ISBN", ErrorMessageResourceType = typeof(Localization.Web.CMS.Global))]
	[RegularExpression(@"^(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$", ErrorMessageResourceName = "MalformedISBN", ErrorMessageResourceType = typeof(Localization.Web.CMS.ProductPicker))]
	[Display(Name = "Suche", Description = "Geben Sie eine ISBN oder eine ISSN ein, um nach einem Titel zu suchen.")]
	public string Query { get; set; }
}

Currently the Required annotation works partially here, as the field is marked as required correctly and the message “This field is required” does appear if I’d leave it blank. But in fact, it should show another message defined in our localization resource. The RegularExpression annotation doesn’t work at all, so I could enter “Hitchhiker’s” instead of an ISBN and it gets recognized as valid while in fact it’s not. Does anyone know some help here? What am I doin’ wrong?

Kind regards and thanks in advance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rene-a-dippecommented, Aug 27, 2019

@HeyJoel I’m not a backend developer so I might not be very helpful here but as mentioned it already works in Cofoundry’s IPageBlockTypeDataModel. Maybe this helps to figure out how to replicate the correct behavior 👍

Lookin’ forward to the fix, good luck and thanks for your help!

0reactions
HeyJoelcommented, Dec 30, 2020

Server-side validation now runs before adding/updating a nested data model. This will be released in 0.8.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Model inside Model not validating using data annotations
If i pass in a model with a description that has 101+ characters the validation works and the results collection has the validation...
Read more >
Validating nested DataAnnotation IOptions recursively with ...
In this short post I show how you can use the MiniValidation library from Damian Edwards to support recursive validation of IOptions object...
Read more >
How do I validate a nested complex model in Blazor?
To validate the nested complex model, replace the DataAnnotationsValidator with the ObjectGraphDataAnnotationsValidator, which validates the entire object, ...
Read more >
[Extensions.Options] ValidateDataAnnotation doesn't work ...
When using ValidateDataAnnotations to validate a configuration hierarchy, you can only validate top level members of the class. Any properties ...
Read more >
Data Annotations Not Working when Model Binding to a ...
I have a model binder which effectively says: var modelType = Type.GetType("Foo,Bar"); var model = Activator.CreateInstance(modelType) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found