2016-03-07 100 views
1

我有一個2字段,郵政編碼和國家的窗體。 我需要根據國家選擇以驗證郵政編碼,使用$驗證如何根據Angular Schema Form中另一個字段的值驗證字段?

這裏是ngController代碼

$scope.schema = { 
"type": "object", 
"title": "my form", 
"properties": { 
    "postalCode": { 
    "title": "Postal Code", 
    "type": "string" 
    }, 
    "countryCode": { 
    "title": "Country", 
    "type": "string", 
    "enum": [ 
     "CA", 
     "US" 
    ] 
    } 
}, 
"required": [ 
    "countryCode" 
] 
}; 


    $scope.form = [{ 
    "key": "postalCode", 
    $validators: { 
     // I would like to pass countryCode as the argument but doesn't work because I don't have model in my closure 
     postalCodeRegex: function (countryCodeValue) { 
      if (angular.isString(countryCodeValue)) {     
       var canada = "^(?!.*[DFIOQU])[A-VXY][0-9][A-Z]●?[0-9][A-Z][0-9]$"; 
       if (/canada/.test(countryCodeValue)) return true; 
       else return false; 
      } 
      // if value is empty 
      return true; 
     } 
    } 
}, 
"countryCode", { 
    "type": "submit", 
    "style": "btn-info", 
    "title": "OK" 
} 
]; 

有另一種方式來訪問從$模型或範圍驗證器的邏輯?

plunker鏈接查看我的代碼: http://plnkr.co/edit/DDbMiw?p=info

回答

相關問題