-1

I'm trying to create an ereg on a select option in object orientated programming for validation. Though it is always returning true even though the selected option isn't part of the ereg!

In my class I have the following:

function makeSelect($category, $fieldName){
    $a = '<div>'."\n";
    $a .= "<select name='$fieldName'>"."\n";
    $a .= "<option value='' selected='true' disabled='disabled'>Pick Category</option>\n";
    foreach ($category as $value){
        $a .= "<option value='$value'>$value</option>"."\n";
        }
    $a .= "</select>"."\n";
    $a .= "</div>"."\n";
    return $a;
}

function checkSelect($selectName){
    if(ereg('^[Pick]', $selectName)){
        $this->messageArray[$selectName] = "<span class='fail'>Please select field</span>";
        $this->testArray[$selectName] = false;
    }
    else {
        $this->messageArray[$selectName] = "<span class='ok'>Sweet</span>";
        $this->testArray[$selectName] = true;
    }
}

In my main file I'm setting the array as

$category = array(
    1=>'First Category',
    2=>'Second Category',
    3=>'Third Category',
    4=>'Fourth Category'
    );

And calling the checkSelect function as

$oForm->checkSelect('category');
sectus
  • 15,605
  • 5
  • 55
  • 97
breaker
  • 41
  • 1
  • 6
  • 3
    **Warning: This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.** –  Sep 26 '14 at 03:41

1 Answers1

0

With calling $oForm->checkSelect('category'); your expression would looks like ereg('^[Pick]', 'category') hence it would be always "true".

I think that you need to pass value from a form but not a name of element. For example.

$oForm->checkSelect($_POST['category']);

P.S. you could rewrite it to non deprecated code easily

preg_match('/^[Pick]/', $selectName)

P.P.S. it's better to rename $selectName to $selectedValue. You trying to check value, but a name.

P.P.P.S. also, your pattern would not match your values.

sectus
  • 15,605
  • 5
  • 55
  • 97