Skip to content

Required Field #1712

Answered by r4g-jon
wws-inc asked this question in Q&A
Sep 11, 2021 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

Hi @wws-inc,

You could use the "Hooks" in Piranha which will allow you to validate your page before it's saved.

In your Startup.cs, you can register the Hook like so:

App.Hooks.Pages.RegisterOnBeforeSave(model =>
{
if (string.IsNullOrWhitespace(model.Title)
hrow new ValidationException("The Title field must be set");
}

I've found the Hooks very useful when validating content and you can also see in the example code, you could check for field length and even use regex to validate the content. Using this method you can validate properties on the page. I also have an extension method that allows you to validate properties in regions too if you would like it.

Hope this helps!

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@biapar
Comment options

Answer selected by wws-inc
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants