Generally, if you are using Yii with any DataBase, you should be maintaining a Model class which extends ActiveQuery like so:
<?php
namespace common\models;
use Yii;
class Profile extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'TABLE_NAME';
}
public function rules()
{
return [
[['Phone', 'Email'], 'required'],
[['Phone'], 'string', 'max' => 30],
[['Email'], 'string', 'max' => 100],
];
}
public function attributeLabels()
{
return [
'Phone' => Yii::t('profile', 'Phone'),
'Email' => Yii::t('profile', 'Email')
];
}
As you can see you can declare your Validation rules in the return of function rules(). The ones in the example here are very simple, however, many other built-in validators exist along with the ability to use your own custom validators, including the possibility of adding JS validation. You can read more about it here.
Now, when you are using your Model, you have to load data into it and validate it to save it afterwards. First of all, making sure that your input is Valid (in this case a .csv file) on the PHP server before sending it to the DataBase is more correct in my opinion, rather than receive a "hard error" from the DataBase's validation engine. Anyhow, in order to load the data into the model, we first need to load the .csv as a string. For this place your .csv into the projects folder, somewhere like \common\models\file.csv
<?php
$csv = file_get_contents(Yii::getAlias('@app/common/models/').'file.csv');
$csvArray = str_getcsv($csv);
$errorCsvArray = [];
foreach ($csvArray as $newEntry) {
$model = new \common\models\Profile();
if ($model->load($newEntry)) {
$model->save();
} else {
$errorCsvArray[] = $model->errors;
}
}
All that is left, is to convert $errorCsvArray to .csv. One way of doing this you can find here.
Moreover, notice that load() also validates before populating the model. More on load(), validate(), save() and errors you can find here.