Closed jeffreyguenther closed 10 years ago
Accidentally closed the issue. I'm still having the issue.
Starting to get a little closer to the cause. It appears the callbacks are never being called for uniqueness tests
judge.validate(control,
valid: (element) ->
console.log "valid"
invalid: (element, messages) ->
console.log "invalid"
)
And the issue was.... judge.validate
is an asynchronous method. The code above doesn't take that into account!
@jrguenther how do you take this into account?
When judge validates for uniqueness, it makes an AJAX call to determine if the email already exists.
My mistake was to think of the validate as a synchronous method and my code would run in sequence. You'll notice I have a loop above to validate a section of a form. Each section contains several controls. Because validate is called once for every control in the loop, I was assuming the validate
calls would return in the same sequence and I would be able to get the result in the loop. Instead, controls were returning invalid when in fact they were valid because they were returning valid after my code had moved on.
To solve the problem, I had to use a jquery deferred to provide a callback.
(".next-btn").on "click", (e) ->
e.preventDefault()
isValid = false
#controls is an object with an array of controls per id. Each id represents a section of the form.
validate_section(controls[id]).done (result) ->
console.log result
$(".participate-form__content").animate({
scrollTop: target.position().top * offset
}, 1000);
validate_section = (controls) ->
console.log controls
count = controls.length
result = true
deferred = $.Deferred()
for control in controls
validate_field(control).done((isValid) ->
console.log "Control is validated"
count--
result = result && isValid
console.log "#Result: #{result}"
console.log "Controls left: #{count}"
if(count == 0 && result)
deferred.resolve(result)
console.log "Section is good"
)
deferred.promise()
validate_field = (control)->
deferred = $.Deferred()
# add spinny part
if control.getAttribute("type") is "email"
$(".has-feedback").append("<i class=\"form-control-feedback fa fa-circle-o-notch fa-spin\"></i>")
judge.validate(control,
valid: (element) ->
console.log "called valid()"
id = element.getAttribute("id")
$("##{id}").toggleClass("invalid-field", false)
deferred.resolve(true)
invalid: (element, messages) ->
id = element.getAttribute("id")
console.log "called invalid()"
console.log messages
# Create html error messages
html_message = messages.join("<br>")
# Create a new pop over
$("##{id}").popover("destroy")
$("##{id}").popover({
# trigger: "manual"
html : true,
placement: "left",
content: html_message
}).popover('show')
I can't seem to get judge to validate my email field. If I turn off validation the field the form validates and can be submit. I have stopped using devise's
:validatable
module because of some of the custom logic I'm doing around validation. Judge seems to be responding to the uniques query fine. The user doesn't exist.Model:
The generated HTML is:
Any idea what the issue might be?