Posted at

バリデーション

More than 1 year has passed since last update.


バリデーションを定義する

app/modles/entry.rb

class Entry < ActiveRecord::Base
validates_presence_of :title
end

class Entry < ActiveRecord:Base
validates_presence_of :title, :content, :on => :create
end


バリデーション パラメータ

:message 

:on
:if
:unless
:allow_nil


バリデーション 宣言

validates_presense_of |   can't be blank 

validates_length_of | 後述
validates_numericality_of | is not anumber
validates_format_of | is invalid
validates_inclusion_of | is not included in the list
validates_exclusion_of | is reserved
validates_confirmation_of | doesn'
t match confimation
validates_uniqueness_of | has already been taken
validates_acceptance_of | must be accepted
validates_associated | is invalid

app/models/user.rb
class User < ActiveRecord::Base
validates_confimation_of :password
<%= password_field "user", "password" %>
<%=
password_confimationの入力項目も用意する %>
<%= password_field"user", "password_confimation" %>

...
end


validates_length_of

class Blog < ActiveRecord::Base

validates_length_of :title, :in => (2..30)
:too_short => "タイトルは2文字以上で入力してください。"
:too_long => "タイトルは30文字以下で入力してください。"
end

:minimum

:maximum
:is
:in/:within
:too_long
:too_short
:wrong_length
:message


validate_uniquness_of

class Entry < ActiveRecord::Base

validates_uniqueness_of :title, :scope => [:blog_id]
end

:scope
:case_sensitive