Custom Property Validation
# help-with-umbraco
m
Hey, has anyone ever used the Content Saving notification to add a custom "mandatory field" validation message, with it showing in the backoffice like any other validation message. The use case is that I have a base composition that is used in multiple places, but i need to make on of the properties mandatory only for a few of them. It's too late to change the existing set up without a bunch of data loss, hence the custom approach. The error notification itself is pretty simple, it's the linking it in with the backoffice UI that i'm not sure about.
r
this is the kind of thing I think we need to start thinking about for the new UI and the exisiting ui so its "easy" to repeat.. alo I don't know the answer
j
I've not done it, but I'd try using the
SendingContentNotification
to adjust the property's validation settings as it gets loaded.
Something like this?
Copy code
csharp

public class EditorSendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
{
    public void Handle(SendingContentNotification notification)
    {
        // something to determine if this is content you want to modify...
        if (notification.Content.ContentTypeAlias.Equals("blogpost"))
        {
            foreach (var variant in notification.Content.Variants)
            {
                var shouldBeManadatory = variant.Tabs.SelectMany(f => f.Properties)
                    .FirstOrDefault(f => f.Alias.InvariantEquals("propertyAlias"));
                if (shouldBeManadatory is not null)
                {
                    shouldBeManadatory.Validation.Mandatory = true;
                }
            }
        }
    }
}
m
THanks dude, i'll give it a go 🙂
Worked like a charm! I had managed to completely miss that notification type, thanks mate!
r
There is a pull request waiting to happen if that spelling mistake is true..😛
a
It's not from the docs, sadly, but I guess you need some mana to check if its manadatory 😛
2 Views