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: 2363 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.
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
|
|
|
|
|
|
Re: Enum Type and 0 index. [message #398 is a reply to message #397] |
Tue, 14 November 2006 08:56 |
AJM
Messages: 2363 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.
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
|
|
|
|
Re: Enum Type and 0 index. [message #401 is a reply to message #399] |
Tue, 14 November 2006 09:46 |
AJM
Messages: 2363 Registered: April 2006 Location: Surrey, UK
|
Senior Member |
|
|
If you know that your database will always be MySQL then you can continue to use ENUM fields. If you ever decide to use another DBMS which does not have ENUM fields then you will have a problem.
I do not plan to remove support for ENUM fields from Radicore so you can carry on using them. I stopped using them in my sample application as I needed a version that could also run on PostgreSQL. This means that I can switch from MySQL to PostgreSQL and back again just by changing one line in the CONFIG.INC file. No code anywhere else needs to change.
I shall update the FAQ to include information regarding ENUM fields.
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
|
|
|
|
|