Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DracoBlue/3181010 to your computer and use it in GitHub Desktop.
Save DracoBlue/3181010 to your computer and use it in GitHub Desktop.
Enables usage of errors in validator definitions in agavi validation
Index: config/AgaviValidatorConfigHandler.class.php
===================================================================
--- config/AgaviValidatorConfigHandler.class.php (revision 731)
+++ config/AgaviValidatorConfigHandler.class.php (working copy)
@@ -70,10 +70,11 @@
foreach($cfg->get('validator_definitions') as $def) {
$name = $def->getAttribute('name');
if(!isset($this->classMap[$name])) {
- $this->classMap[$name] = array('class' => $def->getAttribute('class'), 'parameters' => array());
+ $this->classMap[$name] = array('class' => $def->getAttribute('class'), 'parameters' => array(), 'errors' => array());
}
$this->classMap[$name]['class'] = $def->getAttribute('class',$this->classMap[$name]['class']);
$this->classMap[$name]['parameters'] = $def->getAgaviParameters($this->classMap[$name]['parameters']);
+ $this->classMap[$name]['errors'] = $def->getAgaviErrors($this->classMap[$name]['errors']);
}
}
@@ -122,7 +123,7 @@
if(!class_exists($class)) {
throw new AgaviValidatorException('unknown validator found: ' . $class);
}
- $this->classMap[$class] = array('class' => $class, 'parameters' => array());
+ $this->classMap[$class] = array('class' => $class, 'parameters' => array(), 'errors' => array());
} else {
$class = $this->classMap[$validator->getAttribute('class')]['class'];
}
@@ -134,7 +135,6 @@
);
$arguments = array();
- $errors = array();
$stdMethod = $validator->getAttribute('method', $stdMethod);
$stdSeverity = $parameters['severity'];
@@ -168,6 +168,7 @@
}
}
+ $errors = $this->classMap[$validator->getAttribute('class')]['errors'];
foreach($validator->get('errors') as $error) {
if($error->hasAttribute('for')) {
$errors[$error->getAttribute('for')] = $error->getValue();
Index: config/xsd/parts/validators.xsd
===================================================================
--- config/xsd/parts/validators.xsd (revision 731)
+++ config/xsd/parts/validators.xsd (working copy)
@@ -118,8 +118,10 @@
</xs:group>
<xs:complexType name="validator_definition">
- <xs:sequence>
+ <xs:sequence maxOccurs="unbounded">
<xs:group ref="envelope_1_0:parameters" />
+ <xs:group ref="errors"
+ minOccurs="0"/>
</xs:sequence>
<xs:attribute name="name" type="types_1_0:non_empty_string" use="required" />
<xs:attribute name="class" type="types_1_0:php_class" use="required" />
Index: config/util/dom/AgaviXmlConfigDomElement.class.php
===================================================================
--- config/util/dom/AgaviXmlConfigDomElement.class.php (revision 731)
+++ config/util/dom/AgaviXmlConfigDomElement.class.php (working copy)
@@ -492,6 +492,59 @@
return $result;
}
+
+ /**
+ * Check whether or not the element has Agavi errors as children.
+ *
+ * @return bool True, if there are errors, false otherwise.
+ *
+ * @author Jan Schütze <JanS@DracoBlue.de>
+ * @author Steffen Gransow <agavi@mivesto.de>
+ *
+ * @since 1.0.x
+ */
+ public function hasAgaviErrors()
+ {
+ if($this->ownerDocument->isAgaviConfiguration()) {
+ return $this->has('errors', AgaviValidatorConfigHandler::XML_NAMESPACE);
+ }
+
+ return false;
+ }
+
+ /**
+ * Retrieve all of the Agavi error elements associated with this
+ * element.
+ *
+ * @param array An array of existing errors.
+ *
+ * @return array The complete array of errors.
+ *
+ * @author Jan Schütze <JanS@DracoBlue.de>
+ * @author Steffen Gransow <agavi@mivesto.de>
+ *
+ * @since 1.0.x
+ */
+ public function getAgaviErrors(array $existing = array())
+ {
+ $result = $existing;
+ $offset = 0;
+
+ if($this->ownerDocument->isAgaviConfiguration()) {
+ $elements = $this->get('errors', AgaviValidatorConfigHandler::XML_NAMESPACE);
+
+ foreach($elements as $element) {
+ $key = '';
+ if($element->hasAttribute('for')) {
+ $key = $element->getAttribute('for');
+ }
+
+ $result[$key] = $element->getValue();
+ }
+ }
+
+ return $result;
+ }
}
-?>
\ No newline at end of file
+?>

Definition of the Validator for Password:

<validator_definitions>
	<validator_definition name="password" class="AgaviStringValidator">
        <errors>
            <error for="min">Passwort muss mindestens 6 Zeichen haben.</error>
            <error>Kein Passwort gewählt!</error>
        </errors>
	    <ae:parameters>
	        <ae:parameter name="min">6</ae:parameter>
	    </ae:parameters>
	</validator_definition>
</validator_definitions>

Usage:

<validator class="password">
    <argument>password</argument>
    <errors>
        <error>Bitte ein Passwort auswählen.</error> <!-- Overwrites the "Kein Passwort gewählt!" from the definition -->
    </errors>
</validator>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment