| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Library\Validation\Rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Contracts\Validation\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Contracts\Validation\ValidationRule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\Facades\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ValidUpload extends BackpackCustomRule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     use HasFiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * Run the validation rule. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param  string  $attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param  mixed  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param  Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     public function validate(string $attribute, mixed $value, Closure $fail): void | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $entry = CrudPanelFacade::getCurrentEntry(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         if (! array_key_exists($attribute, $this->data) && $entry) { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this->validateFieldRules($attribute, $value, $fail); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         if (! empty($value) && ! empty($this->getFileRules())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             $validator = Validator::make([$attribute => $value], [ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 $attribute => $this->getFileRules(), | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             ], $this->validator->customMessages, $this->validator->customAttributes); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             if ($validator->fails()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 foreach ($validator->errors()->messages()[$attribute] as $message) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                     $fail($message)->translate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public static function field(string|array|ValidationRule|Rule $rules = []): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return parent::field($rules); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  |