QF::QuickForm "date" Jscript validation
| From: | Andy Whitworth | Date: | Sun, 18 Apr 2004 13:39:32 +0000 |
| Subject: | QF::QuickForm "date" Jscript validation | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-11940@lists.php.net to get a copy of this message | ||
Hi,
I'm using the client-side validation option for a QuickForm "date"
element. If I create a "required" client-side validation rule then this
fails to recognise an "empty" date field i.e. one where I've specified
addEmptyOption' => TRUE and have left the input dropdowns as
the empty values. I've looked at the generated Javascript for the
clientside validation and it looks buggy. Anyone have the same
problem ?
// Create the date element as follows
$form->addElement('date', 'dob', 'Date of Birth',
array('format'=>'dFY',
'minYear'=>1900, 'maxYear'=>2004,
'addEmptyOption' => TRUE, 'emptyOptionText'=>'select',
'emptyOptionValue' => ''));
// Specify the client-side validation
$form->addRule('dob', 'Date of Birth is a required field',
'required',
null, 'client');
When I call $form->validate() then is doesn't pick up on an "empty" date
field.
The generated Javascript is:-
var dobElements = '::dob[d]::dob[F]::dob[Y]::';
value = new Array();
var valueIdx = 0;
for (var i = 0; i < frm.elements.length; i++) {
var _element = frm.elements[i];
if (dobElements.indexOf('::' + _element.name + '::') >= 0) {
switch (_element.type) {
case 'checkbox':
case 'radio':
if (_element.checked) {
value[valueIdx++] = _element.value;
}
break;
case 'select':
if (-1 != _element.selectedIndex) {
value[valueIdx++] =
_element.options[_element.selectedIndex].value;
}
break;
default:
value[valueIdx++] = _element.value;
}
}
}
if (value == '' && !errFlag['dob']) {
errFlag['dob'] = true;
_qfMsg = _qfMsg + '\n - Date of Birth is a required field';
}
and the switch(_element.type) construct is falling into the"default" case
for each of the three date fields. As you cansee this will never result in
the subsequent error checkingcode giving an error match as "value" will not
be equal to ''.
Help!!
Andy.