Custom Field Validation Message

Hi guys,

I have a custom field type and I want to add a validation message as the attached picture shows:


How to have exactly this message?
I"ve tried to use the following:

errorCollectionToAddTo.addErrorMessage(config.getCustomField().getFieldName()+": "+i18n.getText(ALLOW_SINGLE_VALUE_MESSAGE), Reason.VALIDATION_FAILED);

But, the displayed message was at the begining of the form not as the picture shows. Can you please help?

thanks,
Rosy

Hello all,

Can someone please help?

Thank you,
Rosy

Hi Rosy,

I think maybe you should use the field-specific version of addError:

void addError(String field, String message);

or…

void addError(String field, String message, Reason reason);

David

Hi @david.pinn,
When I tried the 2nd suggestion, I got the following message at the top of the page:

I’m still not able to have the red format at the level of the custom field.
Any idea?
thanks,
rosy

This should work

errorCollectionToAddTo.addError(config.getFieldId(), i18n.getText(ALLOW_SINGLE_VALUE_MESSAGE), Reason.VALIDATION_FAILED);

You would need to ensure that use correct Field ID, so that the error message can get aligned with field on form. Otherwise it will end up as an error message at top of form afaik

thank you @mmcmahon for your reply, I’ve put the code you suggested inside validateFromParams method of my custom field type, so, for sure, I have the correct field ID, but, I"m still having the error message at the top of the page. I have v7.5.0. Any idea?

 @Override
	 public void validateFromParams(final CustomFieldParams relevantParams, final ErrorCollection errorCollectionToAddTo, final FieldConfig config)
{
errorCollectionToAddTo.addError(config.getFieldId(), i18n.getText(ALLOW_SINGLE_VALUE_MESSAGE), Reason.VALIDATION_FAILED);
}

thanks a lot,
Rosy

Hello @mmcmahon again,
I have put “assignee” word instead of config.getFieldId() in the 1st parameter, and the red message was displayed correctly under “Assignee” field, so, you are right, the issue is with this 1st parameter, but, what’s wrong with : config.getFieldId() ?
Can’t we use this API with specific custom field types?
To note that my custom field type extends AbstractMultiCFType<CrmProject> where CrmProject is an active object class.

thank you,
Rosy

Just wondering, on your velocity template, do you use #customControlHeader / #customControlFooter macros? I believe this takes care of the validation error message, etc.

See https://developer.atlassian.com/jiradev/jira-platform/guides/fields/tutorial-custom-fields-in-jira and potentially https://community.atlassian.com/t5/Answers-Developer-Questions/What-should-be-as-customControlHeader-in-jira-6-0-5/qaq-p/521473

Hope this might help

Hi @mmcmahon,

Thanks a lot for your quick reply. On the edit view of my .vm file, there is already those macros:

#customControlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters $auiparams)

#customControlFooter ($action $customField.id $fieldLayoutItem.fieldDescription $displayParameters $auiparams)

Also, my custom field is working perfectly, it is sttatable and searchable as well. Is there any reason why this validation message doesn’t appear?

thanks in advance,
Rosy

I just tested with my own custom field returning an error, using addError method, plus the header and footer macros.

I also did not provide a summary on issue trying to create.

I got form errors at the top for summary and my custom field, but also error in-line below the input field for summary and my custom field.

Are you getting an error at top and inline with field?

If so that seems expected.

Hi @mmcmahon,

The message “you are allowed to add one value!” appears at top of the form only.

Did I answer your question?
thanks a lot,
Rosy

Sorry, I have no idea :frowning:

Only other thing I can think of is to ensure that you haven’t somehow set a value for a key named “theme” in the display/velocity parameters map that return

Seems very strange. Should work afaik

@mmcmahon thanks a lot! I’ve checked but, this is not set.
I’ve discovered that the problem is being reproduced on all special CF types (CF types that extend AbstractMultiCFType class of a new object (e.g. AbstractMultiCFType<ClassX> ). If we use simply AbstractMultiCFType <Option> or any simple object instead , this is being correctly displayed, but, I can’t.
Anyways. thanks you for your help,
Rosy