1

I have a schema which is used in multiple components in Tridion 2013. This schema has one mandatory field. I want to bypass the tridion mandatory field check for one particular component without impacting other components mandatory check created using the same schema. Is it possible using eventing or through any other way?

  • If it only one component, put some default/temp value.. Why worry so much? – vikas kumar Apr 29 '15 at 20:09
2

This type of validation is done in the core, and there is no way around it. You must define a value in that field (maybe by default?) if the field is set as mandatory.

You could use an Event indeed, on load of the component, that figures out if in this context the field is OK to be empty and enter a value like '[leave empty]' and then strip this value off when publishing. However, in the CM, there's no way to bypass this rule.

  • Can I subscribe to multiple events in a single dll like the below code? – manish Apr 29 '15 at 21:20
2

To add to Nuno's answer, I would consider it the other way around. Don't make the field mandatory in the Schema, but build an event system on save of the Components and check if the field should be mandatory in this case and add your own check.

If you subscribe to for instance the processed phase:

EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSave, EventPhases.Processed);

Any errors that you will raise, will be shows directly to the user, so you can use something like:

throw new TridionArgumentException("You have to fill field xyz.");
0

I believe this would be code to start with - please confirm

using System;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;

namespace TridionEventSystem
{
    [TcmExtension("Tridion2013EventSystem")]  
    public class EventSystem : TcmExtension
    {
        public EventSystem()
        {
            Subscribe();
        }

        public void Subscribe()
        {
            EventSystem.Subscribe<Component, SaveEventArgs>
(OnComponentSavePre, EventPhases.Initiated);

            EventSystem.Subscribe<Component, PublishEventArgs>(OnComponentPublishPre, EventPhases.Initiated);
        }

private static void OnComponentSavePre(Component comp, SaveEventArgs args, EventPhases phase)
        {
            if (component.Title.Equals("MyComp"))
            {
                comp.Field = "[leave empty]";
            }   
        }

private static void OnComponentPublishPre(Component component, PublishEventArgs args, EventPhases phase)
        {
            if (component.Title.Equals("MyComp"))
            {
                comp.Field = "";
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.