即使文件在 fileInput 字段中上传,FileInput 字段也是必需的 - Yii2

问题描述 投票:0回答:1

我有 2 个文件输入字段,设置为必填字段。即使浏览文件,这 2 个字段也会显示错误消息。

查看

<?php $form = ActiveForm::begin([ 'enableAjaxValidation' => true, 'enableClientValidation' => false, 'options'=>['enctype'=>'multipart/form-data']]); ?>
<div class="div_salary_deposited">
    <div class="row">
        <div class="col-lg-4 col-md-4 col-sm-4">
            <div class="form-group">
                <label class="control-label">Scan copy of Payslip: <span class="required">*</span></label>
                <?= $form->field($modelPlacementTrackingSalaryInformation, 'scan_copy_of_payslip')->label(false)->fileInput() ?>
            </div>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4">
            <div class="form-group">
                <label class="control-label">Salary Payment Proof: <span class="required">*</span></label>
                <?= $form->field($modelPlacementTrackingSalaryInformation, 'salary_payment_proof')->label(false)->fileInput() ?>
            </div>
        </div>
    </div>
</div>
<div class="form-group">
    <ul class="list-unstyled list-inline">
        <li>
            <button class="btn btn-info" type="submit">Continue</button>
        </li>
    </ul>
</div>
<div class="clearfix"></div>
<?php ActiveForm::end(); ?>

控制器

/* salary-information */
public function actionSalaryInformation(){
    
    $modelPlacementTrackingSalaryInformation = $this->placementTrackingRepository->getPlacementTrackingSalaryInformationModel();
    $modelPlacementTrackingSalaryInformation->placement_tracking_id = $placement_tracking_id;
    
    if(($postData = Yii::$app->request->post()) && !empty($postData) && $modelPlacementTrackingSalaryInformation->load($postData)){
        
        if(Yii::$app->request->isAjax){
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($modelPlacementTrackingSalaryInformation);
        }
    }
    
    return $this->render('salary_information',[
        'modelPlacementTrackingSalaryInformation' => $modelPlacementTrackingSalaryInformation,
    ]);
}

型号

<?php
namespace app\models;

use Yii;

class PlacementTrackingCandidateSalaryInfo extends \yii\db\ActiveRecord {
    
    public $salaryFrom;
    public $salaryTo;
    
    public function rules() {
        return [
            ['placement_tracking_id','required','message' => 'Placement Tracking Id missing'],
            ['salary_deposited','required','message' => "Please select 'Salary deposited?'"],
            
            /* START: Is Salary Deposited: Yes */
                //Bank Name
                ['bank_name', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please select 'Bank Name'."],
                
                //Branch Name
                ['branch_name', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please enter 'Branch Name'."],
                
                //Salary
                ['salary', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please enter 'Salary'."],
                
                //Salary From Date
                ['salaryFrom', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please select 'Salary From Date'."],
                
                //Salary To Date
                ['salaryTo', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please select 'Salary To Date'."],
                
                //No of Days
                ['no_of_days', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please enter 'No of Days'."],
                
                //Reference No
                ['reference_no', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please enter 'Reference Number'."],
                
                //Scan Copy of Payslip
                ['scan_copy_of_payslip', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please upload 'Scan Copy of Payslip'."],
                
                //Salary Payment Proof
                ['salary_payment_proof', 'required', 'when' => function($model){
                    return ($model->salary_deposited == '1') ? true : false;
                },'message' => "Please upload 'Salary Payment Proof'."],
                
            /* END: Is Salary Deposited: Yes */
            
            [['salary_from_month','salary_from_year','salary_to_month','salary_to_year'],'safe'],
        ];
    }
    
}

图片

即使浏览文件后,也会显示错误消息。

任何帮助/提示/建议都是值得赞赏的。

php yii2 yii2-basic-app yii2-validation
1个回答
0
投票

你可以这样尝试吗?

['salary_payment_proof', 'required','when' => function ($model) {
                        return $model->salary_deposited  == '1';
        }, 'whenClient' => "function (attribute, value) {
            return $('your radio button id').val() == '1';
        }"],
© www.soinside.com 2019 - 2024. All rights reserved.