본문 바로가기

개발/Laravel

Laravel Validate Array of Uploaded File

<?php
	
    public function trackInfoValidation(Request $request)
    {
        $uploadedfilecount = 0;
        $trackfileuploadedcheck = true;

        foreach($_FILES['trackfile']['name'] as $key => $val)
        {
            if($val != "") 
            {
                $uploadedfilecount += 1;
            }
        }

        if(count($request->input('track_seq')) == $uploadedfilecount)
        {
            $trackfileuploadedcheck = false;
        }

        $validator = Validator::make($request->all(), [
            'TrackName.*' => ['required_if:TrackName_ENG.*,""'],
            'TrackName_ENG.*' => ['required_if:TrackName.*,""'],
            'distTrackCode.*' => ['required'],
            'trackfile.*' => ['required'],
            'TrackArtist_Name.*' => ['required_if:TrackArtist_Name_ENG.*, ""'],
            'TrackArtist_Name_ENG.*' => ['required_if:TrackArtist_Name.*, ""'],
        ]);

        if ($validator->fails() || $uploadedfilecount == false)
        {
            return response()->json(array('errors' => $validator->getMessageBag()->toArray()));
        } 
        else 
        {
            return response()->json(array('success' => 'validation success'));
        }
    }
   
?>
반응형

'개발 > Laravel' 카테고리의 다른 글

라라벨 요청 전체에 대한 trim 처리 (미들웨어 사용)  (0) 2020.07.31
Laravel 라우팅 그룹 설정  (0) 2020.07.24
Laravel Ajax Form Validation  (0) 2020.07.21
Laravel Pagination  (0) 2020.07.02
Laravel Storage 외부 파일 조회  (0) 2020.06.27