由於從該頁面的答案瞭解https://github.com/webadvanced/ng-remote-validate
選項指令,比我不太喜歡的選項指令少,因爲每個字段都寫入指令。 模塊是相同的 - 一個通用的解決方案。
但是在模塊中,我錯過了一些東西 - 檢查字段中的幾條規則。
然後我只是修改模塊https://github.com/borodatych/ngRemoteValidate
俄羅斯自述文件的道歉最終會改變。
我急於分享突然有人有同樣的問題。
是的,我們爲此聚集在這裏......
負載:
<script type="text/javascript" src="../your/path/remoteValidate.js"></script>
包括:
var app = angular.module('myApp', [ 'remoteValidate' ]);
HTML
<input type="text" name="login"
ng-model="user.login"
remote-validate="('/ajax/validation/login', ['not_empty',['min_length',2],['max_length',32],'domain','unique'])"
required
/>
<br/>
<div class="form-input-valid" ng-show="form.login.$pristine || (form.login.$dirty && rv.login.$valid)">
From 2 to 16 characters (numbers, letters and hyphens)
</div>
<span class="form-input-valid error" ng-show="form.login.$error.remoteValidate">
<span ng:bind="form.login.$message"></span>
</span>
後端[Kohana的]
public function action_validation(){
$field = $this->request->param('field');
$value = Arr::get($_POST,'value');
$rules = Arr::get($_POST,'rules',[]);
$aValid[$field] = $value;
$validation = Validation::factory($aValid);
foreach($rules AS $rule){
if(in_array($rule,['unique'])){
/// Clients - Users Models
$validation = $validation->rule($field,$rule,[':field',':value','Clients']);
}
elseif(is_array($rule)){ /// min_length, max_length
$validation = $validation->rule($field,$rule[0],[':value',$rule[1]]);
}
else{
$validation = $validation->rule($field,$rule);
}
}
$c = false;
try{
$c = $validation->check();
}
catch(Exception $e){
$err = $e->getMessage();
Response::jEcho($err);
}
if($c){
$response = [
'isValid' => TRUE,
'message' => 'GOOD'
];
}
else{
$e = $validation->errors('validation');
$response = [
'isValid' => FALSE,
'message' => $e[$field]
];
}
Response::jEcho($response);
}
這很好用!我正在使用版本0.6.1我發現使用'inputnameSetArgs'沒有被調用是一個開放的問題。還有一個解決方案,將'$ parent'添加到範圍中。我手動改變了這個,然後'SetArgs'被調用,並向服務器發送額外的參數。 – Guus
完美的作品,謝謝你的分享! –