throw new Validator.InvalidValueException(null, exceptions);
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()); } }
/** * Translate a Vaadin {@link InvalidValueException} into a {@link ValidationException}. * @param exception Exception to translate (not null) * @return Translated ValidationException */ public static ValidationException translateValidationException(InvalidValueException exception) { ObjectUtils.argumentNotNull(exception, "ValidationException must not be null"); List<ValidationException> cs = new LinkedList<>(); if (exception.getCauses() != null && exception.getCauses().length > 0) { for (InvalidValueException cause : exception.getCauses()) { cs.add(translateValidationException(cause)); } } return new ValidationException(exception.getLocalizedMessage(), cs); }
throw new Validator.InvalidValueException("Threshold must be a positive number"); throw new Validator.InvalidValueException("Base must be greater than 1.0");
int intValue = Integer.parseInt((String) value); if (intValue <= 0) { throw new Validator.InvalidValueException("Weight must be > 0"); throw new Validator.InvalidValueException("Weight must be a number");
if (value != null) { if (((Integer) value).intValue() <= 0) { throw new Validator.InvalidValueException("must be > 0"); if (value != null) { if (((Long) value).intValue() <= 0) { throw new Validator.InvalidValueException("must be > 0");
if (value != null) { if (((Integer) value).intValue() <= 0) { throw new Validator.InvalidValueException("must be > 0"); if (value != null) { if (((Long) value).intValue() <= 0) { throw new Validator.InvalidValueException("must be > 0");
messageTemplate = (String) m.invoke(a); } catch (Exception ex) { throw new InvalidValueException( "Annotation must have message attribute");
String msg = getErrorMessage(value); if (msg != null) { throw new InvalidValueException(msg); } else { throw new InvalidValueException("Conversion exception"); throw new InvalidValueException(b.toString());
public void validate(PC value) throws Validator.InvalidValueException { // If there is no validator, there can not be any errors if (validators == null) { return; } // Initialize temps List<InvalidValueException> errors = new ArrayList<InvalidValueException>(); // validate the underlying value, not the formatted value // Gets all the validation errors for (Validator validator : validators) { try { validator.validate(value); } catch (Validator.InvalidValueException e) { errors.add(e); } } // If there were no error if (errors.isEmpty()) { return; } // If only one error occurred, throw it forwards if (errors.size() == 1) { throw errors.get(0); } // Creates composite validation exception final Validator.InvalidValueException[] exceptions = errors.toArray(new Validator.InvalidValueException[errors.size()]); throw new Validator.InvalidValueException(null, exceptions); }
/** * Checks the given value against this validator. If the value is valid the method does nothing. If the value is * invalid, an {@link InvalidValueException} is thrown. * * @param value * the value to check * * @throws InvalidValueException * if the value is invalid */ @Override public void validate(Object value) throws InvalidValueException { Set<?> violations = javaxValidator.validateValue(beanClass, propertyName, value); if (violations.size() > 0) { InvalidValueException[] causes = new InvalidValueException[violations.size()]; int i = 0; for (Object v : violations) { final ConstraintViolation<?> violation = (ConstraintViolation<?>) v; //interpolator will use CurrentLocale SimpleContext context = new SimpleContext(propertyName, value, violation.getConstraintDescriptor()); String msg = krailInterpolator.interpolate(violation.getMessageTemplate(), context); causes[i] = new InvalidValueException(msg); ++i; } throw new InvalidValueException(null, causes); } }
/** * 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()); } }
@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()); } } });
/** * @see com.vaadin.data.Validator#validate(java.lang.Object) */ public void validate(Object value) throws InvalidValueException { if (CmsStringUtil.isNotEmptyOrWhitespaceOnly((String)value) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_fieldOU.getValue())) { try { List<CmsResource> ouRes = OpenCms.getOrgUnitManager().getResourcesForOrganizationalUnit( A_CmsUI.getCmsObject(), m_fieldOU.getValue()); String resPath = (String)value; for (CmsResource res : ouRes) { if (resPath.startsWith(res.getRootPath())) { return; } } throw new InvalidValueException( "The resource path " + value + " is not part of the project OU '" + m_fieldOU.getValue() + "'."); } catch (CmsException e) { // ignore } } } }
/** * @see com.vaadin.data.Validator#validate(java.lang.Object) */ public void validate(Object value) throws InvalidValueException { if ((m_editProperty == CmsResourceTableProperty.PROPERTY_RESOURCE_NAME) && (value instanceof String)) { try { String newName = (String)value; CmsResource.checkResourceName(newName); CmsObject cms = A_CmsUI.getCmsObject(); CmsResource res = cms.readResource(m_editId); if (!res.getName().equals(newName)) { String sourcePath = cms.getSitePath(res); if (cms.existsResource(CmsStringUtil.joinPaths(CmsResource.getParentFolder(sourcePath), newName))) { throw new InvalidValueException("The selected filename already exists."); } } } catch (CmsIllegalArgumentException e) { throw new InvalidValueException(e.getLocalizedMessage(A_CmsUI.get().getLocale())); } catch (CmsException e) { LOG.warn("Error while validating new filename", e); throw new InvalidValueException(e.getLocalizedMessage(A_CmsUI.get().getLocale())); } } } }
@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()); } } } };
public void validateInternal() { if (!modifiedValue.isEmpty()) { for (ItemValueForm form : forms) { form.commit(); } for (DictionaryItemValueWrapper val : modifiedValue) { Date startDate = val.getValidStartDate(); Date endDate = val.getValidEndDate(); if (endDate != null && startDate != null && endDate.before(startDate)) { throw new InvalidValueException(getMessage("validate.item.val.dates")); } } boolean startDateFullRange = false, endDateFullRange = false; for (DictionaryItemValueWrapper val : modifiedValue) { startDateFullRange = validateSingleDate(startDateFullRange, val, val.getValidStartDate()); endDateFullRange = validateSingleDate(endDateFullRange, val, val.getValidEndDate()); } } }
/** * @see com.vaadin.data.Validator#validate(java.lang.Object) */ public void validate(Object value) throws InvalidValueException { CmsScheduledJobInfo info = new CmsScheduledJobInfo(); String stringValue = (String)value; // Job name may be needed in exception try { info.setJobName(m_fieldJobName.getValue()); } catch (CmsRuntimeException e) { throw new InvalidValueException(e.getLocalizedMessage(A_CmsUI.get().getLocale())); } try { info.setClassName(stringValue); } catch (CmsIllegalArgumentException e) { throw new InvalidValueException(e.getLocalizedMessage(A_CmsUI.get().getLocale())); } } }
/** * @see com.vaadin.data.Validator#validate(java.lang.Object) */ public void validate(Object value) throws InvalidValueException { CmsScheduledJobInfo info = new CmsScheduledJobInfo(); // Job name may be needed in exception try { info.setJobName(m_fieldJobName.getValue()); } catch (CmsRuntimeException e) { throw new InvalidValueException(e.getLocalizedMessage(A_CmsUI.get().getLocale())); } String stringValue = (String)value; try { info.setCronExpression(stringValue); } catch (CmsIllegalArgumentException e) { throw new InvalidValueException(e.getLocalizedMessage(A_CmsUI.get().getLocale())); } } }