2016-03-15 127 views
0

我正在用Parsley.js(在Django中)構建一個自定義的遠程驗證器,並且我遇到了一些問題,但我陷入了一對問題。Parsley.js遠程驗證錯誤

forms.py

@parsleyfy 
class CustomUserCreationForm(UserCreationForm): 
    email = forms.CharField(label='', required=True, widget = forms.TextInput(
     attrs = { 
      'placeholder': 'E-mail', 
      'class': 'form-control', 
      'id': 'email', 
      'data-parsley-remote': '', 
      'data-parsley-remote-options': '{ "type": "POST", "dataType": "json" }', 
      'data-parsley-remote': '/check_email/', 
      'parsley-remote-method': 'POST', 
      'data-parsley-remote-validator': 'emailCheck', 
      'data-parsley-remote-message': 'The E-mail is already used.' 
     } 
    )) 

模板

<form data-parsley-validate data-parsley-remote id="user_form" method="post" action="/register/" enctype="multipart/form-data" data-parsley-trigger="focusout"> 

    {% csrf_token %} 

    {{ user_form.as_p }} 

    <input type="submit" class="btn btn-info submit" name="submit" value="Register" /> 

</form> 

而且腳本現在

Parsley.addAsyncValidator(
    'emailCheck', function (xhr) { 
     var email = $('#email').parsley(); 
     var response=xhr.responseText; 
     var jsonResponse = JSON.parse(response); 
     var jsonResponseText = jsonResponse["response"]; 

     window.ParsleyUI.removeError(email,'errorUsername'); 
     if(jsonResponseText == 'This E-mail is ok.') 
      return 200; 
     if(jsonResponseText == '404') 
      window.ParsleyUI.addError(email,'errorUsername',jsonResponseText); 
    }, '/email_check/' 
); 

,第一親瑕疵是,當我編寫現有的電子郵件時,會顯示兩條消息:一條來自表單標籤,另一條來自Django視圖(「jsonResponseText」);每當我刪除「jsonResponseText」我得到一個錯誤:「未捕獲TypeError:無法讀取未定義的屬性'名稱'」。

第二個問題是有點煩人:如果我寫一個不存在的電子郵件並填寫整個表單,當我點擊Register時,什麼都不會發生。我認爲問題在於服務器收到GET請求,而不是POST,但我不知道它來自哪裏。

我還張貼view.py文件,完整性

def register(request): 
    registered = False 

    if request.method == 'POST': 
     user_form = CustomUserCreationForm(request.POST) 

     if user_form.is_valid(): 
      user = user_form.save() 

      registered = True 

      return render(request, 'blog/registered.html') 

     else: 
      print user_form.errors 
    else: 
     user_form = CustomUserCreationForm() 

    return render(request, 'blog/post_list.html', {'user_form': user_form}) 


@requires_csrf_token 
def email_check(request): 
    email = request.POST.get('email', False) 
    if request.is_ajax(): 
     if email: 
      query_email = CustomUser.objects.filter(email=email) 
      if query_email.exists(): 
       #res = "{0} is already in use.".format(email) 
       res = 404 
      else: 
       res = "This E-mail is ok." 
      ajax_vars = {'response': res, 'email': email} 
      json_data = json.dumps(ajax_vars) 
     else: 
      res = "This field is required." 
      ajax_vars = {'response': res, 'email': email} 
      json_data = json.dumps(ajax_vars) 

     return HttpResponse(json_data, content_type='application/json') 

回答

0

不要叫addErrorremoveError,並從asynchValidator返回truefalse

+0

這解決了雙錯誤信息的問題,這是完美的。關於另一個問題的任何想法?它一直髮送GET請求,而沒有遠程驗證器,請求是POST。 –

+0

從模板中刪除「data-parsley-remote」(因爲我已經在forms.py文件中定義了它)我不再獲取GET請求,但註冊按鈕仍然不起作用。 –