Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Set the options for form elements #542

Closed
nikolasha opened this issue Apr 10, 2013 · 6 comments
Closed

[NFR] Set the options for form elements #542

nikolasha opened this issue Apr 10, 2013 · 6 comments

Comments

@nikolasha
Copy link

What do you think about the possibility to set options for form elements?

For example, I need to set the tooltips to form elements. Had to abandon the use of Phalcon\Forms\Element, because I have not figured out how to do it.

It would be nice to have a method to set custom options:

$element = new \Phalcon\Forms\Element\Text('name');
$element->setOption('tooltip', 'This field is required');

Template:

<form>
    <label><?= $element->getLabel() ?></label>
    <?= $element ?>
    <span><?= $element->getOption('tooltip') ?></span>
</form>
@phalcon
Copy link
Collaborator

phalcon commented Apr 10, 2013

Is this the same as #535?

@nikolasha
Copy link
Author

No, it's not the same thing. Attributes relate directly to the tag of the element.
If I do:

$element = new \Phalcon\Forms\Element\Text('name');
$element->setAttribute('tooltip', 'This field is required');

Then get:

<input type="text" name="name" tooltip="This field is required"> 

I need a method getAttribute in particular for the formation of the label:

<label for="<?= $element->getAttribute('id') ?>"><?= $element->getLabel() ?></label>

@phalcon
Copy link
Collaborator

phalcon commented Apr 16, 2013

This is implemented in 1.1.0, thanks

@phalcon phalcon closed this as completed Apr 16, 2013
@phalcon
Copy link
Collaborator

phalcon commented Apr 18, 2013

@nikolasha can we choose another name for 'options', this is because Phalcon\Forms\Element\Select already implements this option to set the select's html options and this could confuse users in the future...

@nikolasha
Copy link
Author

Yes, I've thought about it. For the select element better to rename the property "options" in the "optionValues" and the methods of the setOptionValues ​​and getOptionValues. But since you are, unfortunately, are afraid to change the API ;), then additional options can be given a name, for example, "customOptions".

@nikolasha
Copy link
Author

And do propose refactoring the component forms:

namespace Phalcon\Forms;

abstract class Element
{
    /**
     * @deprecated Here is quite unnecessary
     */
    protected $_form;

    /**
     * @deprecated It is better to keep in the array of attributes
     */
    protected $_name;

    /**
     * @deprecated It is better to keep in the array of options
     */
    protected $_label;

    protected $_attributes;

    protected $_validators;

    /**
     * Error messages should be attached to the element, not the form.
     * Now, to get the message for a particular element has to make a strange loop:
     * <?php
     * $messages = $element->getForm()->getMessagesFor($element->getName());
     * // must be:
     * $messages = $element->getMessages();
     * ?>
     */
    protected $_messages;

    /**
     * @var array custom options
     */
    protected $_options = array();

    /**
     * Value must be stored in the element.
     * Use for this purpose "Phalcon\Tag::setDefault()" is a monstrous magic voodoo ;)
     * By the way, "Phalcon\Tag" in general turned into a class-dump
     */
    protected $_value;

    /**
     * @param string|array $name the element name or configuration
     * @param string $label
     * @param array $attributes
     */
    public function __construct($name, $label = null, $attributes=null)
    {
        if (is_array($name)) {
            $config = $name;
            $name = $config['name']);

            if (isset($config['label']) {
                $label = $config['label']);
            }

            if (isset($config['attributes']) {
                $attributes = $config['attributes']);
            }

            if (isset($config['options'])) {
                $this->setOptions($config['options']);
            }
        }

        // ...
    }

    public function setName($name)
    {
        $this->setAttribute('name', $name);
        return $this;
    }

    public function getName()
    {
        return $this->getAttribute('name');
    }

    public function setLabel($label)
    {
        $this->setOption('label', $label);
        return $this;
    }

    public function getLabel()
    {
        $label = $this->getOption('label');
        if (null !== $label) {
            return $label;
        }
        return $this->getName();
    }

    public function setMessages($messages)
    {
        $this->_messages = $messages;
        return $this;
    }

    public function getMessages()
    {
        return $this->_messages;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant