| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Http\Requests; |
| 6 |
|
| 7 |
/** |
| 8 |
* Trip Request |
| 9 |
* Validates trip creation/update requests |
| 10 |
*/ |
| 11 |
class TripRequest extends BaseRequest |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Validation rules |
| 15 |
*/ |
| 16 |
protected function rules(): array |
| 17 |
{ |
| 18 |
return [ |
| 19 |
'title' => 'required|string|max:255', |
| 20 |
'slug' => 'required|string|max:255', |
| 21 |
'description' => 'nullable|string', |
| 22 |
'price' => 'nullable|numeric|min:0', |
| 23 |
'status' => 'nullable|string|in:draft,active,inactive', |
| 24 |
]; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Validate the request |
| 29 |
*/ |
| 30 |
public function validate(): bool |
| 31 |
{ |
| 32 |
$rules = $this->rules(); |
| 33 |
|
| 34 |
foreach ($rules as $field => $rule) { |
| 35 |
$ruleParts = explode('|', $rule); |
| 36 |
|
| 37 |
foreach ($ruleParts as $rulePart) { |
| 38 |
if ($rulePart === 'required' && !isset($this->data[$field])) { |
| 39 |
$this->errors[$field][] = "The {$field} field is required."; |
| 40 |
} |
| 41 |
|
| 42 |
if (strpos($rulePart, 'max:') === 0 && isset($this->data[$field])) { |
| 43 |
$max = (int) str_replace('max:', '', $rulePart); |
| 44 |
if (strlen($this->data[$field]) > $max) { |
| 45 |
$this->errors[$field][] = "The {$field} field must not exceed {$max} characters."; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if ($rulePart === 'numeric' && isset($this->data[$field]) && !is_numeric($this->data[$field])) { |
| 50 |
$this->errors[$field][] = "The {$field} field must be numeric."; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return $this->isValid(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|