1

My json has two objects one is user_profile and other one is privileges but my validation is working only on user_profile object.

My json:

{
    "user_profile": {
        "email": "[email protected]",
        "password": "admin123",
        "password_confirmation": "admin123",
        "status": 0,
        "first_name": "Shahzad",
        "middle_name": "Hussain",
        "last_name": "Shah",
        "date_of_birth": "2015-01-01",
        "gender": "M",
        "area_id": 1,
        "address": "Minhatten NY",
        "city": "New York",
        "state": "Washington",
        "zip": "12312",
        "fax": "111-111-1111",
        "phone_extension": "2471",
        "work_phone": "111-111-1111",
        "phone_no": "111-111-1111",
        "emergency_contact": "111-111-1111",
        "social_security": "111-11-1111",
        "module_id": 2
        },

    "privileges": {

        "is_super_admin": 1,

      "facilities": [
          {
               "facility_id": 1,
              "is_facility_supervisor": 0,
              "speciality_id": 1,
              "priv_id": "",
              "role_id": 1
          }
      ]
  }
}

I want to validate my "privileges" object as well.

My controller:

public function register(Request $request) {

    $body = $request->all();
    $userProfile = $body['user_profile'];
    $userPrev = $body['privileges'];
    $userProfile['is_super_admin'] = $userPrev['is_super_admin'];
    $facilities = $userPrev['facilities'];

    $bodyObj = array_merge($userProfile, $userPrev);
    $validator = UserValidations::validateUser($bodyObj);

    if ($validator->fails()) {
         return response([
             'status' => false,
             'message'   => __('messages.validation_errors'),
             'errors' => $validator->errors()->all()
        ], 200);
    }

and my validateUser method:

public static function validateUser($data = [], $update = false) {
    $rules = [
        'first_name'    => 'required|max:25',
        'last_name'     => 'required|max:25',
        "address"       => 'required|max:197',
        "city"          => 'required|max:20',
        "zip"           => 'required|max:5',
        "phone_no"      => 'required|max:12',
        "area_id"      => 'exists:areas,id',
        "phone_extension"   => 'min:3|max:5',
        'gender'        => 'in:M,F,X',
        "work_phone"    => 'max:12',
        "emergency_contact"    => 'max:12',
        "fax"   => "max:12",
        "social_security"      => 'required|max:11',
        "module_id" => 'exists:modules,id',

        // privileges object fields
        "role_id" => 'required|exists:roles,id',
        "facility_id" => 'required',
        "speciality_id" => 'required'
    ];


    if($update) {
        $rules['id'] = 'required|exists:users,id';
    } else {
        $rules['email'] = 'required|email|unique:users';
        $rules['password'] = 'required|min:6';
        $rules['password_confirmation'] = 'required_with:password|min:6|same:password';
    }

    return $validator = Validator::make($data, $rules);

}

How i can put validation on privileges object? Your help will be highly appreciated!

1
  • 1
    Can't you just validate it on a different validator? I do think it would make it more maintainable and scalable. Commented Apr 10, 2019 at 9:00

2 Answers 2

1
// privileges object fields
"facilities.*.role_id" => 'required|exists:roles,id',
"facilities.*.facility_id" => 'required',
"facilities.*.speciality_id" => 'required'

I think it will solve your problem

Sign up to request clarification or add additional context in comments.

Comments

0

you can do this

Validator::make($request->all(),[
    'your rules'])

2 Comments

my objects are not in $request directly i have used $bodyobject in validator and merge my both request
Why should one do this? Please add some explanation to your answer such that others can learn from it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.