2016-12-15 29 views
0

類似於this questionthis question我無法弄清楚如何在綁定單個字段時使用Javascript配置Parsley自定義遠程。如何使用javascript配置荷蘭芹自定義遠程,而不是屬性

例如我想(簡體):

$('#field').parsley({ 
    remote: true, 
    remoteValidator: 'mycustom'; 
}); 

是的the example相當於:

<input name="q" type="text" data-parsley-remote data-parsley-remote-validator='mycustom' value="foo" /> 

我已經註冊的遠程之後的例子:

window.Parsley.addAsyncValidator('mycustom', function (xhr) { 
    console.log(this.$element); 
    return 404 === xhr.status; 
}, '/api/foo'); 

當這個執行香菜也嘗試處理內部遙控器內部的遙控功能:

validateString: function validateString(value, url, options, instance) { 

雖然Parsley.asyncValidators確實包括mycustom遠程確定,options參數不是我希望的選項(它是歐芹場本身擁有這些選項爲options屬性)。因此options.validator在此上下文中爲空,因此該方法選擇默認值,該值未配置,因此它在url.indexOf上出錯。無論如何,如果我錯誤地配置了它,這可能都是不相關的。

我已經瀏覽了文檔,示例和源代碼,但無法弄清楚如何從配置中讀取這些值。

更新:我通過鮑爾安裝它,並使用dist/parsely.min.js。我看不到在建築的任何地方parsely.remote.js(在文檔中提到),所以我認爲它的全部編譯。

回答

0

事實證明,這是remote選項的值需要是「遠程」不是true

$('#field').parsley({ 
    remote: 'remote', 
    remoteValidator: 'mycustom'; 
}); 

由於現時data-parsely-remote沒有屬性值,我猜「標籤的存在」將評估爲true,不虛屬性的最後一個字。

+0

奇數,'remote:true'也應該有效。 –

+0

PS:要獲得歐芹字段的選項,只需使用'$('#field')。parsley()。options' –

+0

雖然.options方法是垃圾進出垃圾。它只是返回我放入的內容。 – scipilot

0

當您定義您的validateString(value, url, options, instance)時,options您將收到驗證器的選項remote

requirementType: { 
    '': 'string', 
    'validator': 'string', 
    'reverse': 'boolean', 
    'options': 'object' 
    }, 

所以會有一個validator選項(「mycustom」),可能是reverse選項,也可以一個options選項:此驗證與定義。

所以,你可以在你的領域與綁定:

$('#field').parsley({ 
    remote: true, 
    remoteValidator: 'mycustom', 
    hello: 'world', 
    remoteOptions: { foo: 'bar' } 
}); 

和訪問'bar'您的驗證內的options.options.fooinstance.options.remoteOptions.foo,並獲得'world'instance.options.hello

+0

我沒有定義'validateString()' - 它在Parsley代碼庫中。我只是在那裏嘗試通過嘗試猜測錯誤來猜測爲遠程設備定義的設置。我不想知道庫的內部,只是所需的配置設置,沒有在對象字面格式中記錄。 – scipilot

相關問題