From e2ae34f02f95e9b9709637104e91aa6276ac6893 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Thu, 7 Apr 2011 16:32:25 +0200 Subject: [PATCH] [SONAR-1973] Add validations on forms --- .../app/controllers/reviews_controller.rb | 37 +++++++++++-------- .../main/webapp/WEB-INF/app/models/review.rb | 8 ++-- .../WEB-INF/app/models/review_comment.rb | 9 +++-- .../WEB-INF/app/views/reviews/_form.html.erb | 5 ++- .../app/views/reviews/_form_comment.html.erb | 7 +++- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb index 8004460ddfd..1b1ae8e0676 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb @@ -34,9 +34,7 @@ class ReviewsController < ApplicationController @review.rule_failure_id = params[:violation_id] @review.user = current_user @review_comment = ReviewComment.new - @review_comment.user = current_user - @review_comment.review = @review - @review_comment.review_text = "Enter your review here" + @review_comment.review_text = "" render :partial => "form" end @@ -52,17 +50,18 @@ class ReviewsController < ApplicationController def create review = Review.new(params[:review]) review.user = current_user - # save! - review.save - # --> Build? - #review.review_comments.create(params[:review_comment]) - review_comment = ReviewComment.new(params[:review_comment]) + review_comment = ReviewComment.new(params[:review_comment]) review_comment.user = current_user - review_comment.review_id = review.id - review_comment.save - - params[:rule_failure_id] = review.rule_failure_id - index + review.review_comments << review_comment + if review.valid? + review.save + params[:rule_failure_id] = review.rule_failure_id + index + else + @review = review + @review_comment = review_comment + render :partial => "form" + end end def create_comment @@ -70,9 +69,15 @@ class ReviewsController < ApplicationController review_comment = ReviewComment.new(params[:review_comment]) review_comment.user = current_user - review_comment.save - - index + if review_comment.valid? + review_comment.save + index + else + @review_comment = review_comment + @rule_failure_id = params[:rule_failure_id] + # TODO Find a way to update the right DIV... + render :partial => "form_comment" + end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb index 661227a8fba..bdaaa65e512 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb @@ -20,10 +20,10 @@ class Review < ActiveRecord::Base belongs_to :user belongs_to :rule_failure - belongs_to :resource, :class_name => 'Project', :foreign_key => 'resource_id' + belongs_to :resource, :class_name => "Project", :foreign_key => "resource_id" has_many :review_comments, :order => "created_at", :dependent => :destroy - validates_presence_of :user - validates_presence_of :review_type - validates_presence_of :status + validates_presence_of :user, :message => "can't be empty" + validates_presence_of :review_type, :message => "can't be empty" + validates_presence_of :status, :message => "can't be empty" end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb index d763ae2d426..51e07df0043 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb @@ -20,8 +20,11 @@ class ReviewComment < ActiveRecord::Base belongs_to :user belongs_to :review - validates_presence_of :user - validates_presence_of :review - validates_length_of :review_text, :minimum => 1, :too_short => "Your review cannot be empty." + validates_presence_of :user => "can't be empty" + validate :comment_should_not_be_empty + + def comment_should_not_be_empty + errors.add("Comment", " cannot be empty") if review_text.strip.blank? + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form.html.erb index eb62f5a4f48..ddbd5aa8780 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form.html.erb @@ -16,7 +16,10 @@ <%= text_area :review_comment, :review_text, :id => "reviewText", :rows => 10 %>
+ onclick="new Ajax.Updater({success:'reviewFailure<%= @review.rule_failure_id.to_s -%>', failure:'reviewFailure<%= @review.rule_failure_id.to_s -%>'}, '<%= ApplicationController.root_context -%>/reviews/create?rule_failure_id=<%= @review.rule_failure_id.to_s -%>', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form), onSuccess:function(request){$('createReviewLink<%= @review.rule_failure_id.to_s -%>').style.display='none';} });"> + + <%= f.error_messages :header_message => "Can't create the review", :message => "", :header_tag => :h3 %> + <% end %> \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form_comment.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form_comment.html.erb index 3ef46ed88a6..59420400668 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form_comment.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/reviews/_form_comment.html.erb @@ -8,8 +8,11 @@
<%= submit_to_remote 'create_btn', 'Add comment', :url => { :action => 'create_comment', :rule_failure_id => @rule_failure_id }, - :update => { :success => "reviewFailure" + @rule_failure_id.to_s, :failure => "fail" } %> + :update => { :success => "reviewFailure" + @rule_failure_id.to_s } %> <%= submit_to_remote 'cancel_btn', 'Cancel', - :url => { :action => 'list', :rule_failure_id => @rule_failure_id.to_s }, + :url => { :action => 'index', :rule_failure_id => @rule_failure_id.to_s }, :update => { :success => "reviewFailure" + @rule_failure_id.to_s } %> + + <%= f.error_messages :header_message => "Can't save the comment", :message => "", :header_tag => :h3%> + <% end %> \ No newline at end of file -- 2.39.5