How should I validate the presence of fields that create an instance that doesn't have a model?

Starkers

A sign in form simply populates the sessions hash, and so doesn't have a model. As such, I don't know where to put validations for the sign in form. How should I validate presence of the username and password? Should I just do it with javascript on the clientside? I suppost that makes sense, how could I iterate through the errors hash if it were to fail validations?

Talgat Medetbekov

Really simple, you can realize it using ActiveModel. Just create a class as below and add some validations

class Session
  include ActiveModel::Validations
  extend ActiveModel::Naming

  attr_accessor :username, :password
  validates_presence_of :username, :password

  def initialize(attrs = {})
    attrs.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

s = Session.new(username: 'Abc')
=> #<Session:0x000000058a3270 @username="Abc">
s.valid?
s.errors
=> #<ActiveModel::Errors:0x000000058ad900 ... @messages={:password=>["can't be blank"]}>

And your form should automatically show error messages if exists.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ecto - validate presence of associated model

From Dev

Why doesn't UnboundMethod have the instance method `source` since it has `source location`? And how can I hack this?

From Dev

Django doesn't validate on create?

From Dev

stripe checkout doesn't validate required fields

From Dev

How do I create a default fallback profile for any process that doesn't have one in AppArmor?

From Dev

How to create a instance of UserCredential if I already have the value of Access Token?

From Dev

How to create a instance of UserCredential if I already have the value of Access Token?

From Dev

How do I create an instance of a model in views.py?

From Dev

How to validate presence in Phoenix framework?

From Dev

How can I create an instance of one model as soon as an instance of a different model is created?

From Dev

Can/Should I create a owl/rdfs class that would have only 1 instance

From Dev

how to validate single field in form when i have 5 fields using javascript?

From Dev

With parsley.js, how can I validate fields that have been added after page load?

From Dev

Django doesn't detect the model I try to create

From Dev

Autofill Django model fields when create instance of another model

From Dev

reviewing the fields I have in a model in rails

From Dev

Should I have an encapsulating ViewModel for each Model?

From Dev

How can I create an instance of a derived class from an instance of a base class and include private fields?

From Dev

Create a new model which have all fields of currently existing model

From Dev

Can't create new instance of django model

From Dev

ModelForm won't create instance of model

From Dev

AttributeError: "QuerySet" doesn't have attribute 'model'

From Dev

How can I validate nested model?

From Dev

CakePHP - How can add more validate fields to an existed model?

From Dev

How can I select the item that doesn't have attribute

From Dev

How can I select the item that doesn't have attribute

From Dev

Django doesn't create models fields

From Dev

Should I create controller actions if they won't have their own page(view)?(what are best practices?)

From Dev

Rails - how to validate presence of attribute AFTER creation

Related Related

  1. 1

    Ecto - validate presence of associated model

  2. 2

    Why doesn't UnboundMethod have the instance method `source` since it has `source location`? And how can I hack this?

  3. 3

    Django doesn't validate on create?

  4. 4

    stripe checkout doesn't validate required fields

  5. 5

    How do I create a default fallback profile for any process that doesn't have one in AppArmor?

  6. 6

    How to create a instance of UserCredential if I already have the value of Access Token?

  7. 7

    How to create a instance of UserCredential if I already have the value of Access Token?

  8. 8

    How do I create an instance of a model in views.py?

  9. 9

    How to validate presence in Phoenix framework?

  10. 10

    How can I create an instance of one model as soon as an instance of a different model is created?

  11. 11

    Can/Should I create a owl/rdfs class that would have only 1 instance

  12. 12

    how to validate single field in form when i have 5 fields using javascript?

  13. 13

    With parsley.js, how can I validate fields that have been added after page load?

  14. 14

    Django doesn't detect the model I try to create

  15. 15

    Autofill Django model fields when create instance of another model

  16. 16

    reviewing the fields I have in a model in rails

  17. 17

    Should I have an encapsulating ViewModel for each Model?

  18. 18

    How can I create an instance of a derived class from an instance of a base class and include private fields?

  19. 19

    Create a new model which have all fields of currently existing model

  20. 20

    Can't create new instance of django model

  21. 21

    ModelForm won't create instance of model

  22. 22

    AttributeError: "QuerySet" doesn't have attribute 'model'

  23. 23

    How can I validate nested model?

  24. 24

    CakePHP - How can add more validate fields to an existed model?

  25. 25

    How can I select the item that doesn't have attribute

  26. 26

    How can I select the item that doesn't have attribute

  27. 27

    Django doesn't create models fields

  28. 28

    Should I create controller actions if they won't have their own page(view)?(what are best practices?)

  29. 29

    Rails - how to validate presence of attribute AFTER creation

HotTag

Archive