jeudi 18 octobre 2018

Using required_unless for array

I do have 3 select ..

<select name="roles" id="roles" class="form-control" ng-model="newUserFormData.roles" multiple required>
    <option ng-repeat="role in roleList" value="<% role.id %>"><% role.name %></option>
</select>

<select name="sites" id="sites" class="form-control" ng-model="newUserFormData.sites" multiple>
    <option ng-repeat="site in siteList" value="<% site.id %>"><% site.name %></option>
</select>

<select name="projects" id="projects" class="form-control" ng-model="newUserFormData.projects" multiple>
    <option ng-repeat="project in projectList" value="<% project.id %>"><% project.name %></option>
</select>

Note:

  1. the Roles can be multiple .. is front-end required ..
    • (1) - Admin (not included in the options)
    • (2) - Lead
    • (3) - Analyst
    • (4) - Key Entry
  2. the Sites can be multiple .. not front-end required ..
    • (1) - Loc 1
    • (2) - Loc 2
    • (3) - Loc 3
  3. the Projects can be multiple .. not front-end required ..
    • (1) - Project 1
    • (2) - Project 2
    • (3) - Project 3

now my validator ..

protected function validatorName(array $data)
{
    return Validator::make($data, [ 
        'roles' =>  'required|array|min:1',
        'sites'  => 'required_unless:roles.*,in:2|required_unless:roles.*,in:3',
        'projects'  => 'required_unless:roles.*,in:2|required_unless:roles.*,in:3',
    ],[
        'sites.required_unless' => 'Site is required unless Lead or Analyst is selected as role.',
        'projects.required_unless' => 'Project is required unless Lead or Analyst is selected as role.',
    ]);
}

Problem:

  • When I select either Lead/Analyst as the role, it is still requiring sites and projects when supposedly should not.

    Request Payload: roles: ["3"],

  • When I select Key Entry as the role, it is still requiring sites and projects even there are values.

    Request Payload: projects: ["1"], roles: ["4"], sites: ["2"],

What am I missing ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire