Skip to content

Fix confusing error message when command is blocked by IEventValidationHandler #1933

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Framework/Framework/Runtime/Commands/EventValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using DotVVM.Framework.Binding;
using DotVVM.Framework.Binding.Expressions;
using DotVVM.Framework.Controls;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.Runtime.Commands;
using DotVVM.Framework.Hosting;

namespace DotVVM.Framework.Runtime.Commands
{
Expand All @@ -29,11 +28,13 @@ public FindBindingResult ValidateCommand(string[] path, string commandId, Dotvvm
}

// validate the command against the control
if (result.Control is IEventValidationHandler)
if (result.Control is IEventValidationHandler validationHandler)
{
if (!((IEventValidationHandler)result.Control).ValidateCommand(result.Property!))
if (!validationHandler.ValidateCommand(result.Property!))
{
throw EventValidationException(result?.ErrorMessage, result?.CandidateBindings);
var config = (viewRootControl.GetValue(Internal.RequestContextProperty) as IDotvvmRequestContext)?.Configuration;
var message = $"Execution of '{result.Binding}' was disallowed by '{result.Control.DebugString(config, multiline: false)}'.";
throw EventValidationException(message, result?.CandidateBindings);
}
}
return result;
Expand All @@ -60,7 +61,7 @@ private FindBindingResult FindCommandBinding(string[] path, string commandId,

var candidateBindings = new Dictionary<FindBindingResult.BindingMatchChecklist, CandidateBindings>();
var infoMessage = new StringBuilder();

var walker = new ControlTreeWalker(viewRootControl);
walker.ProcessControlTree((control) =>
{
Expand Down Expand Up @@ -154,7 +155,7 @@ private FindBindingResult FindCommandBinding(string[] path, string commandId,
else if (candidateBindings.All(b => !b.Key.DataContextPathMatch))
{
// nothing in the specified data context path
errorMessage = $"Invalid command invocation - Nothing was found inside DataContext '{string.Join("/", path)}'. Please check if ViewModel is populated.";
errorMessage = $"Invalid command invocation - No commands were found inside DataContext '{string.Join("/", path)}'. Please check if ViewModel is populated.";
}
else
{
Expand Down
29 changes: 29 additions & 0 deletions src/Tests/ControlTests/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.Tests.Binding;
using System;
using DotVVM.Framework.Runtime.Commands;

namespace DotVVM.Framework.Tests.ControlTests
{
Expand Down Expand Up @@ -34,6 +37,32 @@ public async Task RootViewModelIsRecord()
Assert.AreEqual("Text2Text30", (string)r.ViewModelJson["Text"]);
}

[TestMethod]
[DataRow(true, true), DataRow(true, false), DataRow(false, true), DataRow(false, false), ]
public async Task EventValidation_Button(bool visible, bool enabled)
{
var r = await cth.RunPage(typeof(TestViewModel), """
<dot:Button Text=Click
Click={command: IntProp = IntProp + 1}
Visible={value: BoolProp}
Enabled={value: StringProp == "test"} />
""");

r.ViewModelJson["BoolProp"] = visible;
r.ViewModelJson["StringProp"] = enabled ? "test" : "nope";

if (visible && enabled)
{
await r.RunCommand("IntProp = IntProp + 1");
Assert.AreEqual(1, (int)r.ViewModelJson["IntProp"]);
}
else
{
var exception = await Assert.ThrowsExceptionAsync<InvalidCommandInvocationException>(() => r.RunCommand("IntProp = IntProp + 1"));
StringAssert.Contains(exception.Message, "Execution of '{command: IntProp = IntProp + 1}' was disallowed by '<dot:Button Click={command: IntProp = IntProp + 1} Enabled={value: StringProp == \"test\"} Text=Click Visible={value: BoolProp} />");
}
}


public class ViewModel1: DotvvmViewModelBase
{
Expand Down
Loading