Default field values in Forms

Paul Truman asked on March 11, 2024 12:55

Hey, previously in Kentico 12, I would use a macro in the default value of a form to capture the document culture. How can I do this in Kentico 13 using the forms widget? It doesn't seem to support it anymore.

Recent Answers


Liam Goldfinch answered on March 11, 2024 15:12 (last edited on March 11, 2024 15:13)

I would approach this slightly differently.

I would create a custom form component called "Culture Code" or something.

In the view for this form component, I would either render a hidden field (to store the value only) or a text box if you want to display to the visitor.

Then in the C# code you could override the GetValue method to grab the document culture from the current page (maybe by injecting IPageDataContextRetriever):

private readonly IPageDataContextRetriever _pageDataRetriever;

public CultureCodeFieldFormComponent(IPageDataContextRetriever pageDataRetriever)
{
    _pageDataRetriever = pageDataRetriever;
}

public override string GetValue()
{
    var currentPage = _pageDataRetriever.Retrieve<TreeNode>().Page;
    return currentPage.DocumentCulture;
}

Then when you add this to a Form, it should use the page context to drive the value of the field.

0 votesVote for this answer Mark as a Correct answer

Paul Truman answered on March 11, 2024 18:23

Hmm, that doesn't seem to work unfortunately, I keep getting Missing page context data errors. I've tried persisting the page context in my view, but to no success unfortunately.

0 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on March 11, 2024 23:11

I usually try to avoid using it, but you could try using LocalizationContext.CurrentCulture.CultureCode in the GetValue method?

0 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on March 11, 2024 23:15

Or try grab from the current thread Thread.CurrentThread.CurrentCulture.Name

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on March 12, 2024 05:49

Or, how are you distinguishing the culture? By domain or language prefix in the URL? The idea is using global event handler in the live site app and catch the form insert event - e.g. the before insert handler and you should be able to see the request URL and based on that save a value into the field.

0 votesVote for this answer Mark as a Correct answer

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