上传csv文件时的空验证无法使用PHP工作

mqkwyuun  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(56)

日安,
我试图在我的代码中添加一个验证,如果“故障症状”列为空,并且“故障症状指定”有一个值,它将返回一个消息,“故障症状指定”应该为空,因为“故障症状”为空。
下面是我的代码:

if ($column === 'OtherSymptoms') {
    $validValues = ['Y', 'N'];
    $cellValue = $row[$OtherSymptoms];
    $othersSpecifyValue = $row[$OthersSpecify]; 

    if (!in_array($cellValue, $validValues)) {
    $response['errors'][] = [
        'error' => 'Invalid ' . $column . ' Value',
        'message' => '• The value for ' . $column . ' in row ' . $i . ': "' . $cellValue . '" is not valid. Accepted values are: "' . implode('" or "', $validValues) . '".'
        ];
    }

    if (empty($cellValue) && !empty($othersSpecifyValue)) {
        $response['errors'][] = [
            'error' => 'Invalid ' . $column . ' Value',
            'message' => '• The value for ' . $column . ' is empty in row ' . ($i) . ', therefore ' . $OthersSpecify . ' should not value.'
        ];
    }

    if ($cellValue === 'Y' && empty($othersSpecifyValue)) {
        $response['errors'][] = [
            'error' => 'Invalid ' . $column . ' Value',
            'message' => '• The value for ' . $column . ' is set to Y in row ' . ($i) . ', therefore ' . $OthersSpecify . ' in the sperow must not be empty.'
        ];
    }

    if ($cellValue === 'N' && !empty($othersSpecifyValue)) {
        $response['errors'][] = [
            'error' => 'Invalid ' . $column . ' Value',
            'message' => '• The value for ' . $column . ' is set to N in row ' . ($i) . ', therefore ' . $OthersSpecify . ' in the specified row must be empty.'
        ];
    }
}

字符串
“Y”和“N”验证工作正常,但当列的空行不是。我将感谢更正和解决方案。谢谢大家。

vwkv1x7d

vwkv1x7d1#

if (empty($cellValue) && !empty($othersSpecifyValue)) {
    $response['errors'][] = [
        'error' => 'Invalid ' . $column . ' Value',
        'message' => '• The value for ' . $column . ' is empty in row ' . ($i) . ', therefore ' . $OthersSpecify . ' should not value.'
    ];
}

字符串
你正在测试是否有“不良症状”($cellValue)和“指定值”($othersSpecifyValue)都为空。如果满足此条件,则添加错误消息,该消息似乎与您所需的相反。如果我理解正确,则您希望在“故障症状”和“故障指定”都为空时显示错误。当“故障症状”为空时,你应该改变这个条件来检查'BullsSpecify'是否不为空。

if (empty($cellValue) && !empty($othersSpecifyValue)) {
    $response['errors'][] = [
        'error' => 'Invalid ' . $column . ' Value',
        'message' => '• The value for ' . $column . ' is empty in row ' . ($i) . ', therefore ' . $OthersSpecify . ' should be empty.'
    ];
}

相关问题