Home » HTML » HTML_QuickForm2 » Bug #2746
"in array" element rule
Details
| Request #2746 | "in array" element rule |
|---|---|
| Submitted | 2004-11-13 09:00 UTC |
| From | ignatius dot reilly at free dot fr |
| Status | Wont fix |
| Package | HTML_QuickForm2 |
| PHP Version | Irrelevant |
| OS | irrelevant |
| Roadmaps | 0.1.0 |
Comments
[2004-11-13 09:00 UTC] ignatius dot reilly at free dot fr
Description:
------------
The one element rule I use the most is a rule checking that the passed value is in an array.
It is so useful I think it should make it into the system-provided rules.
It applies to all "keyed" elements:
- radio
- checkboxes
- select
Reproduce code:
---------------
// Register "in array" rule
$this->registerRule( "ok_values", "callback", "qf_in_array" ) ;
// Country
$values = list_countries() ;
$this->addElement( "select", "country", "Select country", $values ) ;
$this->addRule( "country", "Please select a valid country", "ok_values", array_keys( $values ) ) ;
// ---------------------
function qf_in_array( $submitValue, $okValues ) {
/*
Validates that passed value belongs to a parameter array $okValues
Two cases:
1. "Single" element: $submitValue = scalar, okValue = simple array
2. "Array" element (eg hierselect): $submitValue = array,
okValue = array of simple arrays
*/
if ( is_array( $submitValue ) ) {
foreach ( $submitValue as $key => $value ) {
if ( !in_array( $value, $okValues[$key] ) ) {
return FALSE ;
}
}
return TRUE ;
} else {
return in_array( $submitValue, $okValues ) ;
}
}
[2004-11-13 10:42 UTC] bmansion at mamasam dot com
I have a rule object reeady for that, it is called HTML_QuickForm_Rule_Enum. I don't think client validation is worthwile for this rule as if someone starts to change what the radios can submit, he can obviously also change the javascript.
[2004-11-13 10:58 UTC] ignatius dot reilly at free dot fr
I agree:
- pointless to do client validation
- easy to implement oneself
My point was that this rule generally is so useful that is seems to me worth including in the next QF version as a "built-in" validation rule.
I know you are generally (rightly) skeptical of such requests, but consider...