CollectionStoreRequest.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Http\Requests\Collection; |
| 4 | |
| 5 | use Kirki\App\Constants\Collection\CustomFieldTypes; |
| 6 | use Kirki\Framework\Http\Request; |
| 7 | use Kirki\Framework\Sanitizer; |
| 8 | |
| 9 | class CollectionStoreRequest extends Request |
| 10 | { |
| 11 | /** |
| 12 | * Prepare data for validation. |
| 13 | */ |
| 14 | protected function prepare_for_validation() |
| 15 | { |
| 16 | $data = $this->get('data'); |
| 17 | |
| 18 | if (is_string($data)) { |
| 19 | $data = json_decode($data, true); |
| 20 | } |
| 21 | |
| 22 | if (is_array($data)) { |
| 23 | $this->merge($data); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Validation rules. |
| 29 | */ |
| 30 | public function rules() |
| 31 | { |
| 32 | return [ |
| 33 | 'ID' => 'nullable', |
| 34 | 'post_title' => 'required|string', |
| 35 | 'post_name' => 'nullable|string', |
| 36 | |
| 37 | 'fields' => 'nullable|array', |
| 38 | 'fields.*.id' => 'required|string', |
| 39 | 'fields.*.kind' => 'required|string|in:custom', |
| 40 | 'fields.*.type' => 'required|string|in:' . CustomFieldTypes::join(), |
| 41 | 'fields.*.title' => 'required|string', |
| 42 | 'fields.*.help_text' => 'nullable|string', |
| 43 | 'fields.*.required' => 'required|boolean', |
| 44 | |
| 45 | 'basic_fields' => 'nullable|array', |
| 46 | ]; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Sanitization filters. |
| 51 | */ |
| 52 | public function filters() |
| 53 | { |
| 54 | return [ |
| 55 | 'ID' => Sanitizer::INT, |
| 56 | 'post_title' => Sanitizer::TEXT, |
| 57 | 'post_name' => Sanitizer::TEXT, |
| 58 | |
| 59 | 'fields' => Sanitizer::ARRAY , |
| 60 | 'fields.*.id' => Sanitizer::TEXT, |
| 61 | 'fields.*.kind' => Sanitizer::TEXT, |
| 62 | 'fields.*.type' => Sanitizer::TEXT, |
| 63 | 'fields.*.title' => Sanitizer::TEXT, |
| 64 | 'fields.*.help_text' => Sanitizer::TEXT, |
| 65 | 'fields.*.required' => Sanitizer::BOOL, |
| 66 | |
| 67 | 'basic_fields' => Sanitizer::ARRAY , |
| 68 | ]; |
| 69 | } |
| 70 | } |
| 71 |