From 467d9b60095d50db192819bb20bc52833aaa7227 Mon Sep 17 00:00:00 2001
From: Damien Regad <damien.regad@merckgroup.com>
Date: Mon, 30 Jul 2012 00:03:55 +0200
Subject: [PATCH] Custom field validation should pass when empty and min
 length > 0

Fix for #12029 (commit 9c71d4f884200f682636e772cf3584202fc2443d)
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
---
 core/custom_field_api.php |   20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/core/custom_field_api.php b/core/custom_field_api.php
index a6faa8a..fd89533 100644
--- a/core/custom_field_api.php
+++ b/core/custom_field_api.php
@@ -1157,21 +1157,29 @@ function custom_field_validate( $p_field_id, $p_value ) {
 			$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 even if minimum length > 0
+			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 even if minimum length > 0
+			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
-- 
1.7.9.5

