code K.O.
code K.O.

Reputation: 171

How to foreach form to volt in Phalcon?

Please advise me, how to foreach the form elements to volt template engine in Phalcon ?

This is my form class RegisterForm extends \Phalcon\Forms\Form :

 public function initialize(){

        $usmsName   = new Text('usmsName',[
            'placeholder'   => 'Full Name',
            'class'         => 'form-control no-border'
        ]);
        $this->add($usmsName);

        $usmsEmail      = new Email('usmsEmail',[
            'placeholder'   => 'E-mail',
            'class'         => 'form-control no-border'
        ]);
        $this->add($usmsEmail);
}

This is my view volt :

{{ form.label('usmsName', ['class':'col-sm-2 control-label']) }}
           <div class="col-sm-10">
                 {{ form.render('usmsName') }}
           </div>

     {{ form.label('usmsEmail', ['class':'col-sm-2 control-label']) }}
           <div class="col-sm-10">
                 {{ form.render('usmsEmail') }}
           </div>

Can i just loop the volt view..? how can i loop it? What i mean, how can i access the elements form and loop it into volt?

Upvotes: 3

Views: 2470

Answers (1)

Nikolay Mihaylov
Nikolay Mihaylov

Reputation: 3876

You can do it this way:

{% for field in form %}
    {{ field.label(['class':'col-sm-2 control-label']) }}
    {{ field.render() }}
{% endfor %}

When looping Phalcon Form object you iterate over each form element. This is really useful for generating common forms, you can just pass configuration and your form will do the rest.

Upvotes: 5

Related Questions