Java Code Examples for org.apache.wicket.validation.IValidatable#error()

The following examples show how to use org.apache.wicket.validation.IValidatable#error() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StrongPasswordValidator.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
private void error(IValidatable<String> pass, String key, Map<String, Object> params) {
	if (web) {
		ValidationError err = new ValidationError().addKey(key);
		if (params != null) {
			err.setVariables(params);
		}
		pass.error(err);
	} else {
		String msg = LabelDao.getString(key, 1L);
		if (params != null && !params.isEmpty() && !Strings.isEmpty(msg)) {
			for (Map.Entry<String, Object> e : params.entrySet()) {
				msg = msg.replace(String.format("${%s}", e.getKey()), "" + e.getValue());
			}
		}
		log.warn(msg);
		pass.error(new ValidationError(msg));
	}
}
 
Example 2
Source File: HibernateValidatorProperty.java    From pm-wicket-utils with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void validate(IValidatable<Object> validatable) {
	Validator validator = HibernateValidatorProperty.validatorFactory.getValidator();
	
	@SuppressWarnings("unchecked")
	Set<ConstraintViolation<Object>> violations = validator.validateValue((Class<Object>)beanModel.getObject().getClass(), propertyName, validatable.getValue());
	
	if(!violations.isEmpty()){
		for(ConstraintViolation<?> violation : violations){
			ValidationError error = new ValidationError(violation.getMessage());
			
			String key = violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName();
			if(getValidatorPrefix()!=null) key = getValidatorPrefix()+"."+key;
			
			error.addKey(key);
			error.setVariables(new HashMap<String, Object>(violation.getConstraintDescriptor().getAttributes()));
			
			//remove garbage from the attributes
			error.getVariables().remove("payload");
			error.getVariables().remove("message");
			error.getVariables().remove("groups");
			
			validatable.error(error);
		}
	}
}
 
Example 3
Source File: ManageUsersPage.java    From webanno with Apache License 2.0 6 votes vote down vote up
private void validateRoles(IValidatable<Collection<Role>> aValidatable)
{
    Collection<Role> newRoles = aValidatable.getValue();
    if (newRoles.isEmpty()) {
        aValidatable.error(new ValidationError()
                .setMessage("A user has to have at least one role."));
    }
    // enforce users to have at least the ROLE_USER role
    if (!newRoles.contains(Role.ROLE_USER)) {
        aValidatable.error(new ValidationError()
                .setMessage("Every user must have 'ROLE_USER'."));
    }
    // don't let an admin user strip himself of admin rights
    if (userRepository.getCurrentUser().equals(getModelObject())
            && !newRoles.contains(Role.ROLE_ADMIN)) {
        aValidatable.error(new ValidationError()
                .setMessage("You cannot remove your own admin status."));
    }
}
 
Example 4
Source File: GeneralSettingsPanel.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> aValidatable)
{
    String kbName = aValidatable.getValue();
    if (kbService.knowledgeBaseExists(projectModel.getObject(), kbName)) {
        String message = String.format(
                "There already exists a knowledge base in the project with name: [%s]!",
                kbName);
        aValidatable.error(new ValidationError(message));
    }
}
 
Example 5
Source File: OPropertyValueValidator.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
protected void validateEmbedded(final IValidatable<T> validatable,
		final OProperty p, final Object fieldValue) {
	if (fieldValue instanceof ORecordId) {
		validatable.error(newValidationError("embeddedRecord"));
		return;
	} else if (fieldValue instanceof OIdentifiable) {
		if (((OIdentifiable) fieldValue).getIdentity().isValid()) {
			validatable.error(newValidationError("embeddedRecord"));
			return;
		}

		final OClass embeddedClass = p.getLinkedClass();
		if (embeddedClass != null) {
			final ORecord rec = ((OIdentifiable) fieldValue).getRecord();
			if (!(rec instanceof ODocument)) {
				validatable.error(newValidationError("embeddedNotDoc"));
				return;
			}

			final ODocument doc = (ODocument) rec;
			if (doc.getSchemaClass() == null
					|| !(doc.getSchemaClass().isSubClassOf(embeddedClass))) {
				validatable.error(newValidationError("embeddedWrongType", "expectedType", embeddedClass.getName()));
				return;
			}
		}

	} else {
		validatable.error(newValidationError("embeddedNotDoc"));
		return;
	}
}
 
Example 6
Source File: OPropertyValueValidator.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
protected void validateLink(final IValidatable<T> validatable,
		final OProperty p, final Object linkValue) {
	if (linkValue == null)
		validatable.error(newValidationError("nulllink"));
	else {
		ORecord linkedRecord = null;
		if (linkValue instanceof OIdentifiable)
			linkedRecord = ((OIdentifiable) linkValue).getRecord();
		else if (linkValue instanceof String)
			linkedRecord = new ORecordId((String) linkValue).getRecord();
		else
			validatable.error(newValidationError("linkwrong"));

		if (linkedRecord != null && p.getLinkedClass() != null) {
			if (!(linkedRecord instanceof ODocument))
				validatable.error(newValidationError("linktypewrong",
						"linkedClass", p.getLinkedClass(), "identity",
						linkedRecord.getIdentity()));

			final ODocument doc = (ODocument) linkedRecord;

			// AT THIS POINT CHECK THE CLASS ONLY IF != NULL BECAUSE IN CASE
			// OF GRAPHS THE RECORD COULD BE PARTIAL
			if (doc.getSchemaClass() != null
					&& !p.getLinkedClass().isSuperClassOf(
							doc.getSchemaClass()))
				validatable.error(newValidationError("linktypewrong",
						"linkedClass", p.getLinkedClass(), "identity",
						linkedRecord.getIdentity()));

		}
	}
}
 
Example 7
Source File: FoundEntityValidator.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> validatable) {
	String value = validatable.getValue();
	String path = StorageUtil.createPath(parentPath, value);		
	if (storageService.entityExists(path)) {			
		ValidationError error = new ValidationError();
		String message = String.format(errorMessage, value);
		error.setMessage(message);
		validatable.error(error);
	}
}
 
Example 8
Source File: PortugueseNIFValidator.java    From pm-wicket-utils with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> validatable) {
	super.validate(validatable);
	
	final String value = validatable.getValue();
	
	if(value==null || value.length()==0) return;
	
	try {
		if(!isNIFValid(value)) validatable.error(new ValidationError(this));
	} catch(NumberFormatException e){
		validatable.error(new ValidationError(this));
	}
}
 
Example 9
Source File: UsernameTextField.java    From wicket-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> validatable) {
	final String field = validatable.getValue();
	if(service.usernameExists(field)){
		ValidationError error = new ValidationError();
		error.setMessage(getString(getClass().getSimpleName()));
		validatable.error(error);
	}
	
}
 
Example 10
Source File: ManageUsersPage.java    From webanno with Apache License 2.0 5 votes vote down vote up
private void validateEnabled(IValidatable<Boolean> aValidatable)
{
    if (!aValidatable.getValue()
            && userRepository.getCurrentUser().equals(getModelObject())) {
        aValidatable.error(new ValidationError()
                .setMessage("You cannot disable your own account."));
    }
}
 
Example 11
Source File: ManageUsersPage.java    From webanno with Apache License 2.0 5 votes vote down vote up
private void validateUsername(IValidatable<String> aValidatable)
{
    if (userRepository.exists(aValidatable.getValue()) && isCreate) {
        aValidatable.error(new ValidationError().addKey("username.alreadyExistsError")
                .setVariable("name", aValidatable.getValue()));
    }
    else if (aValidatable.getValue().contains(" ")) {
        aValidatable.error(new ValidationError().addKey("username.containsSpaceError"));
    }
    else if (!NameUtil.isNameValid(aValidatable.getValue())) {
        aValidatable.error(new ValidationError().addKey("username.invalidCharactersError"));
    }
}
 
Example 12
Source File: ProjectDetailPanel.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> aValidatable)
{
    String newName = aValidatable.getValue();
    String oldName = aValidatable.getModel().getObject();
    if (!StringUtils.equals(newName, oldName) && isNotBlank(newName)
            && projectService.existsProject(newName)) {
        aValidatable.error(new ValidationError(
                "Another project with the same name exists. Please try a different name"));
    }
}
 
Example 13
Source File: ProjectDetailPanel.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> aValidatable)
{
    String newName = aValidatable.getValue();
    if (isNotBlank(newName) && !NameUtil.isNameValid(newName)) {
        aValidatable.error(new ValidationError(
                "Project name shouldn't contain characters such as /\\*?&!$+[^]"));
    }
}
 
Example 14
Source File: KeyBindingsConfigurationPanel.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> aValidatable)
{
    String keyCombo = aValidatable.getValue();
    KeyBinding keyBinding = new KeyBinding(keyCombo, null);
    if (!keyBinding.isValid()) {
        aValidatable.error(new ValidationError("Invalid key combo: [" + keyCombo + "]"));
    }
}
 
Example 15
Source File: AnalysisNameValidator.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> validatable) {
	String value = validatable.getValue();
	if (value.contains(" ")) {			
		ValidationError error = new ValidationError();   
		String errorM = errorMessage;
		if (errorM == null) {
			errorM = "' ' not allowed in name";
		}
           error.setMessage(errorM);
           validatable.error(error);
	} else {
		super.validate(validatable);
	}
}
 
Example 16
Source File: TagSetEditorPanel.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> aValidatable)
{
    String newName = aValidatable.getValue();
    String oldName = aValidatable.getModel().getObject();
    if (!StringUtils.equals(newName, oldName) && isNotBlank(newName)
            && annotationSchemaService.existsTagSet(newName, selectedProject.getObject())) {
        aValidatable.error(new ValidationError(
                "Another tag set with the same name exists. Please try a different name"));
    }
}
 
Example 17
Source File: UserEmailValidator.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> validatable) {
    String email = validatable.getValue();
    if (OrienteerUserRepository.isUserExistsWithEmail(email)) {
        ValidationError error = new ValidationError(this);
        error.setVariable("email", email);
        validatable.error(error);
    }
}
 
Example 18
Source File: IntervalFieldStringValidator.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
@Override
 public void validate(IValidatable<String> validatable) {
     boolean hasError = false;
     String s = validatable.getValue();
     String[] elements = s.split(",");
     if (elements.length > 1) {
         List<String> nonDuplicatesList = new ArrayList<String>(new LinkedHashSet<String>(Arrays.asList(elements)));
         if (elements.length != nonDuplicatesList.size()) {
             // have duplicates
         	hasError = true;
         }
     } else {
         elements = s.split("-");
         if (elements.length == 2) {
             if (SelectIntervalPanel.getComparator(entityType, false).compare(elements[0], elements[1]) >= 0) {
             	hasError = true;
             }
         }
     }
     
     if (hasError) {
ValidationError error = new ValidationError();
String messageKey = Classes.simpleName(IntervalFieldStringValidator.class);
error.addKey(messageKey);
validatable.error(error);
     }
 }
 
Example 19
Source File: IriValidator.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(IValidatable<String> validatable)
{
    if (!URIUtil.isValidURIReference(validatable.getValue())) {
        validatable.error(new ValidationError(this));
    }
}
 
Example 20
Source File: OPropertyValueValidator.java    From wicket-orientdb with Apache License 2.0 4 votes vote down vote up
protected void validateType(final IValidatable<T> validatable,
		final OProperty p, final Object value) {
	if (value != null)
		if (OType.convert(value, p.getLinkedType().getDefaultJavaType()) == null)
			validatable.error(newValidationError("wrongtype", "expectedType", p.getLinkedType().toString()));
}