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

form label

When a checkbox it's created via php, example:

        // Remember
        $remember = new Check('remember', array(
            'value' => 'yes'
        ));

        $remember->setLabel('Remember me');

        $this->add($remember);

then html generated is:

<input id="remember" type="checkbox" value="yes" name="remember">
<label for="remember">remember</label>

How to generate an html structure like:

<label class="checkbox" for="remember">
  <input type="checkbox" value="" id="remember" data-toggle="checkbox">
  Checkbox
</label>

Thanks!!



8.1k

Well, how it's rendered actually depends on the code that is responsible for rendering the element and it's label.

You can play e.g. with getLabel https://docs.phalcon.io/en/latest/reference/forms.html#rendering-forms

@renskii thanks for your reply!

The form it's rendered with .volt template engine, using:

                    {{ form.render('terms') }} 
                    {{ form.label('terms') }}
                    {{ form.messages('terms') }}

and this is php code:

        $terms = new Check('terms', array(
            'value' => 'yes'
        ));

        $terms->setLabel('Accept terms and conditions');

        $terms->addValidator(new Identical(array(
            'value' => 'yes',
            'message' => 'Terms and conditions must be accepted'
        )));

        $this->add($terms);

I am playing with vokuro sample.

Since this use bootstrap, bootstrap framework require below html markup:

<div class="checkbox">
        <label>
          <input type="checkbox"> Remember me
        </label>
      </div>

I can't add into html directly markup because there is validator.

There is a workaround?

edited Jun '14

Work fine if I add php code:

        $terms = new Check('terms', array(
            'value' => 'yes'
        ));
        $terms->setLabel('Accept terms and conditions');
        $terms->addValidator(new Identical(array(
            'value' => 'yes',
            'message' => 'Terms and conditions must be accepted'
        )));
        $this->add($terms);

and on view file directly html markup:

                        <div class="form-group">
                            <label class="checkbox" for="terms">
                              <input type="checkbox" value="yes" id="terms" data-toggle="checkbox">
                              Accept terms and conditions
                            </label>
                            <span class="help-block">{{ form.messages('terms') }}</span>
                        </div>

The validation work fine.

I guess if exist a way to wrap checkbox into label using form class.



8.1k
Accepted
answer
edited Jun '14

Hi @macagoraga,

I actually use something like the renderDecorated example from the docs. That way you can e.g. use {{ form.renderDecorated('whatevva') }} so your element + label get rendered at once.

You could e.g. let renderDecorated have another param, e.g. to dictate wheter the input should be inside the label or not. Or you could get check the class name of the element inside that method and act on that... Or use the Element user options..

A lot of ways you can deal with this. I base all my forms on my own class that extends Phalcon\Forms\Form

AFAIK there is no out-of-the-box way for what you want.

actually use something like the renderDecorated

Thanks! I looking for something like this. I will definitely check in this direction.

Best regards