Symfony2 and layout / configuration (required / help / errors)

Maybe I'm missing something, and I hope this is done very easily.

I have a form, and in the end I want to get the following result:

Fields that:

  • are required / required
  • there is a mistake now
  • have help

should get an extra a-Tag after the label and an extra div filled with help and / or an error, if applicable.

What I need is that the required fields get a-Tag using this:

{% use 'form_div_layout.html.twig' with field_label as base_field_label %}

{% block field_label %}
    {{ block('base_field_label') }}

    {% if required %}
        <a href=""><span>&nbsp;</span></a>
    {% endif %}
{% endblock %}

So, I already tried different versions of this:

{% use 'form_div_layout.html.twig' with field_label as base_field_label %}

{% block field_label %}
    {{ block('base_field_label') }}

    {% if required or help is defined %}
        <a href=""><span>&nbsp;</span></a>
    {% endif %}
{% endblock %}

{% block field_row %}
    {% spaceless %}
        <div class="row">
            {% if required or help is defined %}
                <div>
                    {{ form_errors(form) }}
                    {{ help }}
                </div>
            {% endif %}

            {{ form_label(form) }}
            {{ form_widget(form, { 'attr': {'class': 'grid_4'} }) }}
        </div>          
    {% endspaceless %}
{% endblock field_row %}

And I can't get this to work.

So my questions are:

  • Where can I get help text that may also contain HTML? I tried this in form builderwithout success - but at least with the exception of:

    $builder    ->add('subject', 'text', array(
        'label' => 'Subject',
        'help' => 'Can be formatted content with <strong>HTML-Elements</strong>',
        ));
    
  • , ( ), , ? {{ form_errors(form) }} , , field_row.

+5
2

, .

SF 2.1 1:

namespace Webility\Bundle\WebilityBundle\Form\Extension;

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormViewInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class HelpFormTypeExtension extends AbstractTypeExtension
{

    public function buildView(FormViewInterface $view, FormInterface $form, array $options){
        $view->setVar('help', $options['help']);
    }

    public function getExtendedType(){
        return 'field';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'help' => null
        ));
    }
}

:

<service id="webility.form.extension.help" class="Webility\Bundle\WebilityBundle\Form\Extension\HelpFormTypeExtension">
        <tag name="form.type_extension" alias="field" />
</service> 

: ? , , :

echo '<pre>'; print_r( $form->getErrorsAsString() ); echo '</pre>'; exit;
+6

, Maciej Pyszyński anwser, .

-, . "" "" :

formbuilder twig.

'' -tags ( divs )...

{% block field_label %}
    {{ block('base_field_label') }}

    {% if attr.class is defined and '_hint' == attr.class %}
        <div>
            <a><span class="help">Help Icon</span></a>
            <div class="tooltip">
                {% if help is defined %}
                    {{ help|raw }}
                {% else %}
                    Somebody forgot to insert the help message
                {% endif %}
            </div>
        </div>
    {% endif %}
{% endblock %}

{% block field_row %}
    {% spaceless %}
        <div class="row{% if form_errors(form) %} error{% endif %}">
            {{ form_label(form) }}
            {{ form_widget(form, { 'attr': {'class': 'grid_4'} }) }}
        </div>
    {% endspaceless %}
{% endblock field_row %}

<div class="row{% if form_errors(form.url) %} _error{% endif %}">
    {{ form_label(form.field, null, { 'attr': {'class': '_hint'}, 'help': 'Help text or variable containing it' }) }}
    {{ form_widget(form.field, { 'attr': {'class': 'grid_4'} }) }}
</div>
0

All Articles