DavyJonesLocker / client_side_validations

Client Side Validations made easy for Ruby on Rails
MIT License
2.69k stars 403 forks source link

Turbolinks 5 compatibility #661

Closed tagliala closed 7 years ago

tagliala commented 8 years ago

Turbolinks 5 changed event names, so we need to adjust something in this gem

Current issues

  1. There is a Turbolinks.supported property which should be checked before setting the right event to listen to, so the current implementation is bugged on browsers which don't support Turbolinks when Turbolinks is loaded
  2. Turbolinks 5 uses turbolinks:load instead of page:change. Ref: https://github.com/DavyJonesLocker/client_side_validations/issues/645#issuecomment-223821284

    Approaches

  3. Go back to the old implementation and use a plugin with support for the latest turbolinks version. Cons: we need two different plugins, one for turbolinks and another one for turbolinks-classic. We risk to launch multiple events if gems like jquery.turbolinks are active (ready and page:load).
  4. Add other assets in the main gem (rails.validations.jquery, rails.validations.turbolinks_classic and rails.validations.turbolinks) which add the proper event to listen to. Cons: we will break backward compatibility. And CSV is not properly using SemVer, so 👋 ...
  5. YOUR IDEA HERE
tagliala commented 8 years ago

I came up with

# Determine the proper event to listen to
#
# Turbolinks and Turbolinks Classic don't use the same event, so we will try to
# detect Turbolinks Classic by the EVENT hash, which is not defined
# in the new 5.0 version.
window.ClientSideValidations.event =
  if window.Turbolinks? and window.Turbolinks.supported
    if window.Turbolinks.EVENTS?
      'page:change'
    else
      'turbolinks:load'
  else
    'ready'

# Main hook
# If new forms are dynamically introduced into the DOM the .validate() method
# must be invoked on that form
$(document).bind window.ClientSideValidations.event, ->
  ClientSideValidations.disableValidators()
  $(ClientSideValidations.selectors.forms).validate()

Not an English native speaker, so feel free to provide a better comment

tagliala commented 8 years ago

Reopening.

This is not so simple

Ref: https://github.com/turbolinks/turbolinks/issues/4 https://github.com/kossnocorp/jquery.turbolinks/issues/56

Startouf commented 8 years ago

What are the remaining problems ? Restoring previous pages from session should restore validations ? (From someone who want to add this gem to a rails 5 app)

tagliala commented 7 years ago

I'm going to close this because I've fixed the before mentioned issues in version 4.2.4