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
Was this helpful?