Home > database >  How to validate chosen data in laravel request file?
How to validate chosen data in laravel request file?

Time:04-25

I am using laravel Request to validate data but those data are json so I got data and convert them to array what i need to do now is validating $details from the code below. is there any way to validate them inside the request file apart from using request->validate in controller?

my request file:

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PublicRegisterRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        $details = json_decode($this->request->get('details'));
        return [
            'firstName' => 'required|max:25',
            'lastName' => 'required|max:25',
            'fatherName' => 'required|max:25',

        ];
    }
}

CodePudding user response:

If you access to client side, its proper to get details body instead . I mean instead of getting :

{
  "details" :{
            'firstName' : '...',
            'lastName' : '...',
            'fatherName' : '...',
   }
}

You should get :

{
  'firstName' : '...',
  'lastName' : '...',
  'fatherName' : '...',
}

But , if you dont access to client side , simply you can use this pattern in your request class :

 public function rules()
    {

        return [
            'details.firstName' => 'required|max:25',
            'details.lastName' => 'required|max:25',
            'details.fatherName' => 'required|max:25',

        ];
    }

Hope that would works.

  • Related