Get form name from form component

Sylvain C. asked on May 22, 2020 03:07

Hi,

I would need to access the name of my form from my form component in order to do a data extraction and select only records concerning this form:

My code in my customized form component is the following:

        BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("Meetings_US", SiteContext.CurrentSiteID);        

        var meetings = MeetingItemProvider.GetMeetingItems()
            .WhereContains("NodeAliasPath", formObject.FormDisplayName)
            .Select(u => new MeetingBaseDto
            {                                              
                Name = u.MeetingTitle,
                NodeAlias = u.NodeAlias
            });           

        return new MultiSelectList(meetings, "NodeAlias", "Name");

I would like to extract automatically the name of the form from the form component instead of providing a value in the code ("Meetings_US").

Thank you for your help

Sylvain

Recent Answers


Dmitry Bastron answered on May 22, 2020 10:38

Hi Sylvain,

I don't think it's doable quite easy: form field "doesn't know" what form it is rendered on. I guess this is a continuation of your previous question. So the easiest way of doing so would be for your "meeting selection" form control just add "event code" form control property. So that when admin adds "meeting selection" form control to a form he can just specify event code explicitly rather than reading it from the form name automatically.

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on May 22, 2020 21:02

Thank you Dmitry for your time,

Do you mean add a hidden input in which I store the event code and the "meting selection" form component will then pick up this value to generate the list? Or adding a new control in my form component where I specify the event code? I am kind of struggling to know the best approach and what is feasible or not and to find some code example to move forward...

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on May 22, 2020 22:00 (last edited on May 22, 2020 22:00)

Dmitry, Following your latest links, I was able to make it happen. It is probably not best practise but it works.

What I did: I created a new property in my meeting list form control in the properties class:

[EditingComponent(TextInputComponent.IDENTIFIER, ExplanationText = "Please enter the event code", Label = "Event code")]
[Required]
public string EventCode { get; set; }

In the component class, i did:

public MultiSelectList GetAllMeetings(string code)
    {    

        var meetings = MeetingItemProvider.GetMeetingItems()
            .WhereContains("NodeAliasPath", code)
            .Select(u => new MeetingBaseDto
            {                                              
                Name = u.MeetingTitle,
                NodeAlias = u.NodeAlias
            });           

        return new MultiSelectList(meetings, "NodeAlias", "Name");
    }

And then in the view, I did:

@{
    var htmlAttributes = ViewData.GetEditorHtmlAttributes();    
    var code = Model.Properties.EventCode;
}

@Html.ListBoxFor(x => x.SelectedCategories, Model.GetAllMeetings(code), htmlAttributes)

In you have some advices please let me know. Hope it will useful to someone else.

Sylvain

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 25, 2020 13:58

Yes, this is exactly what I meant.

The only way how to improve it further a little bit I see is developing another form control to select event code. So I assume you have events stored in Kentico in a sort of page type or custom class. If so, you can create "Event selector" form component (pulling all available events automatically) that will be used in your "Meeting selector" form component like this:

[EditingComponent("Your.Custom.Event.Selector", ExplanationText = "Please select the event", Label = "Event")]
[Required]
public string EventCode { get; set; }

After that you content admins creating these forms wouldn't need to remember or copy event codes but would just need to select it from available list.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.