Stripes

Errors occured on null or not present indexed properties if required parameter is used within @ValidateNestedProperties

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Critical Critical
  • Resolution: Cannot Reproduce
  • Affects Version/s: Release 1.5.6
  • Fix Version/s: None
  • Component/s: Validation
  • Labels:
    None

Description

If you use the @ValidateNestedProperties annotation and some of your nested properties are required, you will get validation errors even the index property is null or not present in the request.

The Documentation at http://www.stripesframework.org/display/stripes/Indexed+Properties says:

.. Required field validations are only applied if at least one value with the same index was supplied. To understand this it is easier to think of indexed properties as a mechanism for creating multi-row forms. And this change means that rows in the form that are completely empty are ignored.

The nested validation below whould raise a validation error "name is required" even the indexed property books is not present in the request (null).

@ValidateNestedProperties({
   @Validate(field = "name", required = true, maxlength = 100),
   @Validate(field = "description", maxlength = 255)
})
private List<Book> books;

I fixed this problem and attached the modified classes.

@BenGunter: is the stripes project dead??

Activity

Haggi made changes - 16/Jan/12 2:39 AM
Field Original Value New Value
Attachment DefaultValidationMetadataProvider.java [ 10540 ]
Hide
Haggi added a comment - 16/Jan/12 2:48 AM

!! Please use the latest uploaded file DefaultValidationMetadataProvider.class !!

I added a check if the type of the field which holds the @ValidateNestedProperty annotation is an instanceof Iterable to mark this as an indexed property. This is a fix to the previous uploaded file which consider all properties holds the @ValidateNestedProperty are indexed properties.

Show
Haggi added a comment - 16/Jan/12 2:48 AM !! Please use the latest uploaded file DefaultValidationMetadataProvider.class !! I added a check if the type of the field which holds the @ValidateNestedProperty annotation is an instanceof Iterable to mark this as an indexed property. This is a fix to the previous uploaded file which consider all properties holds the @ValidateNestedProperty are indexed properties.
Ben Gunter made changes - 30/Mar/12 8:09 AM
Fix Version/s Release 1.5.7 [ 10150 ]
Hide
Ben Gunter added a comment - 17/May/12 11:19 AM

I've tested this against the 1.5.x branch, and it works as documented. Perhaps some clarification is needed. The documentation does not say the validations only apply if (in your example) the parameter books[0] is present. It says it applies if any parameter is present having the same index. Thus, if you send books[0].description=foo then the validation for books[0].name applies, even if there is no books[0] parameter. This is useful for creating new, uninitialized objects using lists, as opposed to updating existing objects, in which you would send a books[0]=123 parameter to identify the object being updated.

Thank you for the patch. If you think I've misunderstood, please submit a full web application to demonstrate the problem.

Show
Ben Gunter added a comment - 17/May/12 11:19 AM I've tested this against the 1.5.x branch, and it works as documented. Perhaps some clarification is needed. The documentation does not say the validations only apply if (in your example) the parameter books[0] is present. It says it applies if any parameter is present having the same index. Thus, if you send books[0].description=foo then the validation for books[0].name applies, even if there is no books[0] parameter. This is useful for creating new, uninitialized objects using lists, as opposed to updating existing objects, in which you would send a books[0]=123 parameter to identify the object being updated. Thank you for the patch. If you think I've misunderstood, please submit a full web application to demonstrate the problem.
Ben Gunter made changes - 17/May/12 11:19 AM
Resolution Cannot Reproduce [ 5 ]
Fix Version/s Release 1.5.7 [ 10150 ]
Assignee Ben Gunter [ bengunter ]
Status Open [ 1 ] Resolved [ 5 ]
Hide
Haggi added a comment - 04/Jun/12 7:31 AM

I think you misunderstood me

The validation process will raise an error if the property is not present or null, because of the implementation in validateRequiredFields() within the DefaultActionBeanPropertyBinder.java class.

The assembling of index params at the top of the method is the cause of the problem, because it collects only params as indexed params that are present in the request.

// Assemble a set of names that we know have indexed parameters, so we won't check
// for required-ness the regular way
Set<String> indexedParams = new HashSet<String>();
for (ParameterName name : parameters.keySet()) {
	if (name.isIndexed()) {
		indexedParams.add(name.getStrippedName());
	}
}

So the better solution is extending the DefaultValidationMetadataProvider to obtain the information out of the @NestedProperty annotated property, if it is an instance of Iterable.

Please re-test it.

Show
Haggi added a comment - 04/Jun/12 7:31 AM I think you misunderstood me The validation process will raise an error if the property is not present or null, because of the implementation in validateRequiredFields() within the DefaultActionBeanPropertyBinder.java class. The assembling of index params at the top of the method is the cause of the problem, because it collects only params as indexed params that are present in the request.
// Assemble a set of names that we know have indexed parameters, so we won't check
// for required-ness the regular way
Set<String> indexedParams = new HashSet<String>();
for (ParameterName name : parameters.keySet()) {
	if (name.isIndexed()) {
		indexedParams.add(name.getStrippedName());
	}
}
So the better solution is extending the DefaultValidationMetadataProvider to obtain the information out of the @NestedProperty annotated property, if it is an instance of Iterable. Please re-test it.
Hide
Ben Gunter added a comment - 04/Jun/12 7:38 AM

If, indeed, I did misunderstand what you are saying then I still don't see what the problem is. I'll retest if you submit a complete web app to demonstrate the problem.

Show
Ben Gunter added a comment - 04/Jun/12 7:38 AM If, indeed, I did misunderstand what you are saying then I still don't see what the problem is. I'll retest if you submit a complete web app to demonstrate the problem.

People

Vote (0)
Watch (0)

Dates

  • Created:
    13/Jan/12 9:47 AM
    Updated:
    04/Jun/12 7:38 AM
    Resolved:
    17/May/12 11:19 AM