Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ajit 7 posts 67 karma points
    Feb 13, 2015 @ 09:20
    Ajit
    0

    Mandatory conditional fields in Umbraco backend

    While defining page properties it is possible to make a property mandatory, before it could be published by a content editor. I need a scenario where a property is mandatory only if a checkbox is selected by content editor. For e.g. in attached screenshot SearchTags should only be mandatory if searchable checkbox is selected. 

    Is there any way of achieving this, I am using umbraco 7.21.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 13, 2015 @ 10:13
    Jan Skovgaard
    100

    Hi Ajit

    It's not something that is possible to do out of the box since you build up a document type of different property editors that are not aware of each other.

    However what you can do instead is to build your own custom property editor, which will consist of your "Searchable" and "Search tags" form fields. Then you can make sure that it's not possible to save and publish the page before at least one search tags has been selected.

    You can learn more about how to build property editors by having a look in the AngularJs Workbook https://github.com/umbraco/AngularWorkbook

    Hope this helps.

    /Jan

  • Ajit 7 posts 67 karma points
    Feb 16, 2015 @ 14:41
    Ajit
    0

    Thanks for pointing me to right direction, infact one can create quite complex editors. 

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 16, 2015 @ 19:41
    Jan Skovgaard
    0

    Hi Ajit

    You're welcome - Yes it's actually only the creativity (and skills) that sets the limit. I just created a property editor that gives me some data that I manage from a custom section. It's really nice to be able to have this kind of flexibility :)

    Happy coding!

    /Jan

  • MarcC 49 posts 356 karma points
    Jun 14, 2018 @ 10:59
    MarcC
    4

    Old thread but a solution I use is to prefix an alias 'mc_' (mandatory component) to a list of property types and do a check on saving action to see if a value is available in either of those properties.

    This allows me to add as many mandatory properties without creating custom property types.

    Code below :

        public class MandatoryComponents : ApplicationEventHandler
    {
        /// <summary>
        /// alias prefix to check against
        /// </summary>
        private const string ALIAS_PREFIX = "mc_";
    
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saving += MandatoryCheck;
        }
    
        /// <summary>
        /// Check each property type on a component we are saving for the prefix alias
        /// if at least one has a value continue.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MandatoryCheck(IContentService sender, SaveEventArgs<IContent> e)
        {
            var mandatoryPropertyList = new List<Property>();
    
            foreach(var entity in e.SavedEntities)
            {
                foreach(var prop in entity.Properties)
                {
                    if(prop.Alias.StartsWith(ALIAS_PREFIX))
                    {
                        mandatoryPropertyList.Add(prop);
                    }
                }
            }
    
            if(mandatoryPropertyList.Count < 2)
            {
                return;
            }
            else
            {
                if(mandatoryPropertyList.Any(x => x.Value != null))
                {
                    return;
                }
                else
                {
                    var properties = string.Join(", ", mandatoryPropertyList.Select(x => x.PropertyType.Name).ToArray());
                    e.Cancel = true;
                    e.Messages.Add(new EventMessage("Oops!",
                        "Please add a value to either " + properties, EventMessageType.Error));
                }
            }          
        }
    }
    

    Understand this is an older thread but this may be useful.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Sep 15, 2021 @ 14:42
    Simon Dingley
    0

    Nice little hack - thanks for sharing Marc

Please Sign in or register to post replies

Write your reply to:

Draft