We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Forms - decorators, events, validators

Hi,

I'd like to address the author of Phalcon\Forms module.

Having studied most of other components of Phalcon, I was disappointed to see that Form rendering/validation does not emit any sort of events.

As much as I hate ZF Form module, I admit that it's easy to implement custom rendering and react to validation failures.

I am not sure that existing $form->renderDecorated($elementName) is a good idea. Elements should have one method to render (plus, __toString() magic).

I'd like to be able to receive the following notifications when rendering form/element:

  • beforeRender
  • afterRender
  • beforeValidation
  • onValidateSuccess
  • onValidateError

Obviously, Form should support __toString().

I'm sure you understand what I have mind. So, the question is whether there are any plans to improve Form functionality?

Regards, Temuri

Here is my implementation of decarated view of form elements:

<?php

/**
 * Form
 * @copyright Copyright (c) 2011 - 2013 Aleksandr Torosh (https://wezoom.com.ua)
 * @author Aleksandr Torosh <[email protected]>
 */

namespace Application\Form;

use \Phalcon\Forms\Element\Hidden;
use \Phalcon\Forms\Element\Check;

class Form extends \Phalcon\Forms\Form
{

    public function renderDecorated($name, $decorator = 'block')
    {
        $element = $this->get($name);

        $messages = $this->getMessagesFor($element->getName());

        $html = '';

        if (count($messages)) {
            $html .= '<div class="alert alert-danger">';
            foreach ($messages as $message) {
                $html .= $message;
            }
            $html .= '</div>';
        }

        if ($element instanceof Hidden) {
            echo $element;
        } else {
            switch ($decorator) {
                case 'block' : {
                        switch (true) {
                            default : {
                                    $html .= '<div class="control-group">';
                                    $html .= '<label for="' . $element->getName() . '">' . $element->getLabel() . '</label>';
                                    $html .= $element;
                                    $html .= '</div>';
                                    break;
                                }
                        }
                        break;
                    }
                case 'horizontal' : {
                        switch (true) {
                            case $element instanceof Check : {
                                    $html .= '<div class="control-group">';
                                    $html .= '<label class="checkbox">' . $element->getLabel();
                                    $html .= $element;
                                    $html .= '</label>';
                                    $html .= '</div>';
                                    break;
                                }
                            default : {
                                    $html .= '<div class="control-group">';
                                    $html .= '<label class="control-label" for="' . $element->getName() . '">' . $element->getLabel() . '</label>';
                                    $html .= '<div class="controls">';
                                    $html .= $element;
                                    $html .= '</div>';
                                    $html .= '</div>';
                                }
                        }
                        break;
                    }
            }
        }

        return $html;

    }

}

view:

<?php echo $form->renderDecorated('title','horizontal'); ?>
<?php echo $form->renderDecorated('text'); ?>


51.3k

Thanks. I was hoping that Phalcon\Form would trigger events that you could attach custom actions to.