Method: ARFormWidget#validate

Defined in:
lib/cuca/stdlib/arform.rb

#validate(variables) ⇒ Object

Validate will check on ActiveRecord validation errors



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cuca/stdlib/arform.rb', line 66

def validate(variables)
  form  if @_content.empty? # password fields might write hints to the validator...
  clear
  @form_errors = {}
  p = variables
  p.delete(@submit_name)
  
  if @model.new_record? then 
     @disabled_on_create.each { |d| p.delete(d) }
     @hidden_on_create.each { |d| p.delete(d) }
  else 
     @disabled_on_update.each { |d| p.delete(d) }
     @hidden_on_update.each { |d| p.delete(d) }
  end

  # don't save empty passwords!!
  @password_fields ||= []
  @password_fields.each do |pwf|
     p.delete(pwf) if p[pwf].chomp.empty?
  end

  # remove possible additional data that model doesn't support to
#   p.delete_if { |k,v| [email protected]_to?("#{k}=") }

  column_names = @model.class.columns.map { |c| c.name }
  p.each do |k,v|
    @model.send("#{k}=".intern, v) if (column_names.include?(k) && @model.respond_to?("#{k}=")) || @save_attribs.include?(k)
  end
  # @model.attributes = p
  
  return true if @model.valid?

  @model.errors.each do |k,v|
     @form_errors[k] = v
  end
end