| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\Http\Requests;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Validation\Rule;
- class InhabitantClassificationRequest extends FormRequest
- {
- public function rules(): array
- {
- $id = $this->route('id');
- $rules = [
- 'description' => ['sometimes', 'string', 'max:150'],
- 'start' => ['sometimes', 'integer', 'min:1'],
- 'end' => ['sometimes', 'integer', 'min:1', 'gte:start'],
- 'tbr_percentage' => ['sometimes', 'numeric', 'min:0', 'max:1'],
- ];
- if ($this->isMethod('post')) {
- $rules['description'] = ['required', 'string', 'max:150'];
- $rules['start'] = ['required', 'integer', 'min:1'];
- $rules['end'] = ['required', 'integer', 'min:1', 'gte:start'];
- $rules['tbr_percentage'] = ['required', 'numeric', 'min:0', 'max:1'];
- }
- return $rules;
- }
- public function messages(): array
- {
- return [
- 'description.required' => 'A descrição é obrigatória.',
- 'start.required' => 'O mês de início é obrigatório.',
- 'end.required' => 'O mês de fim é obrigatório.',
- 'end.gte' => 'O mês de fim deve ser maior ou igual ao início.',
- 'tbr_percentage.required' => 'A porcentagem do TBR é obrigatória.',
- ];
- }
- protected function prepareForValidation()
- {
- if ($this->route('id')) {
- $model = \App\Models\InhabitantClassification::find($this->route('id'));
- if ($model) {
- $data = [];
- if (!$this->has('start')) {
- $data['start'] = $model->start;
- }
- if (!$this->has('end')) {
- $data['end'] = $model->end;
- }
- $this->merge($data);
- }
- }
- }
- }
|