Frage

My problem is pretty simple, but i really don't know what to do to solve it.

Here the situation :

In symfony2 (2.3.*) i've created a form (DocumentType.php + twig render template). A file form is included. There are some classic fields (text, choices) plus one file input. This file input is pretty particular, I'm using a little hack to custom the element's render attributes.

Here the EntityType class :

class DocumentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'documentName',
                'text',
                array(
                    'label' => 'Nom du document'
                )
            )
            ->add(
                'documentVersionCustom',
                'text',
                array(
                    'required' => false,
                    'label' => 'Version personnalisée'
                )
            )
            ->add(
                'documentStatus',
                'choice',
                array(
                    'choices' => Document::getStatusChoices(),
                    'attr' => array(
                        'class' => 'select2',
                    ),
                    'label' => 'Etat'
                )
            )
            ->add(
                'file',
                'file',
                array(
                    'mapped' => false,
                    'attr' => array(
                        'class' => 'hidden'
                    ),
                    'label' => 'Fichier',
                    'required' => true
                )
            );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        [...]
    }

    public function getName()
    {
        [...]
    }
}

Here the Twig template (partial):

             <div class="row">
                {{ form_errors(form) }}
                <legend class="span12">Informations du fichier</legend>

                {{ form_label(form.documentName,'Nom du fichier',{'label_attr':{'class':'span4' }}) }}
                {{ form_errors(form.documentName) }}
                {{ form_widget(form.documentName,{'attr':{'class':'span8' }}) }}

                {{ form_label(form.documentVersionCustom,'Version personnalisée',{'label_attr':{'class':'span4' }}) }}
                {{ form_errors(form.documentVersionCustom) }}
                {{ form_widget(form.documentVersionCustom,{'attr':{'class':'span8' }}) }}

                {{ form_label(form.documentStatus,'État',{'label_attr':{'class':'span4' }}) }}
                {{ form_errors(form.documentStatus) }}
                <div class="row">
                    {{ form_widget(form.documentStatus,{'attr':{'class':'span8 select2' }}) }}
                </div>

                <div class="row">
                    {#{{ form_label(form.file,'Fichier',{'label_attr':{'class':'span4' }}) }}#}
                    {{ form_errors(form.file) }}
                    {{ form_widget(form.file,{'attr': {'style': 'display:none;' },'id' : 'browseFile' }) }}
                    <label for="browseFile" class="span4">Fichier</label>

                    <div class="row">
                        <div class="input-append span8">
                            <input type="text" name="subfile" id="subfile" onclick="$('#browseFile').click();">
                            <a class="btn btn-success" onclick="$('#browseFile').click();">Parcourir</a>
                        </div>
                    </div>
                </div>
                <div class="row">
                </div>
                {#<div class="row">#}
                    <input id="fileFormSubmitButton" type="submit" value="Ajouter" class="btn btn-primary span3"/>
                    <span class="span6">&nbsp;</span>
                    <input id="fileFormCancelButton" type="button" value="Annuler" class="btn span3"/>
                {#</div>#}
            </div>

My little hack for this file input is to hide the standard input and, with javascript, redirecting events (click, focus) from the styled element to the hidden one ( #subfile => form_widget(form.file) ).

Everything works fine, but the only problem is about the file input. When submiting the form, the validation messages are correctly shown for the required standard inputs, but for this file input (that is required too), the message is shown in the bottom left hand corner of the browser (the only way to see the « symfony popup tooltip » is to keep the browser (firefox in my case) not in fullscreen mode.

My question : is there a way to specifiy in symfony FormType or anywhere else to « attach » the error message to a specific component ?

Thank you

War es hilfreich?

Lösung

It's possible with custom constraint. in the validator class, we use

$this->context
    ->addViolationAt('lastname', $constraint->message, array('%string%' => $value));

But honestly, if you would do the file input properly you wouldn't have this error. Use the input type file with the form class, or without it, but not half half.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top