Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to add default values to soy forms in a Stash add-on?

Alexandre Andrade May 21, 2013

I'm creating a hook for Stash, it has a form editor to set an URL. I want to know if it's possible to give a textField a default value, so that it doesn't have to be configured everytime.

These are the contents of the soy file:

{namespace stash.config.example.hook.simple}

{template .formContents}
    {call aui.form.textField}
        {param id: 'url' /}
        {param value: $config['url'] /}
        {param labelContent}
            {stash_i18n('stash.web.test.hook.config.label', 'URL')}
        {/param}
        {param descriptionText: stash_i18n('stash.web.test.hook.config.description', 'URL to be notified.') /}
        {param extraClasses: 'long' /}
        {param errorTexts: $errors ? $errors['url'] : null /}
    {/call}
{/template}

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 22, 2013

Hi Alexandre,

{param value: $config['url'] ? $config['url'] : 'Default value'  /}

You could set the value of the textField if 'url' hasn't already been set.

Cheers,

Charles

Karl Ostmo September 14, 2013

What technique should we use to make checkboxes checked by default?

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 14, 2013

Hi Karl,

{call aui.form.checkboxField}
    {param fields: [
        ['id': 'test', 'labelText': 'Hello world', 'isChecked': true]
     ]/}
{/call}

While I know this isn't ideal, I recommend having a look at the form.soy from AUI for the full list of parameters:

https://bitbucket.org/atlassian/aui/src/master/auiplugin/src/main/resources/soy/atlassian/form.soy

You might find some of the comments/examples/links on this Answer useful too:

https://answers.atlassian.com/questions/194459/how-do-i-use-soy-with-stash-to-make-useful-configuration-pages

Cheers,

Charles

Karl Ostmo September 14, 2013

Charles,

That make the checkbox always checked, even if the user un-checks it and saves. When the configuration dialog is opened again, the box is re-checked.

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 14, 2013

Karl,

Sorry, I wasn't thinking.

{call aui.form.checkboxField}
    {param fields: [
        ['id': 'test', 'labelText': 'Hello world', 'isChecked': $config['test']]
     ]/}
{/call}

Or something along those lines. Does that work?

Charels

Karl Ostmo September 17, 2013

That allows the user to set the value, but it doesn't make the checkbox checked by default.

Karl Ostmo September 22, 2013

I guess that I could work around this by inverting the semantics of my checkbox, such that the sensible default is unchecked.

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2013

Hi Karl,

Apologies again. So what about this (actually tested)?

{call aui.form.checkboxField}
    {param fields: [
        ['id': 'test', 'labelText': 'Hello world', 'isChecked': $config['test'] != null ? $config['test'] : true]
     ]/}
{/call}

Charles

Olalekan Omotayo August 17, 2014

Hi Karl,

I have this in my simple.soy file for a stash hook:

{namespace com.atlassian.tutorial.helloworld}
/**
 * @param config
 * @param? errors
 */
{template .formContents}
	{call aui.form.checkboxField}
        {param fields: [
        ['id': 'test', 'labelText': 'Hello world', 'isChecked': $config['test'] != null ? $config['test'] : true] 
        ]/}
    {/call}
{/template}

However when I try to test the hook, I get the following error:

Failed to load config form: com.atlassian.tutorial.helloworld:hello-world-pre-receive-repository-hook-config-form, errors: TypeError: Cannot read property 'length' of undefined

Any ideas why I am getting this error?

Lekan.

0 votes
Karl Ostmo September 14, 2013

What technique should we use to make checkboxes checked by default?

Geoff Williams June 23, 2019

Appreciate this is a late response but I've been stuck on this for a while too.

The problem is that checkboxes are not contained in $config when they are set to false (same as HTML forms) and soy will coerce missing values to false. I found this with the soy debugger[1].

My workaround for this is to detect when the $config map is empty and then use the default value, otherwise coerce the value from where the checkbox data _should_ be in the map. This works because soy expressions are truthy and don't error on missing/null values[2]:

Here's a complete example:

{call aui.form.checkboxField}
{param legendContent: 'Wait' /}
{param fields: [[
'id' : 'wait',
'labelText' : 'Wait for deployment to finish when pushing',
'isChecked': (length(keys($config)) == 0) ? true: $config['wait']
]] /}
{/call}

 

HTH

---

[1] https://github.com/google/closure-templates/blob/master/documentation/reference/debugger.md

[2] https://github.com/google/closure-templates/blob/master/documentation/reference/expressions.md#logical-operators-and-or-logical-operators

TAGS
AUG Leaders

Atlassian Community Events