[hibernate-issues] [Hibernate-JIRA] Issue Comment Edited: (HV-435) org.hibernate.validator.constraints.impl.PatternValidator rejecting empty strings

Stephen Hiley (JIRA) noreply at atlassian.com
Mon Feb 14 19:27:05 EST 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HV-435?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39973#action_39973 ] 

Stephen Hiley edited comment on HV-435 at 2/14/11 6:26 PM:
-----------------------------------------------------------

Yes, I implemented that already, but consider that a workaround. We are developing under a Spring/JSP framework, and so do not have the option of automatically converting empty strings to nulls. So I understand your position, but retaining the status quo will make this an ongoing pain point for us and many others. From a practical standpoint, I think permitting empty strings will be the better path. Thanks for your consideration!

A case in point from your own framework where this has been successfully employed:
{code}
/**
 * Validate that the string is a valid URL.
 *
 * @author Hardy Ferentschik
 */
public class URLValidator implements ConstraintValidator<URL, String> {
	private String protocol;
	private String host;
	private int port;

	public void initialize(URL url) {
		this.protocol = url.protocol();
		this.host = url.host();
		this.port = url.port();
	}

	public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
		if ( value == null || value.length() == 0 ) {
			return true;
		}
{code}


      was (Author: shiley65):
    Yes, I implemented that already, but consider that a workaround. We are developing under a Spring/JSP framework, and so do not have the option of automatically converting empty strings to nulls. So I understand your position, but retaining the status quo will make this an ongoing pain point for us and many others. From a practical standpoint, I think permitting empty strings will be the better path. Thanks for your consideration!
  
> org.hibernate.validator.constraints.impl.PatternValidator rejecting empty strings
> ---------------------------------------------------------------------------------
>
>                 Key: HV-435
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-435
>             Project: Hibernate Validator
>          Issue Type: Bug
>          Components: validators
>    Affects Versions: 4.1.0.Final
>         Environment: Hibernate Core 3.5.6 with MySql database (not relevant to this bug)
>            Reporter: Stephen Hiley
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> The PatternValidator currently checks for a null value and returns true from isValid() in that case. An empty string should have the same effect. This is necessary for the same reason: to allow for an empty form submission (which commonly are submitted as empty strings rather than nulls) and only validate when some value is submitted. IMO, this should be standard behavior. If nothing else, provide an option to treat empty strings as ignored in the same manner that null values are treated today.
> Suggested modification:
> public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
> 		if ( value == null || value.length() == 0) {
> 			return true;
> 		}
> Thanks,
> Stephen Hiley

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list