我在注册场景中拥有一个Alfresco Share客户端表单,我想验证是否选择了用户名。
我可以添加一个Alfresco表单验证器以同步的方式吗?
到目前为止,我的表单是这样的:
this.widgets.saveForm = new Alfresco.forms.Form(this.id +-form); this.widgets.saveForm.addValidation(Dom.get(this.id +-username),this.validateUsername,me,blur,this.msg(validation-hint.username-taken) );但 this.validateUsername 需要创建一个 Alfresco.util .Ajax.request 添加到回购端Web服务,该服务将检查所选用户名的可用性。所以基本上,到了响应时间为止,我的原始validateUsername方法已经完成,返回false(很可能)。
到目前为止,我的进度是禁用提交按钮(这就是为什么我将 me 传递给验证处理程序的原因),并且如果用户名无误,则使用成功/失败回调函数启用它。
如果有更好的方法来设置验证处理程序的作用域为 this ,而不是传递我(这也是可行的,这是为了确保我不会错过更好的方式)。
$ b 解决方案我的解决方法是这样的: $ b
为表单添加一个自定义验证,并为验证器函数传递var me = this以获取作用域。
this.widgets.saveForm.addValidation(Dom .get(this.id +-username), this.validateUsername ,我,blur,this.msg(validation-hint.username-taken));
添加一个验证处理函数,它将a。禁用提交按钮并将检查CSS类设置为适当的形式以供用户获得反馈。代码片段:
validateUsername:function Reg_validateUsername(el,me){ var val = this.fieldId.value; var username = trim(val); me.widgets.registerButton.set(disabled,true); Dom.addClass(me.id +-username-img,checking); Dom.removeClass(me.id +-username-img,hidden); Alfresco.util.Ajax.request({ url:/ alfresco / service / slingshot / register / username-available?username =+ username ,method:GET,successCallback:{ fn:me.onUsernameAvailable ,范围:me } ,failureCallback:{ fn:me.onUsernameTaken ,范围:me } });
写一个后端webscript username-available.get 只有当用户名是免费的(并且是有效的,即使我有另一个验证程序),也能返回成功。
这就是我最终这样做的结果。
>I have an Alfresco Share client-side Form in a registration scenario and I want to validate if the chosen username is taken.
Can I add a Alfresco forms validator in a synchronous way?
So far my form goes something like this:
this.widgets.saveForm = new Alfresco.forms.Form(this.id + "-form"); this.widgets.saveForm.addValidation(Dom.get(this.id + "-username"), this.validateUsername, me, "blur", this.msg("validation-hint.username-taken"));But this.validateUsername needs to make an Alfresco.util.Ajax.request to a repo-side web service which checks availability for the chosen username. So basically, by the time the response is here, my original validateUsername method is long-finished, returning false (likely).
My progress so far is to disable the submit button (that is why I am passing "me" to the validation handler), and have success/failure callbacks enable it if the username is fine.
What would be a decent way to make a synchronous validator?
A side question would be if there is a better way to set the scope of the validation handler to "this", as opposed to passing me (this one works too, this is just to make sure I am not missing a better way).
解决方案My workaround solution is this:
Add a custom validation to the form, and pass the "var me = this" for the validator function to get the scope.
this.widgets.saveForm.addValidation(Dom.get(this.id + "-username"), this.validateUsername, me, "blur", this.msg("validation-hint.username-taken"));
Add a validation handler function which will a. disable the submit button and set the "checking" CSS class to the appropriate form for the user to have feedback. Code snip:
validateUsername: function Reg_validateUsername(el, me) { var val = this.fieldId.value; var username = trim(val); me.widgets.registerButton.set("disabled", true); Dom.addClass(me.id + "-username-img", "checking"); Dom.removeClass(me.id + "-username-img", "hidden"); Alfresco.util.Ajax.request({ url: "/alfresco/service/slingshot/register/username-available?username=" + username , method: "GET" , successCallback: { fn: me.onUsernameAvailable , scope: me } , failureCallback: { fn: me.onUsernameTaken , scope: me } });Write a backend webscript username-available.get that will return success only if the username is free (and valid, even though I have another validator for that).
onUsernameAvailable will set the submit button to enabled again and run validation again (because of other fields) and change the CSS class to "valid".
onUsernameTaken will set the CSS class to "taken".
That's how I ended up doing this.
更多推荐
Alfresco分享客户端同步表单验证
发布评论