Model Validation

: ์œ ํšจ์„ฑ๊ฒ€์‚ฌ ๊ตฌํ˜„์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฐ€๋Šฅ์„ฑ์„ ๋ฐฐ์ œ์‹œ์ผœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ๋™์ž‘์„ ํ•˜๊ฑฐ๋‚˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•˜๋‹ค. ๋˜ํ•œ ์œ ํšจ์„ฑ์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์€ ๊ณต๊ฒฉ์„ ์ตœ์†Œํ™” ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณด์•ˆ ๋Œ€์ฑ…์ด๋‹ค.

ํด๋ผ์ด์–ธํŠธ์ธก์—์„œ ์œ ํšจ์„ฑ๊ฒ€์‚ฌ(jQuery validation)์€ ์„œ๋ฒ„๋ฅผ ๊ฑฐ์น˜์ง€์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„ ๋ถ€ํ•˜๋ฅผ ์ค„์—ฌ์ฃผ๊ฑฐ๋‚˜ ๋น ๋ฅธ ํ”ผ๋“œ๋ฐฑ์„ ์ „๋‹ฌํ•ด ์ค„ ์ˆ˜ ์žˆ์ง€๋งŒ ์–ธ์ œ๊นŒ์ง€๋‚˜ ์ผ์ฐจ์ ์ธ ๊ฒ€์‚ฌ์ด๋‹ค. ํด๋ผ์ด์–ธํŠธ์—์„œ ์˜ˆ๋น„ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผํ•ด์—ฌ ํ†ต์‹ ์ด ๋ฐœ์ƒํ•˜๋Š” ๋‚ญ๋น„๋ฅผ ๋ง‰๊ณ , ์„œ๋ฒ„์—์„œ๋Š” ์ตœ์ข… ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ .

validates field [, ...] name: params[, ...]
# filed : ๊ฒ€์‚ฌ๋Œ€์ƒํ•„๋“œ์ด๋ฆ„
# name : ๊ฒ€์‚ฌ์ด๋ฆ„
# params :๊ฒ€์‚ฌ ๋งค๊ฐœ๋ณ€์ˆ˜

save, save!, create, create!, update, update!๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ์‹œ์ ์— ์ž…๋ ฅ๊ฐ’์„ ๊ฒ€์‚ฌ์ฒ˜๋ฆฌํ•ด ๊ฒ€์‚ฌ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ด๋ฃจ์–ด ์ง€๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ˆ˜ํ–‰๋œ๋‹ค.

๋ฐ˜๋ฉด์— decrement!, decrement_counter, increment!, increment_counter, toggle!, touch, update_all, update_attribute, update_counters, update_column, save(validates: false)๋Š” ๊ฒ€์‚ฌ ์ฒ˜๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ํ•˜์ง€์•Š์•„ ๊ฐ’์˜ ์œ ํšจ์„ฑ์— ๊ด€๊ณ„์—†์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ฐ์ฒด๋ฅผ ๋ฐ”๋กœ ๋ฐ˜์˜ํ•˜๋ฏ€๋กœ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ํ™•์ธ๋œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

์œ ํšจ์„ฑ ์˜ค๋ฅ˜ ํ‘œ์‹œ

<%if @book.errors.any? %>
    <div id="error_explanation">
        <ul>
            <% @book.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

Acceptance ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ(์ˆ˜๋ฝ)

: ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉ์ž๊ฐ€ ์ด์šฉ์•ฝ๊ด€์— ๋™์˜ํ–ˆ๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•  ๋•Œ ์‚ฌ์šฉ. acceptance๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ด€๋ จ ํ•„๋“œ๊ฐ€ ํ•„์š” ์—†๋‹ค.

# controller
def user_params
    params.require(:user).permit(:agreement)
end
validates :agreement, acceptance: true
<div class="field">
    <%= f.label :agreement %><br />
    <%= f.check_box :agreement %>
</div>

acceptance๊ฒ€์‚ฌ์—์„œ ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•  ๋•Œ์˜ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

validates :agreement, acceptance: {accept: 'yes'}
<div class="field">
    <%= f.label :agreement %><br />
    <%= f.check_box :agreement, {}, 'yes' %>
</div>

confirmation ๋™์ผ๊ฒ€์‚ฌ

: ๋น„๋ฐ€๋ฒˆํ˜ธ, ๋ฉ”์ผ ์ฃผ์†Œ๋“ฑ ์ค‘์š”ํ•œ ํ•ญ๋ชฉ์„ ํ™•์ธํ•˜๊ธฐ์œ„ํ•ด ๋‘๋ฒˆ ์ž…๋ ฅํ• ๋•Œ ๋‘๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€์‘ํ•˜๋Š” ํ•„๋“œ๊ฐ€ ํ•„์š”์—†๋‹ค.

# controller
def user_params
    params.require(:user).permit(:email_confirmation, :email)
end
validates :email, confirmation: true
<div class="field">
    <%= f.label :email_confirmation %><br />
    <%= f.text_field :email_confirmation %>
</div>

Uniqueness ์ค‘๋ณต๊ฒ€์‚ฌ

: ์ค‘๋ณต๋˜์ง€ ์•Š์Œ์„ ํ™•์ธํ•œ๋‹ค.

validates :title, uniqueness: { scope: :publish}

์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํด๋ž˜์Šค ๊ณตํ†ต ๋งค๊ฐœ๋ณ€์ˆ˜

์˜ต์…˜

์„ค๋ช…

allow_nil

nil์ธ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ƒ๋žต

allow_blank

nil ๋˜๋Š” ๊ณต๋ฐฑ์ธ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ƒ๋žต

message

์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€

on

์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์‹œ์ (๊ธฐ๋ณธ์ ์œผ๋กœ save)

if

์กฐ๊ฑด์ด true์ผ๋•Œ๋งŒ ์œ ํšจ์„ฑ๊ฒ€์‚ฌ

unless

์กฐ๊ฑด์ด false์ผ๋•Œ๋งŒ ์œ ํšจ์„ฑ๊ฒ€์‚ฌ

๋‹ค๋ฅธ ๊ทœ์น™๋“ค์€ http://guides.rubyonrails.org/active_record_validations.html ์ฐธ์กฐ.

์Šคํ„ฐ๋””๊ณผ์ œ

# user.rb
class User < ApplicationRecord
    has_and_belongs_to_many :groups
    has_many :posts
    has_many :likes
    has_many :comments

    validates :name, length: { maximum: 10 },presence: true
    def name=(s)
        write_attribute(:name, s.to_s.capitalize) 
      end
    validates :age, numericality: {only_integer: true,greater_than_or_equal_to: 20, less_than: 30}, presence: true
    validates :mail, uniqueness: true,presence: true
    validates_format_of :mail,:with => /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
end
  • name: ์ฒซ๊ธ€์ž ์†Œ๋ฌธ์ž์ธ๊ฒฝ์šฐ ๋Œ€๋ฌธ์ž๋กœ ์ž๋™๋ณ€๊ฒฝ์ €์žฅ,์ตœ๋Œ€ 10๊ธ€์ž

  • age: ์ˆซ์žํ˜•์‹ 20๋Œ€(20~29)

  • mail: '์ด๋ฉ”์ผํ˜•์‹',์ค‘๋ณต์•ˆ๋จ

# post.rb
class Post < ApplicationRecord
  belongs_to :user
  has_many :comments

  validates :title, length: { in: 2..30 }, presence: true
  validates :content, presence: true
  def content=(c)
      write_attribute(:content, c.gsub!(/shit/,"****").gsub!(/fuck/,"****").gsub!(/hell/,"****"))
  end

end
  • title: ์ตœ์†Œ2๊ธ€์ž,์ตœ๋Œ€30๊ธ€์ž

  • content: ์š•์„คํ•„ํ„ฐ๋ง(**)

# comment.rb
class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :post

  validates :content, presence: true, length: {maximum: 200}
  def content=(c)
      write_attribute(:content, c.gsub!(/shit/,"****").gsub!(/fuck/,"****").gsub!(/hell/,"****"))
  end 
end
  • content: ์š•์„คํ•„ํ„ฐ๋ง(**),์ตœ๋Œ€200๊ธ€์ž

# like.rb
class Like < ApplicationRecord
  belongs_to :user
  belongs_to :post
end
# group.rb
class Group < ApplicationRecord
    has_and_belongs_to_many :users
    validates :name, length: { maximum: 20},presence: true
end
  • name: ์ตœ๋Œ€20๊ธ€์ž

Last updated