QF3.2 - 2 BC breaks with custom method!
| From: | Ignatius Reilly | Date: | Wed, 05 Nov 2003 19:46:04 +0000 |
| Subject: | QF3.2 - 2 BC breaks with custom method! | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-8787@lists.php.net to get a copy of this message | ||
OK. I struggled to upgrade to 3.2
The validation of custom functions seems to suffer at least two BC breaks:
The set-up:
---------------------
I had extended HTML_Quickform, and added a custom method:
is_ok_values( $elementName, $submitValue, $format = NULL )
this method is called back at validation time to check that an element's
values are within an array property.
To be more specific:
class MyQF extends HTML_QuickForm {
// This method checks that the passed values are within an array of
acceptable values
function is_ok_values( $elementName, $submitValue, $format = NULL ) {
// returns TRUE if $submitValue is in the $this->ok_values[$elementName]
array
)
// I register the is_ok_values() custom function as a method of the current
MyQF object
$this->registerRule( 'ok_values', 'function', 'is_ok_value', $this
) ;
// I create an element "myElt"
$this->addElement( 'select', 'myElt', 'select a gromit:', some
array ) ;
// I feed the ok_values property array with an array of values
$this->ok_values['myElt'] = some array ;
// I create a rule:
$this->addRule( "elt", "please select a valid value", "ok_values" )
;
...
}
Problem 1: "registry" implementation
---------------------
The new implementation creates a registry object at the first rule
registration (I think). This registry contains a copy of the QF object at
this time; therefore further modifications of the QF object are not
reflected.
In the case above, when I add new elements, the additional "ok values"
object property is not reflected in the registry.
I found an (ugly) way around this problem:
instead of doing
$this->addRule( "elt", "please select a valid value", "ok_values" )
;
doing
$this->rulesToAddAtValidateTime[] = '\$this->addRule( "elt", "please
select
a valid value", "ok_values" ) ;' ;
and at validation time call eval() on each "differed rule"
Problem 2: custom methods are not any more passed the element name!!
---------------------------
Old implementation: (Quickform.php, line 1495)
----
case 'function':
...
} elseif (method_exists($this, $ruleData[1])) {
return $this->$ruleData[1]($elementName, $submitValue,
$format);
Very nice. All custom methods must be passed the arguments ($elementName,
$submitValue, $format)
New implementation: (Callback.php, line 61)
----
function validate($value, $options = null)
...
if (isset($callback[1])) {
return call_user_func(array($callback[1], $callback[0]),
$value, $options);
I do not have access anymore to the element name!!
What would seem to be nice:
-----------------
1. at each new entry in the registry object, update the copy of the current
QF object (is this copy needed all along, BTW? I did not enquire deep
enough)
2. return to the previous implementation of custom functions and methods.
3. even better include this kind of validation, which I find quite useful
(ok, I read previous threads about including new validation rules...)
I'd be most delighted if somebody offered a workaround to problem 2. I
really would like to upgrade.