Browse files

added the `craue_hasFormDeepErrors` function

  • Loading branch information...
1 parent 164b1ac commit 8ebfe4d20d93ed37645a9e9f53ad5b0ac1c1cac4 @craue committed Jan 8, 2012
Showing with 40 additions and 2 deletions.
  1. +3 −0 README.md
  2. +3 −1 Resources/config/twig/FormExtension.xml
  3. +34 −1 Twig/Extension/FormExtension.php
View
3 README.md
@@ -33,6 +33,9 @@ Provides the filters
Provides a mechanism to render a form several times on one page. This is done by cloning the form prior to rendering
using the `craue_cloneForm` function.
+It also provides the `craue_hasFormDeepErrors` function to check if a form (while taking into account all of its
+children) has any errors.
+
## StringHelperExtension
Provides the `craue_substr` function wrapping PHP's `substr` function.
View
4 Resources/config/twig/FormExtension.xml
@@ -11,6 +11,7 @@
<parameters>
<parameter key="craue_twig_extensions.form.class">Craue\TwigExtensionsBundle\Twig\Extension\FormExtension</parameter>
<parameter key="craue_twig_extensions.form.cloneFormAlias" />
+ <parameter key="craue_twig_extensions.form.hasFormDeepErrorsAlias" />
</parameters>
<services>
@@ -19,8 +20,9 @@
<call method="setFormFactory">
<argument type="service" id="form.factory" on-invalid="ignore" />
</call>
- <call method="setAlias">
+ <call method="setAliases">
<argument>%craue_twig_extensions.form.cloneFormAlias%</argument>
+ <argument>%craue_twig_extensions.form.hasFormDeepErrorsAlias%</argument>
</call>
</service>
</services>
View
35 Twig/Extension/FormExtension.php
@@ -26,17 +26,26 @@ class FormExtension extends \Twig_Extension {
*/
protected $cloneFormAlias = null;
+ /**
+ * @var string
+ */
+ protected $hasFormDeepErrorsAlias = null;
+
public function setFormFactory(FormFactoryInterface $formFactory) {
$this->formFactory = $formFactory;
}
/**
* @param string $cloneFormAlias Alias for the cloneForm function.
+ * @param string $hasFormDeepErrorsAlias Alias for the hasFormDeepErrors function.
*/
- public function setAlias($cloneFormAlias = null) {
+ public function setAliases($cloneFormAlias = null, $hasFormDeepErrorsAlias = null) {
if (!empty($cloneFormAlias)) {
$this->cloneFormAlias = $cloneFormAlias;
}
+ if (!empty($hasFormDeepErrorsAlias)) {
+ $this->hasFormDeepErrorsAlias = $hasFormDeepErrorsAlias;
+ }
}
/**
@@ -58,6 +67,12 @@ public function getFunctions() {
$functions[$this->cloneFormAlias] = $getCloneFormMethod;
}
+ $hasFormDeepErrorsMethod = new \Twig_Function_Method($this, 'hasFormDeepErrors');
+ $functions['craue_hasFormDeepErrors'] = $hasFormDeepErrorsMethod;
+ if (!empty($this->hasFormDeepErrorsAlias)) {
+ $functions[$this->hasFormDeepErrorsAlias] = $hasFormDeepErrorsMethod;
+ }
+
return $functions;
}
@@ -88,4 +103,22 @@ public function cloneForm($value, array $formOptions = array()) {
));
}
+ /**
+ * @param FormView $form A form.
+ * @return bool If the form (taking into account all of its children) has errors.
+ */
+ public function hasFormDeepErrors(FormView $form) {
+ if (count($form->get('errors')) > 0) {
+ return true;
+ }
+
+ foreach ($form->getChildren() as $child) {
+ if ($this->hasFormDeepErrors($child)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
}

0 comments on commit 8ebfe4d

Please sign in to comment.