@Override public void evaluate(Property component, WidgetElement element) throws Exception { if (component instanceof Validatable) { Validatable validatable = (Validatable) component; try { validatable.validate(); } catch (InvalidValueException e) { errors.add(e.getMessage()); } } if (!component.isReadOnly()) { try { fetchOrCreateAttribute(element); } catch (Exception e) { errors.add(getMessage("processdata.block.error.eval.other").replaceFirst("%s", component.toString()).replaceFirst("%s", element.getBind()) + "<br/>" + e.getMessage()); } } } };
@Override public void buttonClick(Button.ClickEvent event) { Map<Field, String> messages = new LinkedHashMap<Field, String>(); for (Object propertyId : form.getItemPropertyIds()) { Field field = form.getField(propertyId); try { field.validate(); } catch (Validator.InvalidValueException e) { messages.put(field, e.getMessage()); } } if (messages.isEmpty()) { form.commit(); callback.onSave(item); } else { StringBuilder sb = new StringBuilder(); for (String msg : messages.values()) { sb.append(msg).append("<br/>"); } validationNotification(getApplication(), getI18NSource(), sb.toString()); } } });
private void validateAndSave() { if (url.getValue() == null || ((String) url.getValue()).trim().isEmpty()) { commit(); return; } try { ScriptProcessorRegistry registry = ProcessToolContext.Util.getThreadProcessToolContext().getRegistry().lookupService( ScriptProcessorRegistry.class.getName()); Property scriptType = formProperties.get("scriptEngineType"); if (scriptType == null || scriptType.getValue() == null || ((String) scriptType.getValue()).isEmpty()) throw new Validator.InvalidValueException("script.undefined.type"); ScriptProcessor scriptProcessor = registry.getScriptProcessor((String) scriptType.getValue()); if (scriptProcessor == null) throw new Validator.InvalidValueException("script.processor.not.found"); InputStream is = new URL((String) url.getValue()).openStream(); scriptProcessor.validate(is); url.commit(); showInfoNotification("validation.script.ok"); } catch (Validator.InvalidValueException e) { logger.log(Level.SEVERE, e.getMessage(), e); showWarningNotification(e.getMessage(), null); } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); showWarningNotification("validation.script.error", e.getMessage()); } }
/** * Validates the given field and sets the component error accordingly. * Please note, that the provided field must implement {@link Field} and must be a sub class of {@link AbstractComponent}. * * @param field The field to validate (must be a sub class of {@link AbstractComponent}). * @param swallowValidationExceptions Indicates if an InvalidValueException is swallowed and not propagated. * If false the first occurring InvalidValueException is thrown. * @throws Validator.InvalidValueException If the field is not valid (see {@link Validator#validate(Object)}. */ public static void validateField(Field<?> field, boolean swallowValidationExceptions) throws Validator.InvalidValueException { if (field instanceof AbstractComponent && field.isEnabled()) { try { field.validate(); ((AbstractComponent) field).setComponentError(null); } catch (Validator.InvalidValueException ex) { // Some fields unify exceptions, we have to consider this if (ex.getMessage() == null) { ex = ex.getCauses()[0]; } // set error message ((AbstractComponent) field).setComponentError(new UserError(ex.getMessage())); if (!swallowValidationExceptions) { throw ex; } } } }
private void validateAndSave() throws Validator.InvalidValueException { if (code.getValue() == null || ((String) code.getValue()).trim().isEmpty()) { commit(); return; } try { ScriptProcessorRegistry registry = ProcessToolContext.Util.getThreadProcessToolContext().getRegistry().lookupService( ScriptProcessorRegistry.class.getName()); Property scriptType = formProperties.get("scriptEngineType"); if (scriptType == null || scriptType.getValue() == null || ((String) scriptType.getValue()).isEmpty()) throw new Validator.InvalidValueException("script.undefined.type"); ScriptProcessor scriptProcessor = registry.getScriptProcessor((String) scriptType.getValue()); if (scriptProcessor == null) throw new Validator.InvalidValueException("script.processor.not.found"); InputStream is = new ByteArrayInputStream(((String) code.getValue()).getBytes()); scriptProcessor.validate(is); code.commit(); showInfoNotification("validation.script.ok"); } catch (Validator.InvalidValueException e) { logger.log(Level.SEVERE, e.getMessage(), e); showWarningNotification(e.getMessage(), null); } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); showWarningNotification("validation.script.error", e.getMessage()); } }