ruby on rails - What is reform's rationale for validation not being in models? -


the reform gem advocates not having validations within models. instead, advocates them being in form.

validations models

...

sometimes when still keep validations in models (which shouldn't) copying them form might not feel right. in case, can let reform automatically copy them.

...

be warned not encourage copying validations. should rather move validation code forms , not work on model directly anymore.

what rationale reform gem project, or authors, give not having validation in models?

i split validations 2 different categories:

  • data integrity
  • business logic

data integrity means model can't used without meeting criteria. need ensure model never considered valid in state. of time concerns things such unicity persistence related. these constraints better enforced @ database level rather in model.

business logic on other hand directly related functional needs of application.

let's take example: when user signs want him fill e-mail when edits profile want make sure fills in first name , last name.

considering validations belong model, need start cluttering conditional validations. here check before validating if model persisted or not , trick. add more forms different requirements , becomes hell manage.

that because validation logic depends on context of application (the form being filled user, api endpoint being called ...). want validate parameters required application context valid, not model state.

things become lot more easy , maintainable if set-up different sets of validation each context. leads bit more code write lot cleaner , maintainable logic.


Comments