Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Forms - Elements - User Options

Hi again,

yes, today I've got many questions. Sorry for that, but I cleaning up my code and want to improve it.

My next question is about User Options of an form-element.

In the docs there is nothing said about the functionality: http://docs.phalconphp.com/en/latest/reference/forms.html#setting-user-options (paragraph is empty)

I use classes to build my forms and I've written a base-class with a function renderDecorated that does the right html-code for bootstrap.

I wanted to be able to provide helptext for form-elements and instead of extended every element-class of phalcon, I thought: Why not doing something like this in the form-class:

private function elementBaseUri ($value) { $element = new Text('baseUri', array( 'id' => 'baseUri', 'required' => 'required', 'class' => 'form-control', 'value' => $value, 'placeholder' => $this->translate->("FORMSSYSTEMSETTINGSBASEURIPLACEHOLDER") )); $element->setLabel($this->translate->("FORMSSYSTEMSETTINGSBASEURILABEL")); $element->setUserOption('helptext', 'FORMSSYSTEMSETTINGSBASEURIHELPTEXT'); $element->addValidators(array( new PresenceOf(array( 'message' => $this->translate->('ERRORFORMSVALIDATIONFIELD_PRESENCEOF', array('field'=>'baseUri')) )) )); return $element; }

And my renderDecorated-method does something like this:

if ($this->getUserOption('helptext', NULL) != NULL) echo "\t", '<p class="help-block">', $this->getUserOption('helptext'), '</p>', PHP_EOL;

But a var_dump says me, that getUserOption returns NULL.

So for what is setUserOption good for?

Thx alot - again



1.5k
Accepted
answer
edited Feb '15
public function initialize($user, $options)
    {

        if ($options['edit']) {
            $this->add(new Hidden('id'));
        } else {
            $this->add(new Text('id'));
        }

        $this->add(new Text('name'));
    }

user options are more in line with that

basically you can give parameters to the forms to trigger it to react differently. Eg User form with an options to change it from new/edit/delete, all under one class called UserForm but generates 3 different forms depending on user options