Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validation object string with validation laravel?

I use laravel 5.6

I use https://laravel.com/docs/5.6/validation#form-request-validation to validation server side

My controller like this :

<?php
....
use App\Http\Requests\UserUpdateRequest;

class UserController extends Controller
{
    ...
    public function update(UserUpdateRequest $request)
    {
        // dd($request->all());
    }
}

Before run statement in the update method, it will call UserUpdateRequest to validation server side

The validation like this :

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserUpdateRequest extends FormRequest
{
    ....
    public function rules()
    {
        dd($this->request->all());
        return [
            'name'          => 'required|max:50',
            'gender'        => 'required',
            'birth_date'    => 'required',
            'address'       => 'required',
            'status'        => 'required'
        ];
    }
}

The result of dd($this->request->all()); like this :

Array
(
    [selected_data] => {"name":"agis","gender":"2","birth_date":"2018-03-13","address":"london"}
)

How can I validation if the data is object array like that?

like image 461
moses toh Avatar asked Jan 28 '23 12:01

moses toh


1 Answers

You can use a dot notation like so:

public function rules()
{
    return [
        'selected_data.name' => 'required|max:50',
        'selected_data.gender' => 'required',
        'selected_data.birth_date' => 'required',
        'selected_data.address' => 'required',
        'selected_data.status' => 'required',
    ];
}

Read more about it here: Validating Array.

Hope this helps.

like image 166
Risan Bagja Pradana Avatar answered Feb 05 '23 18:02

Risan Bagja Pradana