需要引导文件输入字段

时间:2016-02-17 07:46:19

标签: yii2

如何设置所需的Bootstrap fileinput字段?

HTML:

<input id="input-702" overwriteInitial="false" name="productimage[]" type="file" multiple="true" class="file-loading" data-preview-file-type="text">

JS:

    $('#input-702').fileinput({

        uploadUrl: '$action', // server upload action 
        uploadAsync: false,
        maxFileCount: 15,
        uploadExtraData:function(){
            return {id:$('#draftproductid').val()};
        },
         initialPreviewConfig: [
            { url: '/site/file-delete',},
            { url: '/site/file-delete',}, 
        ],
    });
    // CATCH RESPONSE
    $('#input-702').on('filebatchuploaderror', function(event, data, previewId, index) {
    var form = data.form, files = data.files, extra = data.extra, 
        response = data.response, reader = data.reader;

    });

    $('#input-702').on('filebatchuploadsuccess', function(event, data, previewId, index) {
       var form = data.form, files = data.files, extra = data.extra, response = data.response, reader = data.reader;
        var imagename = JSON.stringify(response);
        $('#imagetitle').val(imagename);            
    });

    $('#input-702').on('filepredelete', function(event, key) {
        console.log('Key = ' + key);
    });

我想在表单提交上设置验证。 这怎么可能 ? 请尽可能帮助我。

1 个答案:

答案 0 :(得分:0)

在相关模型中的验证规则中设置必需,例如(名称,电子邮件,suubjec,正文)样本

class YourModel extends \yii\db\ActiveRecord
{ 
  ..... 
  public function rules()
  {
    return [
        // the name, email, subject and body attributes are required
        [['name', 'email', 'subject', 'body'], 'required'],

        // the email attribute should be a valid email address
        ['email', 'email'],
     ];
 }