cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Display alert dialog on user interface from custom java validation

JD_8489700
10-Marble

Display alert dialog on user interface from custom java validation

Hello everyone!

I've made a custom validation class on Promotion Request Wizard to allow only 1 Object to be promoted.

On my implementation of DefaultUIComponentValidator, the method performFullPreValidation(UIValidationKey arg0, UIValidationCriteria arg1, Locale arg2) throws a WTException when validation not met the requirements, but the message on Exception do not show to the final user, just appear on console.

What's happening? Am I missing something? What's the correct way to display exception message to the final user through alert dialog?

 

This is the implementation:

 

 

public UIValidationResultSet performFullPreValidation(UIValidationKey arg0, UIValidationCriteria arg1, Locale arg2) throws WTException {
	if(arg1.getTargetObjects().size() == 1) {
		return super.performFullPreValidation(arg0, arg1, arg2);
	} else if(arg1.getTargetObjects().size() > 1) {
		throw new WTException("Only one object allowed for promotion");
	} else {
		throw new WTException("Need to select one object for promotion");
	}
		
}

 

 

 

 

ACCEPTED SOLUTION

Accepted Solutions

 

Hello @JD_8489700 ,

 

I try some ways how to achieve what you need but I don't think that it is possible by that Validator. May be I am not right.

 

I found the solution but is is not recommended because .I change js script directly in the OOTB js file.

That means every update/upgrade you have to change that js file. (i guess that there could be better implementation 😄 )

 

But I couldn't find better way now how to disallow promotes just one object even there are more. (I could not find the promotable objects in a NmCommandBean there is just selection... )

 

you have to modify JS script with following code

the file is in <WTHOME>\codebase\netmarkets\javascript\promotionRequest\promotionRequest.js

 

find a row with function validatePromotionTable: function(wizardAction) {,,,,,

 

in the end is a row with the code 

 

 

return PTC.promotion.validateForDuplicates(allOids, promotedOids);

 

 

before this row put following code that controls how many promotables are set to promote

 

 

            if (promotedOids.length !== 1 ) {
                JCAAlert(
                    "",
                    "only One promotables is allowed"
                    + "\n\n"
                    + "remove some promotables from table");
                return false;
            }

 

 

save the file, and restart Windchill. 

clear a explorer cache or use CTRL+5 to load new js script. 

{in some cases the restart is not needed. }

 

HelesicPetr_0-1662399408498.png

 

 

PetrH

View solution in original post

8 REPLIES 8
avillanueva
22-Sapphire II
(To:JD_8489700)

throwing an exception is not the right way. You need to return a UIValidationResult with a UIValidationStatus.DENIED.  Page 426 of the customizer's guide has an example.  There you can provide a message back to the user.

Hello @avillanueva!

The example on page 426 is marked as deprecated and need to change the return type to UIValidationResult, but the return type is UIValidationResultSet.

I'm going to try to use some UIValidationResultSet implementations...

Hi @JD_8489700 

Try to use something like this>

 

 

 

		if(arg1.getTargetObjects().size() == 1) {
			return super.performFullPreValidation(arg0, arg1, arg2);
		} else if(arg1.getTargetObjects().size() > 1) {

			UIValidationResultSet validResultSet = UIValidationResultSet.newInstance();
			UIValidationResult result = UIValidationResult.newInstance();
			result.setTargetObject(arg1.getContextObject());
			result.setStatus(UIValidationStatus.DENIED); // or NOT_VALIDATED
			
			List<UIValidationFeedbackMsg> msgList = new ArrayList();
			msgList.add(UIValidationFeedbackMsg.newInstance("Only one object allowed for promotion", FeedbackType.FAILURE));

			result.setFeedbackMsgList(msgList);
			validResultSet.addResult(result);
			return validResultSet;

		} else {
			
			UIValidationResultSet validResultSet = UIValidationResultSet.newInstance();
			UIValidationResult result = UIValidationResult.newInstance();
			result.setTargetObject(arg1.getContextObject());
			result.setStatus(UIValidationStatus.DENIED); // or NOT_VALIDATED

			List<UIValidationFeedbackMsg> msgList = new ArrayList();
			msgList.add(UIValidationFeedbackMsg.newInstance("Need to select one object for promotion", FeedbackType.FAILURE));

			result.setFeedbackMsgList(msgList);
			validResultSet.addResult(result);
			return validResultSet;
		}

 

 

Hope this can help

 

PetrH

 

Hi @HelesicPetr, this method did not returned the message to the user and passed validation as valid.

Is something missing in other configuration?

Hi @JD_8489700 

I guess that there could be better status for result

 

result.setStatus(UIValidationStatus.DENIED); // or NOT_VALIDATED

 

 

DENIED do not work try another one.  Check UIValidationStatus.class to get all options and try them. 

 

where is  a performFullPreValidation() called? from some Validator class defined in a modelAction.xml or by clicking finish so in Processor? or another way? 

 

If you call own ...Processor and use preOperation method to check the results the return is FormResult and the message to a user needs to be set in the FormResult.

 

The performFullPreValidation do not return the FormResult thus you need to read the error message from the UIValidationResultSet and forward it to a FormResult object.

 

FormResult uses a FeedbackMessage object. Exampe:

 

 

ArrayList<String> incorectMessage = new ArrayList<>();
WTMessage msg = new WTMessage("cz.aveng.Publish.PublishMessageResource", "TASK_DELETE_WARNING_PERMISION_TITLE", (Object[]) null);
feedBack = new FeedbackMessage(FeedbackType.FAILURE, locale, msg.getLocalizedMessage(locale), incorectMessage, "Only CAD Documents are allowed to add. Remove selection of non CAD Documents");

 

PetrH 

Hi @HelesicPetr, I've created an entry into my xconf:

<Service context="default" name="com.ptc.core.ui.validation.UIComponentValidator" targetFile="codebase/service.properties">
	<Option cardinality="duplicate" requestor="java.lang.Object" selector="promotionObjectList" serviceClass="com.myclass.validator.PromotionRequest"/>
</Service>

and an entry in the service.properties:

wt.services/svc/default/com.ptc.core.ui.validation.UIComponentValidator/promotionObjectList/null/0=com.myclass.validator.PromotionRequest/duplicate

 

If I get it, I need to implement the FormResult when I try to validade the component. Correct?

Where should I start?

 

 

Hello @JD_8489700 ,

 

I try some ways how to achieve what you need but I don't think that it is possible by that Validator. May be I am not right.

 

I found the solution but is is not recommended because .I change js script directly in the OOTB js file.

That means every update/upgrade you have to change that js file. (i guess that there could be better implementation 😄 )

 

But I couldn't find better way now how to disallow promotes just one object even there are more. (I could not find the promotable objects in a NmCommandBean there is just selection... )

 

you have to modify JS script with following code

the file is in <WTHOME>\codebase\netmarkets\javascript\promotionRequest\promotionRequest.js

 

find a row with function validatePromotionTable: function(wizardAction) {,,,,,

 

in the end is a row with the code 

 

 

return PTC.promotion.validateForDuplicates(allOids, promotedOids);

 

 

before this row put following code that controls how many promotables are set to promote

 

 

            if (promotedOids.length !== 1 ) {
                JCAAlert(
                    "",
                    "only One promotables is allowed"
                    + "\n\n"
                    + "remove some promotables from table");
                return false;
            }

 

 

save the file, and restart Windchill. 

clear a explorer cache or use CTRL+5 to load new js script. 

{in some cases the restart is not needed. }

 

HelesicPetr_0-1662399408498.png

 

 

PetrH

Thanks, @HelesicPetr , this is it!

Thanks for your time!

Announcements

Top Tags