Home » RADICORE development » Bug Reports » Enum Type and 0 index.
Enum Type and 0 index. [message #391] |
Mon, 13 November 2006 08:19  |
johandewit
Messages: 25 Registered: May 2006 Location: Belgium
|
Junior Member |
|
|
File : includes/std.validation.class.inc
Funtion : validatefield
if ($fieldspec['type'] == 'enum') {
debugbreak();
// get enum array for this field
$enum = $this->caller->getValRep($fieldname);
// if we already have the value do not replace it
if (!in_array($fieldvalue, $enum)) {
// replace index number with text value
$fieldvalue = $enum[$fieldvalue];
} // if
} // if
Problem ; If $fieldvalue=0, where 0 = array_index, the function in_array returns TRUE instead of FALSE. The fieldarray is not replaced with the $enum[0] value, resulting in validation error.
See also http://be.php.net/manual/en/function.in-array.php#61491
Changing line 111 to if (!in_array($fieldvalue, $enum, true))
solves this, and the fieldvalue is rpelaced with $enum[0] as intended.
At the end of this function, the enum type is passed to the validateNumber, (default case branch) which always results in an error message.
I've attached a patch against version 1.17.0 which makes the enum field type working (in my situation).
Greetings
Johan
|
|
|
Re: Enum Type and 0 index. [message #394 is a reply to message #391] |
Mon, 13 November 2006 12:57   |
AJM
Messages: 2382 Registered: April 2006 Location: Surrey, UK
|
Senior Member |
|
|
I created a field with the type enum('one', 'two', 'three') and tested it with both NULL and NOT NULL. The field was displayed as a dropdown list, and in one test I obtained the values using function _cm_getValRep ($item='', $where)
{
if ($item == 'enum_test') {
$array = $this->getEnum($item);
return $array;
} // if
}
and in another test using
function _cm_getValRep ($item='', $where)
{
if ($item == 'enum_test') {
$array['one'] = 'un';
$array['two'] = 'deux';
$array['three'] = 'trois';
return $array;
} // if
}
In neither test did I have the need to change line 111 to
if (!in_array($fieldvalue, $enum, true))
I did see, however, the need to insert
before
default:
// perform validation if field type is numeric (integer, decimal)
$fieldvalue = $this->validateNumber($fieldname, $fieldvalue, $fieldspec);
} // switch
I also found and fixed a problem in file 'include.xml.php4/5.inc' which inserted a blank entry into the lookup array even if there was one already there. My changed files are attached.
|
|
|
|
|
|
Re: Enum Type and 0 index. [message #398 is a reply to message #397] |
Tue, 14 November 2006 08:56   |
AJM
Messages: 2382 Registered: April 2006 Location: Surrey, UK
|
Senior Member |
|
|
The correct way to load an ENUM field is to put the following code in the _cm_getValRep() method:
if ($item == 'enum_field') {
$array = $this->getEnum($item);
} // if
This obtains the array of values from the database, but is indexed from 1 not 0.
I think I should change the export function so that when it creates the array of values in the ?.dict.inc file it forces the first index to be 1, as follows:
'values' => array(1 => 'entered', 'modified', 'submitted', 'approved', 'rejected', 'deleted')
The reason that I stopped using ENUM fields in my code is that Radicore has to support other databases, and ENUM fields are unique to MySQL.
|
|
|
|
|
|
|
Goto Forum:
Current Time: Wed Jun 18 15:01:12 EDT 2025
Total time taken to generate the page: 0.01301 seconds
|