-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathrails.validations.simple_form.bootstrap4.coffee
35 lines (29 loc) · 1.46 KB
/
rails.validations.simple_form.bootstrap4.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
###!
* Client Side Validations - SimpleForm (Bootstrap 4) - v<%= ClientSideValidations::SimpleForm::VERSION %> (https://github.com/DavyJonesLocker/client_side_validations-simple_form)
* Copyright (c) <%= DateTime.now.year %> Geremia Taglialatela, Brian Cardarella
* Licensed under MIT (https://opensource.org/licenses/mit-license.php)
###
ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] =
add: (element, settings, message) ->
@wrapper(settings.wrapper).add.call(@, element, settings, message)
remove: (element, settings) ->
@wrapper(settings.wrapper).remove.call(@, element, settings)
wrapper: (name) ->
@wrappers[name] || @wrappers.default
wrappers:
default:
add: (element, settings, message) ->
wrapperElement = element.parent()
errorElement = wrapperElement.find("#{settings.error_tag}.invalid-feedback")
unless errorElement.length
errorElement = $("<#{settings.error_tag}/>", { class: 'invalid-feedback', text: message })
wrapperElement.append errorElement
wrapperElement.addClass settings.wrapper_error_class
element.addClass 'is-invalid'
errorElement.text message
remove: (element, settings) ->
wrapperElement = element.parent()
errorElement = wrapperElement.find("#{settings.error_tag}.invalid-feedback")
wrapperElement.removeClass settings.wrapper_error_class
element.removeClass 'is-invalid'
errorElement.remove()