Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在YII2中修改ActiveField的formgroup div类_Yii2_Active Form - Fatal编程技术网

如何在YII2中修改ActiveField的formgroup div类

如何在YII2中修改ActiveField的formgroup div类,yii2,active-form,Yii2,Active Form,我有这样的模板: <div class="form-group form-file-upload form-file-multiple"> <input type="file" multiple="" class="inputFileHidden"> <div class="input-group"> <input type="text" class="form-control inputFileVisible" placeholder="Sin

我有这样的模板:

<div class="form-group form-file-upload form-file-multiple">
<input type="file" multiple="" class="inputFileHidden">
<div class="input-group">
    <input type="text" class="form-control inputFileVisible" placeholder="Single File">
    <span class="input-group-btn">
        <button type="button" class="btn btn-fab btn-round btn-primary">
            <i class="material-icons">attach_file</i>
        </button>
    </span>
</div>
<?php

use yii\helpers\Html;
echo Html::beginForm(
    ['controller/action'],
    'post',
    ['enctype' => 'multipart/form-data'] //if you want to upload file with post
); ?>
    <div class="form-group form-file-upload form-file-multiple">
        <?= Html::activeFileInput(
            $model,
            'file_name',
            ['class' => 'inputFileHidden', 'multiple' => '']
        ); ?>
        <div class="input-group">
            <?= Html::activeTextInput(
                $model,
                'file_name', 
                [
                    'class' => 'form-control inputFileVisible',
                    'placeholder' => 'Single File'
                ]
            ); ?>
            <span class="input-group-btn">
                <button type="button" class="btn btn-fab btn-round btn-primary">
                    <i class="material-icons">attach_file</i>
                </button>
            </span>
        </div>
    </div>   
<?= Html::endForm(); ?>

附加文件
我使用yii2框架并尝试配置字段输入文件

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'file_name')->fileInput(); ?>
<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>

我试着在互联网上搜索,但只有这样才能从组中更改div类

<?php $form = ActiveForm::begin([
'fieldConfig' => ['options' => ['class' => 'form-group invisible']],
 ]); ?>

请帮助我,我怎样才能添加

<input type="file" multiple="" class="inputFileHidden">
<div class="input-group">
<input type="text" class="form-control inputFileVisible" placeholder="Single File">
<span class="input-group-btn">
    <button type="button" class="btn btn-fab btn-round btn-primary">
        <i class="material-icons">attach_file</i>
    </button>
</span>

附加文件

分成一组

如果不需要使用活动表单的客户端验证功能,则不必使用
$form->field()
生成输入。您可以使用
yii\helpers\Html
而不是
yii\widgets\ActiveForm
来更好地控制生成的Html

您可以使用如下模板:

<div class="form-group form-file-upload form-file-multiple">
<input type="file" multiple="" class="inputFileHidden">
<div class="input-group">
    <input type="text" class="form-control inputFileVisible" placeholder="Single File">
    <span class="input-group-btn">
        <button type="button" class="btn btn-fab btn-round btn-primary">
            <i class="material-icons">attach_file</i>
        </button>
    </span>
</div>
<?php

use yii\helpers\Html;
echo Html::beginForm(
    ['controller/action'],
    'post',
    ['enctype' => 'multipart/form-data'] //if you want to upload file with post
); ?>
    <div class="form-group form-file-upload form-file-multiple">
        <?= Html::activeFileInput(
            $model,
            'file_name',
            ['class' => 'inputFileHidden', 'multiple' => '']
        ); ?>
        <div class="input-group">
            <?= Html::activeTextInput(
                $model,
                'file_name', 
                [
                    'class' => 'form-control inputFileVisible',
                    'placeholder' => 'Single File'
                ]
            ); ?>
            <span class="input-group-btn">
                <button type="button" class="btn btn-fab btn-round btn-primary">
                    <i class="material-icons">attach_file</i>
                </button>
            </span>
        </div>
    </div>   
<?= Html::endForm(); ?>

附加文件
您可以使用
Html::error()
Html::errorSummary()
显示错误。更多信息

另一个选项是实现自己的小部件,它将扩展
yii\widgets\InputWidget
。您将实现
run()
方法,并在那里生成所需的html代码。然后您可以使用小部件,例如
$form->field(…)->widget(YourWidget::class)


.

Yii2框架。要修改/添加表单组类,请使用字段属性:
“选项”

$form->field($model,“username”[
“选项”=>[“类”=>“表单组某些自定义类”]
])->textInput([“自动完成”=>“关闭”])->标签('USERNAME');

感谢您的回复,这很有效,但现在我如何获得验证?服务器端验证应该仍然可以正常工作。如果您需要客户端验证,您必须自己部分实现它。您可以使用验证器的
clientValidateAttribute()
方法获取用于字段验证的JS代码。如果希望对其他字段而不是文件输入进行客户端验证,则仍然可以对其他字段使用ActiveForm,并仅对文件输入使用Html帮助程序(跳过
Html::beginForm
Html::endForm
)我的服务器端验证不起作用,验证忽略文件扩展名规则,但不使用字符串长度(最大值)规则,但可能是我会发布另一个问题。