Discussion:
Problem with select list custom field.
forums-YrDk4eUcT3KaMJb+
2010-01-22 16:32:35 UTC
Permalink
Hello!
I am new to jira.
I've got the problem with adding options to my custom field which represented by select list.
As far as I know I need just override getVelocityParameters in my class and tune edit-select.vm
Now I've got enabled plugin but don't see any options except "NONE".

What I like to be answered is:

Which approach should I use to populate the options?

How do I implement a SelectCF properly?

* which classes should I extend or implement?
* what else do I have to know?



Please help me!


That's my class
{code}
package com.atlassian.jira.plugin.customfield.impl;

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.customfields.converters.SelectConverter;
import com.atlassian.jira.issue.customfields.converters.StringConverter;
import com.atlassian.jira.issue.customfields.impl.FieldValidationException;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.customfields.persistence.PersistenceFieldType;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.customfields.impl.SelectCFType;
import com.atlassian.jira.issue.customfields.SortableCustomField;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import org.apache.log4j.Category;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;



public class BoolCFType extends SelectCFType implements SortableCustomField<String>
{
private static final PersistenceFieldType PERSISTENCE_TYPE = PersistenceFieldType.TYPE_LIMITED_TEXT;

private final SelectConverter selectConverter;
private final OptionsManager optionsManager;

public BoolCFType(CustomFieldValuePersister customFieldValuePersister, StringConverter stringConverter,
SelectConverter selectConverter, OptionsManager optionsManager, GenericConfigManager genericConfigManager)
{
super(customFieldValuePersister, stringConverter, selectConverter, optionsManager, genericConfigManager);
this.selectConverter = selectConverter;
this.optionsManager = optionsManager;
}

public String getStringFromSingularObject(Object singularObject)
{
assertObjectImplementsType(String.class, singularObject);
return selectConverter.getString(singularObject);
}

public Object getSingularObjectFromString(String string) throws FieldValidationException
{
return selectConverter.getObject(string);
}

public Map getVelocityParameters(Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem)
{
Map params = super.getVelocityParameters(issue, field, fieldLayoutItem);
List options = new ArrayList();
options.add("TRUE");
options.add("FALSE");
params.put("options", options);
return params;
}
}
{code}


and here
edit-select.vm

/////////////////////////////////////////////////////////////////////////////////////////////////////////
#controlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters.noHeader)

<select name="$customField.id" id="$customField.id">
#if (!$fieldLayoutItem || $fieldLayoutItem.required == false)
<option value="-1">$i18n.getText("common.words.none")</option>
#else
<option value="">$i18n.getText("common.words.none")</option>
#end
#foreach ($option in $options.options)
<option value="$textutils.htmlEncode($option.value)"
#if ($value && $value == $option.value)selected#end
$option.value</option>
#end
</select>

#controlFooter ($action $fieldLayoutItem.fieldDescription $displayParameters.noHeader)
////////////////////////////////////////////////////////////////////////////////////////////////////
--
Post by Ivan85 - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-22 17:47:51 UTC
Permalink
Hi Ivan,

You have *just* strings in your context (mapped to "options" velocity variable).

String is quite simple object, obviously without any field named "value" or method named "getValue".
Please see you foreach code block.
{code}
#foreach ($option in $options.options)
<option value="$textutils.htmlEncode($option.value)"
#if ($value && $value == $option.value)selected#end
<div class="jive-quote">$option.value</option></div>
#end
{code}

As you see you get all the options and try print out each elements' *value*. It's not there.

I believe you should rewrite this block to something like this:
{code}
#foreach ($option in $options.options)
<option value="$textutils.htmlEncode($option)"
#if ($value && $value == $option)selected#end
<div class="jive-quote">$option</option></div>
#end
{code}

Hope it helps.

--wbr, Leonids M.
--
Post by leonardinius - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-23 07:53:26 UTC
Permalink
Thank you for answer Leonids.

I've changed that block, but result the same( select list has no options ).
--
Post by Ivan85 - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-23 12:33:31 UTC
Permalink
Ough, sorry my bad.
Typo.

Please make foreach over $options, not $options.options

(since the options you have put in the cotexts are bind to the variable name $options and do not contain any inner variabsles/getters for options)

So,

{code}
#foreach ($option in $options)
<option value="$textutils.htmlEncode($option)"
#if ($value && $value == $option)selected#end
$option</option>
#end
{code}

Hope it helps.

--wbr, Leonis M.
--
Post by leonardinius - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-23 14:07:29 UTC
Permalink
Thank you! That was one mistake.
Another is that path of "package com.atlassian.jira.plugin.customfield.impl;" was not equal to path in jar. But jar also contained old folder with same named class.

Careless mistakes it's my doom.

Thank you for help Leonids!

Now "TRUE" and "FALSE" options are available to chose.
But unavailable to create issue with this field.
While crating an issue I've got next error:
"Invalid value 'FALSE' passed for customfield 'boolF'. Allowed values are: , -1"

I think that values for options should be set in the same manner as for "None" option
{code}
#if (!$fieldLayoutItem || $fieldLayoutItem.required == false)
<option value="-1">$i18n.getText("common.words.none")</option>
#else
<option value="">$i18n.getText("common.words.none")</option>
#end
{code}

Is that correct?

Thanks in advance!
--
Post by Ivan85 - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-24 00:15:13 UTC
Permalink
You are always welcome, Ivan.

Not sure about your last statement.
I believe the problem lies in the value validation method.

You see, the lifecycle of the edit form is the following:
* Form values customfield_1234:value1 are collected and CustomFieldParams are created
* The custom field params are validated
* Custom field params are persisted if no validation errors occurs

The methid responsible for value validation is *validateFromParams*
I believe the problem is - you inherit this one from SelectCFType which allows only -1 (empty value)and the values defined as the CustomField options (in the CF admin screen).

I believe you should override it to something like this (*pseudocode*):
{code}
@Override
public void validateFromParams(final CustomFieldParams relevantParams, final ErrorCollection errorCollectionToAddTo, final FieldConfig config)
{
final String selectedString = (String) relevantParams.getFirstValueForNullKey();

if (StringUtils.isNotBlank(selectedString) && !"-1".equals(selectedString))
{
if(!StringUtils.equalsIgnoreCase(selectedString, "TRUE") && StringUtils.equalsIgnoreCase(selectedString, "FALSE") ){
errorCollectionToAddTo.addError(relevantParams.getCustomField().getId(), "...");
}
}

}
{code}

Hope it helps.

--wbr, Leonids M.
--
Post by leonardinius - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-25 09:14:36 UTC
Permalink
That's great! One more time thank you Leonids! :)

But I've got another trouble.

When issue created I don't see values of new custom fields

Image:!Loading Image...!

Any ideas why?

Thanks in advance!
--
Post by Ivan85 - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-25 09:20:42 UTC
Permalink
Hi Ivan.

You are always welcome :)

I believed the problem is: you have adjusted your edit.vm custom field template appropriately. But what about view.vm template?
Could you please share it's code and we all could try to identify the problem.

--wbr, Leonids M.
--
Post by leonardinius - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-25 09:28:21 UTC
Permalink
So here the code of view-rawtext.vm
{code}
## Displays unescaped text - use only for 'trusted' content (entered by admins) that can contain HTML tags
#if ($value)
$!value.toString()
#end
{code}
--
Post by Ivan85 - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-25 09:40:45 UTC
Permalink
Hmm seems to be perfectly valid.

Just 2 cents - why to check against NPE if $value.toString() if it's already done in the #if statement.

* Could you verify that the values are actually persisted?
* The template used is the same as you posted here? (MB typo in atlassian-plugin.xml or maven resource filtering while producing jar ..)
* Mb alter the template to include #else with some debug statement (like undefined value).

I believe it's the storage problem actually (params -> value -> params conversion cycle isn;t working properly at some point).

Hope it helps,

-wbr, Leonids M.
--
Post by leonardinius - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-25 10:07:18 UTC
Permalink
Leonids, thank you very much!
You are TRUE JIRA GURU!

For now I've got all that I wanted. Values are visible!

But I think that wasn't my last trouble. :)
--
Post by Ivan85 - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
forums-YrDk4eUcT3KaMJb+
2010-01-25 12:17:34 UTC
Permalink
You are welcome, Ivan. It;s nice to help people keep rolling instead of struggling on some unclear bit of the framework.

PS. Thanks a lot for being so thankful :) The attitude like yours obviously motivates to help lots more!

--wbr, Leonids M.
--
Post by leonardinius - online at:
http://forums.atlassian.com/thread.jspa?forumID=100&threadID=40550
Continue reading on narkive:
Loading...