Hiding required properties on the create new page in Episerver

Tags: Episerver Episerver Edit Mode Jul 27, 2017

By default Episerver will show all required properties on the create new screen. I guess that it's presumed that the content cannot be created without these properties existing which in many cases is true (e.g. setting a list root). However in many cases I've seen the [Required] attribute used where the content could be created/edited without the property existing but we need to stop the content being published without it (see my note on using IValidator's later in this post).

Overusing the [Required] attribute on content types can make the editor face a form to fill in even before they think about creating some content. This can interrupt the editor flow as they may not know how/where the property is going to be used on the content they are creating.

This post describes how it's possible to hide required properties on the create new screen but ensure they are still required when trying to publish content.

The main of the work is done with an Episerver IMetaDataExtender. The example below changes the content model and marks properties to be not required if they are decorated with the [HideOnContentCreate] attribute:

The attribute itself is just a marker we can use on our model:

Finally we register our meta data extender with an Episerver initialisation module:

We can now mark content models with our new [HideOnContentCreateAttribute] attribute but use the standard [Required] attribute too:

The final result

This stops a property being shown on the create new screen but the [Required] attribute is still respected when editing content:

Other approaches

An alternative approach could be to use an IValidator. This can create the same effect for the editor. However creating a separate IValidator every time you create a model could interrupt developer flow and also adds more code to your solution. Neither choice is right or wrong!

I wanted to mention Grzegorz Wiecheć for his Show additional properties on content creation post as this inspired me to create a "reverse logic" version.

Conclusion

This post shows how it's possible to mark a property as required but not force an editor to enter it even before thinking about the content they are creating. Have you encountered this requirement yourself? How have you solved similar requirements? I'd be happy to discuss in the comments below or on @davidknipe.

comments powered by Disqus