Jump to content

validating a dropdown! Need some help here


kee2ka4

Recommended Posts

Hey peeps,

 

I use two functions called

 

1. element_value($error, $row, $param) that returns the correct value for a form element!

Here is the code:

/**
 * returns the correct value for our form element
 * @param bool $error
 * @param string $row
 * @param string $param
 * @return string
 */
function element_value($error, $row, $param)
{
	if ($error){ return ''; }
	if ($row) {  return $row;  }
	if ($param) { return $param; }
	return '';
}

 

and 2. error_msg($error, $msg) which returns the error message of a form element if there is an error. Here is the code:

 function error_msg($error, $msg)
  {
    if($error) { return $msg; }
	return '';
  }

 

My question is, this works fine with an input box or a text box field but I cannot make it to work for a dropdown. So if I don't make a selection for a dropdown, it does not echo out the error. Here is my form element...

Code for the input box, this validates correctly!

div class="elementPost">
<label for="title" class="smlText"><strong>Title: <img src="/public/images/required.gif" width="8" height="8" /> </strong><span class="helpMsg"> <?php echo error_msg($errors['title'], 'Oops! Title must be atleast 5 chars.');  ?></span></label>
<br/>
<input class="textpost" type="text" value="<?php echo element_value($errors['title'], $postAd['title'], $params['postAd']['title']);  ?>" name="postAd[title]" onFocus="javascript:toggleMsg('msg-2')" onBlur="javascript:toggleMsg('msg-2')"/> 
<span id="msg-2" class="smallmsg" style="visibility:hidden;">Minimum 5 characters </span>
</div>

 

Form element for the dropdown - this doesnt work,  it doesn't give out the error when I do not make a selection in the dropdown:

<div class="elementPost">
<label for="email" class="smlText"><strong>Country: <img src="/public/images/required.gif" width="8" height="8" /> </strong><span class="helpMsg"> <?php echo error_msg($errors['cid'], 'Oops! Please select a Country.');  ?></span></label>
<br/>
<select name="postAd[cid]">
<option value="">Select Country</option>
<?php $countryList = getCountryList(); foreach($countryList as $countryName): ?>
<option value="<?php echo element_value($errors['cid'], $countryName['cid'], $params['postAd']['cid']);  ?>" <?php echo (($countryName['cid'] == $params['postAd']['cid']) ? ' selected="selected"' : null) ?>><?php echo $countryName['country_name'] ?></option>
<?php endforeach; ?>
</select>
</div>

 

Any Ideasss?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.