MarimerLLC / cslaforum

Discussion forum for CSLA .NET

Home Page:https://cslanet.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Correct use For attribute on CSLAValidationMessage component of CSLA Blazor

Brannos1970 opened this issue · comments

Question
@rockfordlhotka I am testing to new implementation from #1509 (MarimerLLC/csla#1509 (comment)) but I think I am doing it wrong. I wrote the following on my razor page
<CslaValidationMessage For="@vm.Model.Name" />

I get the error on code CS0411

The type arguments for method 'TypeInference.CreateCslaValidationMessages_1(RenderTreeBuilder, int, int, Expression<Func>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. Server C:\Users\Korye\Source\Repos\TifsaScheduler_Csla\Server\obj\Debug\netcoreapp3.1\Razor\Pages\AddAppointment.razor.g.cs

I tried setting the PropertyType by entering <CslaValidationMessages PropertyType=@typeof(vm.Model.Name)" For="@vm.Model.Name" /> but Csla analyzer want a ; and/or } and nothing else I enter is being accepted. I also tried to use PropertyName but it required a valid PropertyType.

Can you provide some guidance?

@TheCakeMonster is perhaps the best person to answer, as he implemented the For behavior.

That said, I'm pretty sure the answer is that you need to use an expression just like with the standard Blazor ValidationMessage component.

  <CslaValidationMessage For="() => vm.Model.Name" />

Yes, that's right. It needs to be an expression, so the leading empty brackets and implies syntax are important.

Using For requires a slightly more complex syntax, but with the complexity comes great flexibility. The new For attribute allows validation on any property throughout an entire object graph, no matter how complex that graph.

Look at the ValidationMessage example at this location, which shows a real-world example of the correct syntax

https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1

For the CSLA component I went with a plural component name, as we are definitely expecting multiple messages to be emitted by a CSLA object.

Here's the exact version that I think would work for your particular scenario:

<CslaValidationMessages For="(() => vm.Model.Name)" />

The syntax I have shown should definitely work. The same expression without the outer, enclosing brackets would probably work as well. However, when Blazor accepts expressions without the enclosing brackets is a little more complex than we might hope for, in this early version of the framework, and the opinions of Steve Sanderson and Ryan Nowak on guidance and best practice vary in this particular detail.

Thank you. I understand now and my errors have cleared up.