Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 2.3.2

Request #2746 "in array" element rule
Submitted: 2004-11-13 09:00 UTC
From: ignatius dot reilly at free dot fr Assigned:
Status: Wont fix Package: HTML_QuickForm2
PHP Version: Irrelevant OS: irrelevant
Roadmaps: 0.1.0    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 38 + 19 = ?

 
 [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 ) ; } }

Comments

 [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...
 [2005-07-02 06:55 UTC] jausions
Then it would be good to have QuickForm_Rule_Enum showing up somewhere in the documentation... -Philippe
 [2006-06-08 13:17 UTC] avb (Alexey Borzov)
Moving feature requests to HTML_QuickForm2.
 [2006-10-20 22:28 UTC] avb (Alexey Borzov)
This rule won't be needed in QuickForm2: values of selects, checkboxes and radios will pass a sort of intrinsic validation, confirming that the value could possibly originate from this control. Validation rules will be applied to "clean" values after this intrinsic validation.