Class: BetterAjaxValidationController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/installed_files/better_ajax_validation_controller.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) ajax_validate_attribute



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/installed_files/better_ajax_validation_controller.rb', line 2

def ajax_validate_attribute
	model = params[:model]
	klass = model.to_s.camelize.constantize
	object = klass.new(params[model])

	render :update do |page|
		# If the model is valid on the given attribute
		if (errors = klass.better_valid_on?(params[model])).blank?
			# Mark the field as valid
			page << "markFieldValid('#{params[:field_id]}');"
		else
			# Mark the field as invalid
			page << "markFieldInvalid('#{params[:field_id]}', '#{escape_javascript(errors[0].to_s)}');"
		end
	end
end

- (Object) ajax_validate_model



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/installed_files/better_ajax_validation_controller.rb', line 19

def ajax_validate_model
	model = params[:model]
	klass = model.to_s.camelize.constantize
	object = klass.new(params[model])

	# Pull out the object's @errors hash
	errors = object.errors.instance_variable_get("@errors")

	invalid = false
	params[model].each do |attribute|
		if errors.include?(attribute[0])
			invalid = true
		end
	end

	render :update do |page|
		# If the all validations are passed
		if invalid
			page << "validForm = false;"
			# Mark each invalid field as invalid
			errors.each do |attr, error|
				page << "markFieldInvalid('#{model}_#{error[0].instance_variable_get('@attribute')}', '#{escape_javascript(error[0].to_s)}');"
			end
		else
			page << "validForm = true;"
		end
	end
end