Skip to content

Instantly share code, notes, and snippets.

@achempion
Created August 10, 2016 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achempion/c3ef71cb70b3c50a928d4ee272321d64 to your computer and use it in GitHub Desktop.
Save achempion/c3ef71cb70b3c50a928d4ee272321d64 to your computer and use it in GitHub Desktop.
$ ->
$(document).on 'ajax:error', '.js-form-validation', (e, xhr, status, error) ->
$form = $(@)
resource_name = $form.attr('data-js-form-validation-resource')
$form.find('.field_with_errors').removeClass('field_with_errors')
$form.find('.error-message').removeClass('is_showed').text('')
setTimeout ->
$.each xhr.responseJSON, (key, value) ->
id_name = "#{resource_name}_#{key}"
$form.find("##{id_name}").addClass('field_with_errors')
$form.find(".error-message[for=#{id_name}]").text(value[0]).addClass('is_showed')
, 200
$(document).on 'focus', '.js-form-validation .field_with_errors', ->
$(@).removeClass('field_with_errors');
$(document).on 'change', '.js-form-accept--checkbox', ->
is_accepted = $(@).is(':checked')
$(@).closest('form').find('.js-form-accept--submit').attr('disabled', !is_accepted)
$(document).on 'ajax:success', '.js-form-success--form', (e, data, status, xhr) ->
$(@).hide(200)
$(@).parent().find('.js-form-success--block').addClass('is_showed')
$(document).on 'change', '.js-form-toggle_block--checkbox', ->
$block_for_toggle = $(@).closest('form').find('.js-form-toggle_block--block')
if $(@).is(':checked')
$block_for_toggle.addClass('is_showed')
else
$block_for_toggle.removeClass('is_showed')
$('.js-form-submit--link').on 'click', (e) ->
e.preventDefault()
$(@).closest('form').submit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment