Can “field-with-errors” be attached to the parent of the input tag that raises the error? - ruby-on-rails

So I have an input element like this. The wrapping element is about, you know, a visual thing.
<div class="input-wrap">
<input class="blah-blah" />
</div>
When the <input> contains the error, it'll be like this:
<div class="input-wrap">
<div class="field-with-errors">
<input class="blah-blah" />
</div>
</div>
But what I want to do is:
<div class="input-wrap field-with-errors">
<input class="blah-blah" />
</div>
I found this page, it's very close to my question
Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?
Now I know I can throw
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
"#{html_tag}".html_safe
}
to avoid making a wrapping tag around the <input> tag that has an error on. But what I really wanna do is, again, adding "field-with-errors" class on the direct parent of the <input> tag. Can I do that? Does ActionView hold the tree structure of DOM Nodes?

You can put the code for handling errors wherever you like, just call it as a block on the instance variable, for example
if #instance.errors.any?
<div class="field with errors">
#instance.errors.full_messages.each do |msg|
<p><%= msg %></p>
end
end
If you user this a lot, it's good to pull it out into a helper and pass in the instance variable as a parameter.

Related

Grails formRemote updating wrong div

Having a comment within a comment but I'm having trouble in updating via form remote in correct place
I have this code
<div id="comment">
<g:render template="comment" var="comment" collection="${review.comment}" />
</div>
<g:formRemote class="ui comment form" name="commentForm"
url="[controller: 'game', action: 'addComment']" update="comment">
The problem is it's updating correctly in the database. but In the view it is only updating in the top most parent comment and not the correct comment
Sample pictures:
After clicking Add comment button :
After refreshing the page:
Don't mind the miss arrangement on the last picture I have a little sorting problem which I'm gonna fix later, the problem is the formremote updating the wrong one. The last picture is just to show that the update in the database is correct
edit:
Here is the action and template
def addComment(){
gameService.addComment(params.comment, params.gameId, params.userId, params.reviewId)
def comment = gameService.listComment(params.gameId,params.reviewId)
render(template: 'comment', collection: comment, var: 'comment')
}
template:
<div class="comment">
<a class="avatar"> <img
src="${createLink(controller:'user', action:'avatar_image', id:"${comment.user.id}" )}" />
</a>
<div class="content">
<g:link class="author" controller="user" action="userProfile"
params="${[userId:"${comment.user.id}"]}">
${comment.user.name }
</g:link>
<div class="metadata">
<span class="date"> ${comment.date }
</span>
</div>
<div class="text">
${comment.comment }
</div>
</div>
Looks to me like your first piece of code already is used multiple times, resulting in more than one div with the id "comment" (I guess that because you have multiple textareas and thus multiple forms in your screenshots, and this would totally explain your problem).
So, if you have multiple div-tags with the same id, the formRemote places the returned html inside the first one.
Edit: damn, just noticed that this question is quite old :-/

How to create a wrapper component / directive?

I have some markup I would like to cover with a simple wrapper. In this case, the idea is to wrap two <input> elements within some markup, so that this markup:
<div class="group-class">
<div class="first-class">
<input id="first">
</div>
<div class="second-class">
<input id="second">
</div>
</div>
would be marked up like this instead:
<my-wrapper>
<input id="first">
<input id="second">
</my-wrapper>
I initially thought of a component, but that would create a shadowDOM, and thus would not behave the same way as if <my-wrapper> didn't exist. <my-wrapper> tag should replace itself with the content of the first example, so that the DOM rendered is the same. I believe I saw some replace option for an angular-directive, but I am very unsure how to do this.

AngularJs not working with dynamic html

In my cshtml file I have a form named 'ApplyMedicalMain' and I want to show a dynamically loaded division when the form is dirty but its not happening even though the form is dirty ...
Below is my code that i got in Firefox Inspect Element:
<form class="form_section ng-dirty ng-valid ng-valid-required" name="ApplyMedicalMain" method="post" action="/MVC/Quote/ApplyMedical">
<div id="Step1_PartialView" class="QuoteStep1">
<script type="text/javascript" src="/Scripts/Renderings/Presales/ApplyMedical.js">
<div name="Conditions" id="conditions_or_symptoms" ng-hide="ApplyMedicalMain.$dirty">
<div class="generic_error_message select">
<div class="error_icn_message"></div>
</div>
As you can see above, I have mentioned ng-hide for the division name='conditions' but it is not getting hidden even though the form has class 'ng-dirty'.And please note that the the division 'conditions' is loaded dynamically from other partial view.
can someone help me ?
You forgot to wrap everything in a 'ng-app' container, here is a working example (I cleaned a bit the code)
<div ng-app>
<form class="form_section ng-dirty ng-valid ng-valid-required" id="ApplyMedicalMain" method="post">
<div ng-hide="ApplyMedicalMain.$dirty">Hidden when dirty</div>
<div ng-show="ApplyMedicalMain.$dirty">Shown when dirty</div>
</form>
</div>
I'm not entirely sure why your example isn't working, but I would suggest you use css to do your hiding/showing rather than using angular's bindings:
form.ng-dirty .hide-on-dirty{
display:none;
}
And then:
<form class="form_section ng-dirty ng-valid ng-valid-required" name="ApplyMedicalMain" method="post" action="/MVC/Quote/ApplyMedical">
<div id="conditions_or_symptoms" class="hide-on-dirty"> ... </div>
</form>
That's a bit more efficient that creating a binding to the form controller's state. But its just a thought. There might be more of a reason why you're wanting to do the binding.

Successive form submission without losing data in Rails?

I am confused between the controller and views interaction. I have this initial form which validates the csv file uploaded. (POST form).
After this form is validated successfully, I give the user the option to confirm the details which, and this confirm button acts as another form.
The thing is I want to keep the details from the previous form values saved in the params hash. So basically I want to perform a merge with the second form.
Is this possible? If so, can you help me with the code for the second form cause currently it overrides the previous form. Both forms point to the same function in the controller.
<% unless #contents.blank? || #errors.present? %>
<form name="confirm_bulk_order" method="post" enctype="multipart/form-data" class="search line" action="/orders/create_bulk_order" id="confirm_bulk_order">
<div class="search-btn-align" id="confirmButton">
<input type="submit" name="confirm_bulk_order" value="Confirm Order" class="lmargin10 uiButton">
</div>
</form>
<% else %>
<form name="upload_bulk_order_csv" method="post" enctype="multipart/form-data" class="search line" action="/orders/create_bulk_order" id="upload_bulk_order_csv">
<div class="fileformField">
<span class="formlabel"> Upload CSV File: </span>
<input class="required" required="true" type="file" name="datafile"/>
</div>
<div class="search-btn-align" id="uploadButton">
<%= submit_tag 'Validate Bulk Order', :class => 'lmargin10 uiButton' %>
</div>
</form>
<% end %>
In controller orders
def create_bulk_corder
if #errors.blank? and params[:confirm_bulk_order]=="Confirm Order"
#Send the final REST order call
else
#contents = read_csv_file(params[:datafile]) if params[:datafile].present?
validate_order(#contents)
#Populate #errors etc, etc
....
....
end
render
end
What all changes must I make for this to be possible?
You're losing your params when the confirm button is pressed because they're not part of the confirm form. You could avoid this by adding a hidden field:
<form name="confirm_bulk_order" method="post" enctype="multipart/form-data" class="search line" action="/orders/create_bulk_order" id="confirm_bulk_order">
<!-- This assumes that #contents is a simple value... it's probably not, so you might need several hidden_field_tags here, one for each part of #contents that you want in your params -->
<%= hidden_field_tag "contents", #contents %>
<!-- You probably also want to show the user some info about their submission here -->
<div class="search-btn-align" id="confirmButton">
<input type="submit" name="confirm_bulk_order" value="Confirm Order" class="lmargin10 uiButton">
</div>
</form>
If you go with this approach, be sure to re-validate params[:datafile] after submission, since a malicious user could change this param value to get around your validation logic.
Also, I suggest factoring your action into two separate actions, and likewise for the views. You've got if-else blocks where you really ought to have separate files.
I suggest maybe using a gem that deals with multi paged forms or wizards: https://www.ruby-toolbox.com/categories/rails_wizards

custom validation markup in Rails(3)

I'm trying to accomplish the following mark-up for all my form elements
<div class="input-container">
<label>Topic Title</label>
<div class="input-holder">
<input type="text" />
</div>
</div>
<div class="textarea-container">
<label>Post</label>
<div class="textarea-holder">
<textarea></textarea>
</div>
</div>
invalid fields mark-up:
<div class="input-container alert">
<label>Topic Title</label>
<div class="input-holder">
<input type="text" />
</div>
</div>
<div class="textarea-container alert">
<label>Post</label>
<div class="textarea-holder">
<textarea></textarea>
</div>
</div>
here's my current haml markup:
.input-container
= f.label :title, 'Topic Title'
.input-holder
= f.text_field :title
.textarea-container
= f.label :body, 'Post'
.textarea-holder
= f.text_area(:body, :size => "60x10")
Now what would I need to do if I want the container divs to have the alert class when a field in my form is invalid?
I want to do the same thing but unfortunately haven't figured out a clean way to handle it.
You can override ActionView::Base.field_error_proc, but that doesn't help with the enclosing elements.
It can be done manually by checking errors on each field ('post.errors['body'].nil?') and outputting your alert class conditionally.
The next step could be extracting the logic into a view helper, and perhaps some further abstraction after that.
But it would still be nice to do this in a more automated, Rails-y fashion.
It might be best to use either the formtastic or simple_form gems which out of the box support highlighting an individual invalid field.
I'm almost certain they do this by changing the CSS class on the field or its container, which you could also hook into in your stylesheets.

Resources