How validate with getInstancesByName?


#1

Hi,

I need to validate the multiple uploaded images, so how I can do it, please!

Thanks

    public static function uploadPicture ($vid) {
  $model = new Pictures ();
  $model->load(\Yii::$app->getRequest()->getBodyParams(), '');
  $model->vid_image = \yii\web\UploadedFile::getInstancesByName('vid_image');
  $imageDir = Yii::$app->params[ 'uploadDir' ];


  //if ( $model->validate() AND !empty($model->vid_image) ) { //does not work
  if ( !empty($model->vid_image) ) {
    foreach ( $model->vid_image as $images => $image) {
      $model->name = "t_" . time() . "_i_" . uniqid() . '.' . $image->extension;
      $model->vid = $vid;


      echo $image->hasError;//return empty
      Yii::$app->end();
      //if ( $model->save() and $model->validate() ) { // does not work
      if(1==1 and $model->validate()){
        $image->saveAs($imageDir . '/' . $model->name);
        Yii::$app->getResponse()->setStatusCode(201);
        $id = implode(',', array_values($model->getPrimaryKey(true)));
        Yii::info("[pic.21] image: " . $model->name . " uploaded to: " . $imageDir, __METHOD__);
      } elseif ( $model->hasErrors() ) {
        $response = \Yii::$app->getResponse();
        $response->setStatusCode(500);
        throw new ServerErrorHttpException('Failed to create the object for unknown reason. [APIx001]');
      }
    }
  }
  return $model;
}

#2

Should I make the manual validation?