How to validate in this case?

My application has the following models:

Form Field FieldForm Value Record

(and others that are irrelevant to the question)

In a Form, I have many Fields and for a Field, there are many Forms. The idea is to generate a form with customizable Fields. The Field has an attribute called "type" that defines what control will be rendered (input:text, input:checkbox, etc) and "format", that defines what data validation will be made. It can be a date, a phone number or something else.

The FieldForm has attributes to reference Field and Form, and also max_length, min_length, unique and required, as well. These attrs are there because they don't depend directly on Field type and can vary between forms.

The unique attribute is specially critical. It allow the user to specify what field will be used to check duplicities (for example, denying a user to register a form with the same email twice). It's a boolean field and for a Form, just one field will be "unique".

The Value model is a simple dictionary to use with the Fields that are radio, select or checkbox. A Field can have many Values.

Finally, the Record model has attributes to save the reference of a FieldForm, the reference of a Value and a data field that corresponds to the user input, if Field is something that can be manually entered, there is no need to reference Value, so it can be null or zero.

Well... The complicated part comes now. I haven't idea on how to implement the validation. If the validation is done in the controller, I don't know how to show the errors that could happen (errors.add_to_base doesn't work because 'errors' object belongs to the model). If the validation is done in the model, I'll mighty be breaking 50% of the MVC rules by including references of other models into the Record model skeleton.

For instance, this is the method I use to check duplicates:

(actually it is part of Records controller)

  private   def _exists_by_unique? form, records

    data = nil

    unique_field = FieldForm.where(:form_id => form.id, :unique => true).first

    if not records or not unique_field

      false

    else

      records.each do |record|

        if record.field_form_id == unique_field.id

          data = record.data

        end

      end

      if data

        Record.exists? ["field_form_id = ? AND data = ?", unique_field.id, data ]

      end

    end

  end

It returns true if the record is duplicate or false if it is not or there is no unique field defined.

I think it's pretty absurd to put this method in the model! And so the further validations the Records must do before saving into the db, like checking if record.data.length is in field_form.min_length..field_form.max_length and if record.data.empty? if field_form.required == true...

So, any suggestions to help me?

[ Please, have patience with me! I'm about 1 month working with Rails. Sorry for the english errors! ]

So the record corresponds to what is entered by a user filling in one of these generated forms and what you are trying to do is check that they have filled in the form respecting all the constraints setup for that form?

Fred

Don't use 'type' as an attribute, it will cause you grief. Rails will assume you are using STI and you will get some strange errors.

Colin

@Fred

Yes, that's what I want to do.

@Colin

That's just an alias for the post. In the model, its real name is "field_type".

Thanks,

Mendel

That is ok then. When posting questions it is best to stick as closely as possible to the real problem in order to avoid confusion.

Colin

@Fred

Yes, that's what I want to do.

I'd be tempted to have a non persistent model that contains the set of choices made by the user and write the validations in this model. If you use the ActiveModel stuff for your validations it should be just like writing validations on a normal model, will play nicely with form helpers etc.

Fred