Search code examples
csstwitter-bootstrap-3phalconvolt

Form Theme / Template in Phalcon


Having ditched Symfony in favor of Phalcon I have stumbled across a problem regarding form templates.

I understand for example a field is rendered in this way:

{{ text_field("name", "size" : 30) }}

{{ submit_button("Save") }}

and if I wanted to apply a class to this field it would look like:

{{ text_field("name", "size" : 30, "class" : "classForField") }}

{{ submit_button("Save", "class" : "btn btn-success") }}

However doing this for the 100's of fields, buttons etc in my app would be a massive hassle, especially if I decided for some reason to change the Css clas for just 'text_fields' for example.

In Symfony you would define a basic template for all fields in a template file which in turn would be used by all forms unless overridden.

I've looked through many of the sample projects, and through the documentation but I can't find such a feature in Phalcon. I'm hoping this is just me looking for the wrong terminology could someone point me in the right direction please.

Specifically I want to apply Bootstrap classes to my elements.


Solution

  • You may want to use Phalcon\Forms.

    You can use Form objects to declare, generate and than validate you forms. Based to manual example you can add attributes to fields during Form::add like this:

    $this->add(new Text("name", array(
        'maxlength' => 30,
        'placeholder' => 'Type your name',
        'class' => 'css classes'
    )));
    

    It takes some time to learn manipulating those forms, especially when it goes to sanitization+validation. Anyway I would recommend to make your extensions for al fields in \Phalcon\Forms\Element\*, where you can declare your default css, being able to adjust them during both element creation in Form, and later during Volt template.