2

私の問題は非常に単純ですが、それを解決するために何をすべきか本当にわかりません。

ここで状況:

symfony2 (2.3.*) で、フォーム (DocumentType.php + twig render テンプレート) を作成しました。ファイル形式が含まれています。いくつかの古典的なフィールド (テキスト、選択肢) と 1 つのファイル入力があります。このファイル入力は非常に特殊です。要素のレンダリング属性をカスタマイズするためにちょっとしたハックを使用しています。

ここでEntityTypeクラス:

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()
    {
        [...]
    }
}

Twigテンプレート(部分):

             <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>

このファイル入力に対する私の小さなハックは、標準入力を非表示にし、javascript を使用して、イベント (クリック、フォーカス) をスタイル付き要素から非表示の要素にリダイレクトすることです ( #subfile => form_widget(form.file) )。

すべて正常に動作しますが、唯一の問題はファイル入力に関するものです。フォームを送信すると、必須の標準入力に対して検証メッセージが正しく表示されますが、このファイル入力 (これも必須) の場合、メッセージはブラウザーの左下隅に表示されます (« を表示する唯一の方法です)。 symfony popup tooltip » は、ブラウザー (私の場合は firefox) をフルスクリーン モードにしないようにすることです。

私の質問: symfony の FormType または他の場所で、エラー メッセージを特定のコンポーネントに「添付」する方法はありますか?

ありがとうございました

4

1 に答える 1

2

カスタム制約で可能です。バリデータクラスでは、

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

しかし、正直なところ、ファイル入力を適切に行っていれば、このエラーは発生しません。入力型ファイルをフォーム クラスと一緒に使用するか、フォーム クラスを使用せずに使用しますが、半分半分は使用しないでください。

于 2013-08-16T09:50:39.377 に答える