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.

$element->setLabel() and css class

How do I add a css "class=whatever" to the Label?

In a Form class:

<?php
MyForm extends Form(
    public function initialize() {
        $email = new Text('email');
        $email->setLabel('email')
        // How do I add a css "class=whatever" to the above email label?
        //...
        $this->add($email);     
    }
);


4.6k
Accepted
answer

http://docs.phalconphp.com/en/latest/reference/forms.html

Hardcode like this

<form method="post">
    <?php
        // Проходим через форму
        foreach ($form as $element) {

            // Собираем все сгенерированные сообщения для текущего элемента
            $messages = $form->getMessagesFor($element->getName());

            if (count($messages)) {
                // Выводим каждый элемент
                echo '<div class="messages">';
                foreach ($messages as $message) {
                    echo $message;
                }
                echo '</div>';
            }

            echo '<p>';
            echo '<label for="', $element->getName(), '">', $element->getLabel(), '</label>';
            echo $element;
            echo '</p>';

        }
    ?>
    <input type="submit" value="Send"/>
</form>

Or you can try to create own \Phalcon\Tag, pass it to Di and rewrite lable method maybe it can helps you



8.5k

Yes, I know about this, thank you. I thought there is an array $option or something which I am missing, but seems not. Will make my own Tag then.



8.5k

After checking the code, it actually can be done, pass the array with options to the label().

In Volt:

<?php
form.label('email', ['class': 'whatever']) 

or in PHP

<?php
$form->label('email', ['class' => 'whatever']);

Yeap, but we was talking about Form Elements as I known.