2

I want to set a General Field (Simple Text Field) on a Component to specific word (ex. "aaa") when the Component is saved, by using Event System.

I success to do the above, but I have one problem.

If the General Field is not Mandatory, it's no problem.

But if the General Field is Mandatory, it's problem, that is, the component isn't saved because of displaying error message "Mandatory field "xxx" is missing a value".

One solution is I set the General Field to not Mandatory.

But I want to set the General Field to Mandatory.

So please tell me a solution, that is,

how to set a General Field to specific word before SDL Tridion check whether the General Field is missing a value or not, using event system.

UPDATE2

the below code is my code.

    private void Subscribe()
    {
        EventSubscription subScription = EventSystem.Subscribe<Component, SaveEventArgs>(copys, EventPhases.Initiated);
        _subScriptions.Add(subScription);
    }

    private void copys(Component component, SaveEventArgs args, EventPhases phases)
    {
        if (!component.Schema.Title.Equals("copy_test")) return;

        ItemFields generalFields = new ItemFields(component.Content, component.Schema);

        foreach (ItemField child in generalFields) 
        {
            if (child.Name=="copy") 
            {
                TextField text = (TextField)child;
                text.Value = "set the value";
            }
        }
        component.Content = generalFields.ToXml();
    }
  • What event phase are you listening to? Initiated? – robrtc Jun 17 '14 at 6:56
  • While I do prefer some defaults for authors, I see the Event System as just one technical solution to the requirement. In addition to defaults on schema fields (or a default keyword that you can change), you could consider copy and paste, Experience Manager Page Types, or Custom Content Types. The idea would make the set of options ("aaa") easily changed, or even changed after-the-fact. Is the field really "aaa?" :-) – Alvin Reyes Jun 17 '14 at 16:18
  • >robrtc.. Thank you answer my question. Event phase is Initiated and SaveEventArgs. – Arai Yuhei Jun 18 '14 at 2:39
  • >Alvin Reyes... Thank you answer my question. the above specific word is a example. Many simple text field are set automatically. And I confirm whether this can be or not by Event System.(Especially, the simple text field is mandatory.) – Arai Yuhei Jun 18 '14 at 2:44
  • 1
    Default value in schema? – Nuno Linhares Jun 18 '14 at 9:52
9

It sounds like you are hooking into the Processed (or later) phase of the Save operation, which will only be called after the save has happened.

You should instead hook into the Initiated phase of the save. This one occurs before the save and so giving the field a value will satisfy the mandatory requirement of the field when the save does occur.

Update: I feel kind of silly for not noticing this before, but this will never work in the CME, if the field is mandatory. It's not a problem with your event handler: the CME also does client-side validation and that is what is preventing your save in the first place.

|improve this answer|||||
  • Thank you answer to my question. I hook the Initiated phase of the save (component). But display the above error message. – Arai Yuhei Jun 18 '14 at 2:25
  • I don't think you're doing it right, then, if you are still getting an error about the field missing a value. Either it's a different field, a different casing of the letters, or you're not setting the value correctly. You'll need to post your actual code for us to help further then. – Peter Kjaer Jun 18 '14 at 8:46
  • Thank you answer to my question. I update, I show a part of my code. the EventPhases is initiated. – Arai Yuhei Jun 18 '14 at 9:32
  • That's not enough to help you; we need the code that actually sets the field value too. – Peter Kjaer Jun 18 '14 at 11:19
  • Thank you answer to my question. I update, I show my code. – Arai Yuhei Jun 19 '14 at 2:40
7

Perhaps you can do this through a GUI extension - and thus better inform a user that a value was entered. I presume you're aware that fields can have 'default' values already - which may negate your code even running in many instances?

The best way to approach this (event system, GUI extensions, user process/education) will likely depend on the specifics of 'why' you want populate something, just a couple of quick questions that spring to mind are - are you writing the value irrespective of the user entry (if so, then perhaps this should be a read-only field [read a.n.other GUI extension]) - are you wanting to inform the user they entered something invalid / null

Rob has a nice example of this here and there's a good discussion of the different options from Nuno

|improve this answer|||||
  • Thank you answer to my question. I want to set a Simple Text Field on a Component to specific word (ex. "abc") only. If the text field is not Mandatory, I know that I can do by Event System. Even if the Text Field is not read-only field, Event System set a specific word. – Arai Yuhei Jun 18 '14 at 2:37
2

Though I am not sure, but I doubt the objects (Components etc.) will restore their values across events. I suspect in the code above, you may need to call the Component.Save method explicitly once you change the content value of the component - After this statement:

component.Content = generalFields.ToXml();

try putting the

component.Save();

and check.

|improve this answer|||||
  • Thank you answer my question. I do the above . But I don't solve the problem. – Arai Yuhei Jun 20 '14 at 0:38

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.