test/errors_test.rb in reform-2.1.0 vs test/errors_test.rb in reform-2.2.0.rc1

- old
+ new

@@ -1,38 +1,58 @@ -require 'test_helper' +require "test_helper" class ErrorsTest < MiniTest::Spec class AlbumForm < Reform::Form property :title property :hit do property :title - validates :title, :presence => true + validation do + key(:title).required + end end collection :songs do property :title - validates :title, :presence => true + validation do + key(:title).required + end end property :band do # yepp, people do crazy stuff like that. property :name property :label do property :name - validates :name, :presence => true + validation do + key(:name).required + end end # TODO: make band a required object. - validate :music_taste_ok? + validation do + key(:name).required(:music_taste_ok?) - private - def music_taste_ok? - errors.add(:base, "You are a bad person") if name == 'Nickelback' + configure do + config.messages_file = "test/validation/errors.yml" + + def music_taste_ok?(value) + value != "Nickelback" + # errors.add(:base, "You are a bad person") if name == "Nickelback" + end + end end + # validate :music_taste_ok? + + # private + # def music_taste_ok? + # errors.add(:base, "You are a bad person") if name == "Nickelback" + # end end - validates :title, :presence => true + validation do + key(:title).required + end end let (:album) do OpenStruct.new( :title => "Blackhawks Over Los Angeles", @@ -53,73 +73,73 @@ "title" => "", "songs" => [{"title" => ""}, {"title" => ""}]) } # FIXME: what happens if item is missing? it do form.errors.messages.must_equal({ - :title => ["can't be blank"], - :"hit.title"=>["can't be blank"], - :"songs.title"=>["can't be blank"], - :"band.label.name"=>["can't be blank"] + :title => ["must be filled"], + :"hit.title"=>["must be filled"], + :"songs.title"=>["must be filled"], + :"band.label.name"=>["is missing"] }) end it do - #form.errors.must_equal({:title => ["can't be blank"]}) + #form.errors.must_equal({:title => ["must be filled"]}) # TODO: this should only contain local errors? end # nested forms keep their own Errors: - it { form.hit.errors.messages.must_equal({:title=>["can't be blank"]}) } - it { form.songs[0].errors.messages.must_equal({:title=>["can't be blank"]}) } + it { form.hit.errors.messages.must_equal({:title=>["must be filled"]}) } + it { form.songs[0].errors.messages.must_equal({:title=>["must be filled"]}) } it do form.errors.messages.must_equal({ - :title => ["can't be blank"], - :"hit.title" => ["can't be blank"], - :"songs.title"=> ["can't be blank"], - :"band.label.name"=>["can't be blank"] + :title => ["must be filled"], + :"hit.title" => ["must be filled"], + :"songs.title"=> ["must be filled"], + :"band.label.name"=>["is missing"] }) end end describe "#validate with main form invalid" do it do form.validate("title"=>"", "band"=>{"label"=>{:name => "Fat Wreck"}}).must_equal false - form.errors.messages.must_equal({:title=>["can't be blank"]}) + form.errors.messages.must_equal({:title=>["must be filled"]}) end end describe "#validate with middle nested form invalid" do before { @result = form.validate("hit"=>{"title" => ""}, "band"=>{"label"=>{:name => "Fat Wreck"}}) } it { @result.must_equal false } - it { form.errors.messages.must_equal({:"hit.title"=>["can't be blank"]}) } + it { form.errors.messages.must_equal({:"hit.title"=>["must be filled"]}) } end describe "#validate with collection form invalid" do before { @result = form.validate("songs"=>[{"title" => ""}], "band"=>{"label"=>{:name => "Fat Wreck"}}) } it { @result.must_equal false } - it { form.errors.messages.must_equal({:"songs.title"=>["can't be blank"]}) } + it { form.errors.messages.must_equal({:"songs.title"=>["must be filled"]}) } end describe "#validate with collection and 2-level-nested invalid" do before { @result = form.validate("songs"=>[{"title" => ""}], "band" => {"label" => {}}) } it { @result.must_equal false } - it { form.errors.messages.must_equal({:"songs.title"=>["can't be blank"], :"band.label.name"=>["can't be blank"]}) } + it { form.errors.messages.must_equal({:"songs.title"=>["must be filled"], :"band.label.name"=>["is missing"]}) } end describe "#validate with nested form using :base invalid" do - it "xxx" do + it do result = form.validate("songs"=>[{"title" => "Someday"}], "band" => {"name" => "Nickelback", "label" => {"name" => "Roadrunner Records"}}) result.must_equal false - form.errors.messages.must_equal({:base=>["You are a bad person"]}) + form.errors.messages.must_equal({:"band.name"=>["You are a bad person"]}) end end describe "correct #validate" do before { @result = form.validate( @@ -138,8 +158,8 @@ describe "Errors#to_s" do before { form.validate("songs"=>[{"title" => ""}], "band" => {"label" => {}}) } # to_s is aliased to messages - it { form.errors.to_s.must_equal "{:\"songs.title\"=>[\"can't be blank\"], :\"band.label.name\"=>[\"can't be blank\"]}" } + it { form.errors.to_s.must_equal "{:\"songs.title\"=>[\"must be filled\"], :\"band.label.name\"=>[\"is missing\"]}" } end -end \ No newline at end of file +end