Reputation: 2363
I am using a PHP validation script that only supports a single-dimensional array.
How can this switch case be modified so a multi-dimensional array will work?
Currently, it just shows a select field as being empty, when it actually has a selected value.
Switch case:
case 'in-array' :
if (!(in_array($value, $rule->criteria))) {
$this->_errors[$rule->fieldname] = $rule->message;
return;
}
break;
Call to require validation on State field:
$validator->addRule('state', 'Please select a state', 'in-array', $states);
$validator->addEntries($_POST);
$validator->validate();
$entries = $validator->getEntries();
foreach ($entries as $key => $value) {
${$key} = $value;
}
States array:
$states = array('AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District Of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho');
Upvotes: 0
Views: 96
Reputation: 12433
It looks like your $value
is the array key, not the array value. Try adding array_key_exists
to your case 'in-array'
if -
if (!(in_array($value, $rule->criteria)) && !(array_key_exists($value, $rule->criteria)))
Upvotes: 1