Sha256: 027013bc3fe1a98a2d2d3f9cec36e3cbe913edc33cf1028319dc7f926de9f7af

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

# bring in ValidationSyncFormHelper module
<% environment.context_class.instance_eval { include ValidationSyncFormHelper } %>

ValidationSync =

	init: ->
		<% find_all_models.each do |model| %>
			# validate if a relevant form is present
			ValidationSync.validate<%= model[:name].camelize %>() if $(".new_<%= model[:name] %>[data-sync-validations='true'], .edit_<%= model[:name] %>[data-sync-validations='true']").length
		<% end %>



	# build a validateModel() method for each ActiveRecord model
	<% find_all_models.each do |model| %>

		validate<%= model[:name].camelize %>: ->

			# trigger validation
			$(".new_<%= model[:name] %>[data-sync-validations='true'], .edit_<%= model[:name] %>[data-sync-validations='true']").validate

				# what attributes should we validate?
				rules:
					
					<% model[:attributes].each do |attr| %>
						"<%= model[:name] %>[<%= attr %>]": 
							required: <%= check_model_validation(model[:name], attr, ActiveRecord::Validations::PresenceValidator) %>
							number: <%= check_model_validation(model[:name], attr, ActiveModel::Validations::NumericalityValidator) %>
							minlength: '<%= get_validation_attr(model[:name], attr, ActiveModel::Validations::LengthValidator, :minimum) %>'

						<% unless get_validation_attr(model[:name], attr, ActiveModel::Validations::LengthValidator, :maximum).nil? %>
							maxlength: '<%= get_validation_attr(model[:name], attr, ActiveModel::Validations::LengthValidator, :maximum) %>'
						<% end %>
					<% end %>


				# what error messages should show?
				messages:

					<% model[:attributes].each do |attr| %>
						"<%= model[:name] %>[<%= attr %>]": 
							required: "<%= get_validation_message(model[:name], attr, ActiveRecord::Validations::PresenceValidator) %>"
							number: "<%= get_validation_message(model[:name], attr, ActiveModel::Validations::NumericalityValidator) %>"
					<% end %>

	<% end # find_all_models.each %>



# Fire init() on ready (page:load for turbolinks)
$(document).on('ready page:load', ValidationSync.init)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
validation_sync-0.0.4 app/assets/javascripts/validations.js.coffee.erb
validation_sync-0.0.3 app/assets/javascripts/validations.js.coffee.erb
validation_sync-0.0.2 app/assets/javascripts/validations.js.coffee.erb