Permalink
Browse files

Custom field validation should pass when empty and min length > 0

Fix for #12029 (commit 9c71d4f)
introduced a regression in the validation of numeric custom fields when
the field's minimum length is greater than 0 and the data being
validated is empty. This prevents e.g. reporting of new issues with a
hidden custom field, or updating issues having a non-required, empty
field.

This commit resolves the problem by passing validation when the field is
empty. This should not cause problems for required fields, as that case
is covered by an earlier check in bug_report.php and bug_update.php.

Fixes #14516
  • Loading branch information...
1 parent 79e4d02 commit 9094a34d71f2bbf14bb57ff22035fa427da5adc5 @dregad dregad committed Jul 29, 2012
Showing with 19 additions and 7 deletions.
  1. +19 −7 core/custom_field_api.php
View
@@ -1148,30 +1148,42 @@ function custom_field_validate( $p_field_id, $p_value ) {
$t_length = utf8_strlen( $p_value );
switch ($t_type) {
case CUSTOM_FIELD_TYPE_STRING:
+ # Empty fields are valid
@atrol

atrol Jul 31, 2012

Member

This introduces another issue.
Now I am able to edit an issue and to clear a custom text field also when Min. Length is set > 0

+ if( $t_length == 0 ) {
+ break;
+ }
# Regular expression string validation
- if( !is_blank( $t_valid_regexp ) && !is_blank( $p_value ) ) {
+ if( !is_blank( $t_valid_regexp ) ) {
$t_valid &= preg_match( "/$t_valid_regexp/", $p_value );
}
# Check the length of the string
$t_valid &= ( 0 == $t_length_min ) || ( $t_length >= $t_length_min );
$t_valid &= ( 0 == $t_length_max ) || ( $t_length <= $t_length_max );
break;
case CUSTOM_FIELD_TYPE_NUMERIC:
- $t_valid &= ( $t_length == 0 ) || is_numeric( $p_value );
-
+ # Empty fields are valid
+ if( $t_length == 0 ) {
+ break;
+ }
+ $t_valid &= is_numeric( $p_value );
+
# Check the length of the number
$t_valid &= ( 0 == $t_length_min ) || ( $t_length >= $t_length_min );
$t_valid &= ( 0 == $t_length_max ) || ( $t_length <= $t_length_max );
-
+
break;
case CUSTOM_FIELD_TYPE_FLOAT:
+ # Empty fields are valid
+ if( $t_length == 0 ) {
+ break;
+ }
# Allow both integer and float numbers
- $t_valid &= ( $t_length == 0 ) || is_numeric( $p_value ) || is_float( $p_value );
-
+ $t_valid &= is_numeric( $p_value ) || is_float( $p_value );
+
# Check the length of the number
$t_valid &= ( 0 == $t_length_min ) || ( $t_length >= $t_length_min );
$t_valid &= ( 0 == $t_length_max ) || ( $t_length <= $t_length_max );
-
+
break;
case CUSTOM_FIELD_TYPE_DATE:
# gpc_get_cf for date returns the value from strftime

0 comments on commit 9094a34

Please sign in to comment.