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 - 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: https://docs.phalcon.io/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->("FORMS_SYSTEMSETTINGS_BASEURIPLACEHOLDER") )); $element->setLabel($this->translate->("FORMS_SYSTEMSETTINGS_BASEURI_LABEL")); $element->setUserOption('helptext', 'FORMS_SYSTEMSETTINGS_BASEURIHELPTEXT'); $element->addValidators(array( new PresenceOf(array( 'message' => $this->translate->('ERROR_FORMS_VALIDATION_FIELD_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



2.1k
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