RequireIfMissing with lists

Aaron Spike <[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
I'm currently learning how to use FormEncode. While working with one
form I found that my validation schema would be less verbose and easier
to understand if RequireIfPresent accepted lists. So I wrote my own
implementation of RequireAllIfAnyPresent. But on reflection I can't
think of a reason why the included version couldn't be adapted to handle
lists. So attached is a patch against svn that attempts to implement a
version of RequireIfMissing/RequireIfPresent that optionaly accepts
lists of fields to validate. This has been only lightly tested and I am
unfamiliar with the code base so I welcome any testing or feedback. I
would love to see something like this included if people think it seems
reasonable.

Aaron Spike

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/

_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
requireifpresent_accept_lists.patch (text/plain, 2.1 KB)
Index: formencode/validators.py
===================================================================
--- formencode/validators.py	(revision 3126)
+++ formencode/validators.py	(working copy)
@@ -2444,15 +2444,42 @@
 
     def _to_python(self, value_dict, state):
         is_required = False
-        if self.missing and not value_dict.get(self.missing):
-            is_required = True
-        if self.present and value_dict.get(self.present):
-            is_required = True
-        if is_required and not value_dict.get(self.required):
-            raise Invalid('You must give a value for %s' % self.required,
-                          value, state,
-                          error_dict={self.required: Invalid(self.message(
-                              'empty', state), value, state)})
+        
+        if not isinstance(self.missing, list):
+            missing = [self.missing]
+        else:
+            missing = self.missing
+        if not isinstance(self.present, list):
+            present = [self.present]
+        else:
+            present = self.present
+        if not isinstance(self.required, list):
+            required = [self.required]
+        else:
+            required = self.required
+        
+        for item in missing:
+            if item and not value_dict.get(item):
+                is_required = True
+        for item in present:
+            if item and value_dict.get(item):
+                is_required = True
+        
+        requirements = {}
+        if is_required:
+            for item in required:
+                if item and not value_dict.get(item):
+                    requirements[item] = Invalid(self.message('empty', state), value_dict, state)
+        
+        if requirements:
+            keys = requirements.keys()
+            if len(keys) > 1:
+                name = '%s and %s' % (', '.join(keys[:-1]),keys[-1])
+            else:
+                name = keys[0]
+            msg = 'You must give a value for %s' % name
+            raise Invalid(msg, value_dict, state, error_dict=requirements)
+        
         return value_dict
 
 RequireIfPresent = RequireIfMissing
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.