mardi 21 juin 2016

how to validate nested json using request file in laravel 5.1

This is my form post data ,I want to validate this post data using custom validation request class.I am able to check all the validation rules against normal data but unable to check the same kind of rules to secondatry_users.I need my request files also apply rules on secondary_users data defined in the rules method

[
  "first_name" => "Shubham"
  "last_name" => "Jolly"
  "email" => "sachinu.meta@gmail.com"
  "mobile" => ""
  "status" => "1"
  "role" => "7"
  "trial" => ""
  "password" => "HQfZ6$px"
  "autogenerate" => "1"
  "secondary_users" => "{
            "1":{
            "recordID":1,
            "secondaryUserFirstName":"shubhamiii",
            "secondaryUserLastName":"s",
            "secondaryUserEmail":"shubha.jolly@daffodilsw.com",
            "secondaryUserMoble":"",
            "secondaryUserStatus":"1",
            "secondaryUserTrial":"",
            "secondaryUserPassword":"SyEsM#w2",
            "tag":""
            },
            "2":{
            "recordID":2,
            "secondaryUserFirstName":"shubha",
            "secondaryUserLastName":"jolly",
            "secondaryUserEmail":"shubham.jolly@daffodilsw.com",
            "secondaryUserMoble":"9896156667",
            "secondaryUserStatus":"1",
            "secondaryUserTrial":"2",
            "secondaryUserPassword":"aasM#w2",
            "tag":""
            }
        }"
]

How to validate this data i have created rules but its not working for the secondary_users input.

public function rules() {                 
     // Basic Property Validation  
    $rules = [
    'first_name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users',
    'role' => 'required',
    'status' => 'required',
    'password' => 'required|min:8|max:30|http://regex:/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()+]+.*)[0-9a-zA-Z\w!@#$%^&*()+]{8,}$/',
     'trial' => 'numeric',
     ];
      //secondary users validation
    if (isset($secondaryUser) && !empty(json_decode($secondaryUser))) {
         $usersArray = json_decode($secondaryUser);
             foreach ($usersArray as $key => $value) {
                   $rules['secondary_users.' . $key . '.secondaryUserFirstName'] = 'required|max:255';
                   $rules['secondary_users.' . $key . '.secondaryUserEmail'] = 'required|email|max:255|unique:users';
                   $rules['secondary_users.' . $key . '.secondaryUserStatus'] = 'required';
                   $rules['secondary_users.' . $key . '.secondaryUserTrial'] = 'numeric';
                   $rules['secondary_users.' . $key . '.secondaryUserPassword'] = 'required|min:8|max:30|http://regex:/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()+]+.*)[0-9a-zA-Z\w!@#$%^&*()+]{8,}$/';

                }
            }
        return rules;
        }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire