LoginSignup
0
0

More than 5 years have passed since last update.

バリデーション

Posted at

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

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0