MVC2 Custom Editor Template outer validation box - asp.net

I've got a custom Editor Template that is essentially:
<div id="control">
<%: Html.DropDownListFor(model => model.Day, Model.Days)%>
<%: Html.DropDownListFor(model => model.Month, Model.Months)%>
<%: Html.DropDownListFor(model => model.Year, Model.Years)%>
</div>
Whilst my validation is successful/fine with this control (ValidationMessageFor works) I have been unable to find how to highlight the control when validation fails (e.g. with a TextBoxFor the textbox border goes red if validation fails)
Does anyone know how I can add this behaviour with a custom editor template please?

Interesting question. If you can detect a validation error, then you should be able to apply the appropriate class. Something like this, substitute in your own field name:
<div id = "control" class="<%=ViewData.ModelState.IsValidField("DateField") ? "" : "validation-error" %>">

Related

Rails 7 using hotwire to replace a form element

Context: a form has a collection_select, but without a value that interests the user.
A second form allows to create a new entry that would populate the collection_select with a desired value.
Class Article has_many :tags
The create turbostream does render the object, but the form does not display the change & I suspect it is due to the atomicity of the form. the IDed div tag was tried both outside and inside the form_fields tag to the same neutered effect.
<%= form.fields_for :tags do |tag| %>
<div id='tags'>
<%= f.collection_select(:tag, #tags, :id, :name) %>
</div>
<% end %>
The turbo_stream file tris to replace the target. If f.collection_select is used, this generates an error as rails, handling the snippet does not know of the form's existence. Using select_tag, a tag is rendered and delivered but the div is not refreshed
<%= turbo_stream.replace "tags" do %>
<div class='fade-in-div'>
<%= select_tag :tag, options_from_collection_for_select(#tags, :id, :name) %>
</div>
<% end %>
How can these options for select be updated with hotwire?
Functional answer that does not answer the question:
• the new data needs its own field
• make that an allowed attribute with the relevant accessor atrr_accessor
• process the new value
• update the record
• choose preferred path: redirect or turbo_stream a partial as a replacement of entire form.

Capybara rspec inconsistent behaviour when trying to click buttons / submit forms

This is something that leads me to waste hours and I wish I knew why. I've found lots of examples (not all answered) where forms are not being submitted, it works on a webpage, but not in capybara. things like find/xpath work in the irb.
web page
file box with a upload button, attaching the file works (i'm using poltergeist render to see that file name is attached)
debugging / options tried
find () works e.g. in irb it returns an object: Capybara Element tag=input
if i attempt to "visit '' it returns {"status"=>"fail", "click"=>{"x"=>124, "y"=>283}} [nothing in the log that action called. DEFINITELY works in development environment]
click_on 'Upload' fails: {"status"=>"fail", "click"=>{"x"=>124, "y"=>283}}
find_button('Upload').click: {"status"=>"fail", "click"=>{"x"=>124, "y"=>283}}
find("#upload_bill_button1").trigger("click"): returns "click"
i have tried exec_script of javascript checkBill() . page.execute_script('checkBill()'): returns nil
find(:css, '#upload_bill_button1').click . returns: {"x"=>124, "y"=>282}
nothing in the logs to show the action is being completed
i have
js=>true as its a javascript submit (does a test first, code below).
made sure all ids are unique
capybara::DSL added
system setup
Mac ox X
poltergeist (working for all my other tests)
ruby 1.9.3
phantom 1.9.2
is there further debug to figure out the problem ? other logs i'm missing?
any guidance much appreciated
<div class="row">
<div class="large-10 columns" >
<%=form_tag( "/upload_bill", :method=>:post, :multipart => true, :id => "form_upload1") do -%>
<fieldset>
<legend>Select your bill file</legend>
<%=file_field_tag 'bill', :onclick => "checkBill()" -%>
<div class="large-2 columns" >
<!-- TODO: make the browse button the zurb class type -->
<%- if params[:action].eql?( 'index')%>
<%=submit_tag "Upload", :onclick => "checkBill();return false;", :id => "upload_bill_button1", :class => "button postfix" -%>
<% end %>
</div>
</fieldset>
<% end %>
</div>
</div>
<%- if params[:action].eql?('import_bill')%>
<%=link_to (image_tag "/images/btn-uploadbill.png"), "#", :onclick=>"checkBill();return false;", :id => "upload_bill_button2" -%>
<%=link_to (image_tag "/images/btn-cancel.png"), root_path %>
<%- end %>
<script type="text/javascript">
function checkBill(){
var bill = $("#bill").val();
if(bill.split('.').reverse()[0]=="csv"||bill.split('.').reverse()[0]=="pdf"||bill.split('.').reverse()[0]=="pdftranslatedtxt"){
//success bill supported, data send to app
jQuery('#form_upload1').submit();
}
else if(bill==""){return false; }
else{// return error wrong format of bill alert('We only currently support PDF or CSV file format.')
}
}
</script>
UPDATE
I have the code working on some instance of this page. I have also have one situation where the find/trigger is working (the logs show actions, but the poltergeist:render shows no page update)
so, rspec function is
def new_import_bill(billfile)
path = DATA_TEST+billfile
attach_file("bill", path)
find("#upload_bill_button1").trigger("click")
end
I have debugged the page output e.g.
puts page.html
and there is no difference between the page html that works and the pages that do not work., but in certain situations it refuses to click or the poltergeist.render does not update.
as per this poltergeist bug (https://github.com/jonleighton/poltergeist/issues/209) I have tried removing full:true. No difference
has anyone else come across these inconsistences?

ASP.Net MVC 4 & Unobtrusive Validation Summary

I am trying to display a summary error message on a form as well as property level errors.
The property errors get rendered using html.validationmessagefor(model =>...) which works fine.
But when there is one or more validation errors, I want html.ValidationSummary(true) to display the message "Your form is missing some details - see below".
There also might be some server side validation which will occur post submit and will be added with ModelState.AddError.
How can I get a class level dataattribute (presumably using [AttributeUsage(AttributeTargets.Class)]) to display in the summary validation using unobtrusive validation?
Is this what you are looking for:
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
#Html.ValidationSummary("Errors:")
<div>
#Html.EditorFor(model => model.PathToExcel)
#Html.ValidationMessageFor(model => model.PathToExcel)
</div>
<div>
<input type="submit" value="Load" />
</div>
}
this uses 2 ValidationSummary's, one to fill the ValidationMessageFor-fields and one to use a Summary. Summary only works after Submitting.

How do I conditionally render a form in Razor?

When I use the following code, I get a friendly error message (as friendly as YSCD's get) telling me I shouldn't use '#', yet when I don't use it, my form declaration renders as literal Razor code, not as the intended HTML elements. What am I doing wrong?
#if (Model.Step == Trocrates.Web.Models.PasswordResetModel.PasswordResetSteps.StartRequest)
{
#using (Html.BeginForm()) { Html.ValidationSummary(true);
<fieldset style="border: 0px;">
<div class="editor-label">
#Html.LabelFor(model => model.UserName);
</div>
<div class="editor-field">
Html.EditorFor(model => model.UserName) Html.ValidationMessageFor(model => model.UserName)'
<input type="submit" value="Log In" />
#Html.ActionLink("Send", "BeginResetPassword", "Account")
</div>
</fieldset>
}
}
Sorry readers, it was originally that less visible open brace glyph on the same line as BeginForm that caused confusion. When I close that things fell back into place.
Did you try removing the # symbol on the using statement? Since you're already in code mode because of the if block the # symbol doesn't mean anything there. That's the only thing off hand that looks out of place to me.

Can I assign HTML values in MVC view using Html Helpers?

Is this right? I am trying to display value in input box dynamically?
can anyone advice me is this corect approach? but still I am getting here only + + in input box?
Html.DisplayFor will render a label in this case. If you want to write in this way just use <%= Model.Date.ToString() %> for the value attribute of the input.
These HTML helpers will render the markup for you, don't try and use them as methods to return data. You can get the data by just using <%=Model.MyProperty%> as long as it is a strongy-typed view.
Try just using <%= Html.EditorFor(m => m.Date) %>
OR
<%= Html.TextBoxFor(m => m.Date) %> (the EditorFor will automatically render a textbox anyway)
OR
<%= Html.TextBox("Date", Model.Date) %> (this is not a strongly-typed helper, you're doing the data binding yourself with the second argument)
Maybe do you want this?
<input
type="text"
id="Date-<%=Model.ID%>"
value= " <%= "+" + Model.Date.ToString() + "+" %>" />
I don't know what Model is for you, but something like this might help you, if it is an object of a class that has some property Date.

Resources