Change class of div based on a field's validation result - forms

I want to wrap fields in a <div class="validation-error"> when they have validation errors, and in plain <div>s otherwise:
Validation Error:
<div class="validation-error">
<sf:input path="title" cssErrorClass="validation-error"/>
<sf:errors path="title" cssErrorClass="validation-error" />
</div>
No Validation Error:
<div>
<sf:input path="title" cssErrorClass="validation-error"/>
<sf:errors path="title" cssErrorClass="validation-error" />
</div>
How can I check that title has an error or not?

You shouldn't apply validation-error class to whole <div> element of your input. I don't think that <errors> tag was meant to work like that.
What you could do is very simple and neat solution. Simply display error message with all proper classes and styles applied to it or don't display it at all. For instance:
<tr>
<td><label>Username</label></td>
<td><form:input id="username" path="nickname" /></td>
<td><span id="usernameError" class="error"><form:errors path="nickname" /></span></td>
</tr>
When there is no error message, appropriate cell will be left blank and user won't see anything. However, if the message will be displayed - it will have proper styling thanks to class="error" notation in mine example.
Ohh, and you can use divs, spans or any other elements if you wish. I just tend to use table for forms.

Related

Material Design Lite *ngIf on Form Input Field Angular 5

I want to build some dynamic form fields.
When I put an *ngIf in fron of the div the material design does not work properly (no effects etc.).
Here is the input field that works
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="number" id="playerCount" [(ngModel)]="playerCount" (ngModelChange)="setPlayerCount(playerCount)">
<label class="mdl-textfield__label" for="playerCount">Anzahl Spieler</label>
</div>
and the one that does not work
<div *ngIf="players" class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="stuff">
<label class="mdl-textfield__label" for="stuff">stuff</label>
</div>
The second div should appear as soon as the first has been filled out.
You have to register new elements to MDL's componentHandler since *ngIf actually removes the element from the dom, you have to register that element every time Angular removes/inserts it.
You can either call componentHandler.upgradeAllRegistered(); every time *ngIf inserts the element or replace *ngIf with something that is just hides the element but not remove it.

Issue finding elements using Selenium::Remote::Driver in Perl

As part of a test scrip I have to redirect to a 3rd party hosted payment page. However, when I try and find the input box elements it errors slightly differently depending on which call I try:
$driver->find_element_by_id("card.billingAddress.houseNumberOrName")->clear
returns "Element is not currently visible and so may not be interacted with"
$driver->find_element_by_xpath('//input[#id="card.billingAddress.houseNumberOrName"')->clear
returns "SyntaxError: The expression is not a legal expression."
The source of the section I am searching is:
<tr>
<td colspan="2"><div class="fieldSubHeader">Billing Address</div>
</td>
</tr>
<tr>
<td><div id="idAddress1_card" style="display:inline">Address 1</div>
<div id="idHouseNumberOrName_card" style="display:none">House Number/Name
</div</td>
<td><div class="fieldDiv" id="idDivAddress1_card">
<input type="text" class="inputField" id="card.billingAddress.houseNumberOrName" name="card.billingAddress.houseNumberOrName" value="" size="15" maxlength="40" />
</div></td>
</tr>
Is it the . in the ID that is causing the issue?
(I have checked the computed CSS and it is not hidden!)
Many thanks
You are missing the closing ]. Replace:
'//input[#id="card.billingAddress.houseNumberOrName"'
with:
'//input[#id="card.billingAddress.houseNumberOrName"]'
HERE ^
Note that you might simplify it by switching directly to the find_element_by_id() method.

HTML form interleaves with <div> or <td> won't work

Does anybody experience the odd phenomenon: if an HTML form is divided into several parts by <div>'s or table <td>'s, then the <input type="submit" ... > button won't work -- nothing happens when clicking (no 'post' message is sent). If I take out all the <div>'s or table elements, the button works. Why is that? In my case, the form is inserted by Ajax. Could that be a cause of the problem? Thanks.
For example:
<div style="float:left">
<form action="/..." class="submitForm" method="post">
...
...
</div>
<div style="float:left">
...
...
</div>
<div>
...
<input type="submit" value="submit">
</form>
</div>
or,
<table>
<tr>
<td>
<form action="/..." class="submitForm" method="post">
...
...
</td>
<td>
...
<input ...>
...
</td
<td>
...
<input type="submit" value="submit">
</form>
</td>
There is nothing odd about it.
The HTML is invalid. The code doesn't mean anything.
Then the browser attempts to recover from your error. The results are not what you want.
If you don't write real HTML, you shouldn't expect browsers to be able to handle it.
Elements must describe a tree. If you open an element, you must close it before you close any of its ancestors.
http://validator.w3.org/ provides a useful tool for performing basic automated QA on your HTML.

Make radio buttons mutually exclusive when not contained in the same form element?

I'm making a calendar where each day has a radio button that allows you to select it. I want users to only be able to select one day. The issue I'm having is I've made the calendar as a table so each <td> has the following:
<td>
<form>
Mon 18
<input type="radio" name="day" value="mon18" />
</form>
</td>
So as all the radio buttons aren't contained in one form element, the normal behaviour that makes the selection mutually exclusive isn't occurring. If I create 1 form element around all my mark up then the table will be contained in it, would this be semantically correct?
Is there a semantically correct solution to my problem? I guess I could do it with javascript and server side validation.
It is perfectly semantically correct to have the form enclosing your table, that's actually the only sensible way to build your radiobutton-based calendar with mutually exclusive selection!
The following code does validate:
<form>
<table>
<tr>
<td><input type="radio" name="day" value="1"></td>
<td><input type="radio" name="day" value="2"></td>
...
</tr>
</table>
</form>

vb.net inline IF with OR... not evaluating

I'm working on a small problem where I'm trying to show/hide a panel based on two criteria
A specific data field must not be blank
The specific data filed must also not equal "Not Relocatable"
Unfortunately this doesn't seem to be working for me (note that setting either one or the other criteria works just fine.)
<asp:Panel runat="server" Visible='<%#If(Not String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "_236")) Or Not DataBinder.Eval(Container.DataItem, "_236") = "Not Relocatable", True, False)%>'>
<tr>
<td>
</td>
<td class="align-right lightgreen">
Buyer would consider relocating a business, if it is:
</td>
<td>
</td>
<td colspan="3">
<%#DataBinder.Eval(Container.DataItem, "_236")%>
</td>
<td>
</td>
</tr>
</asp:Panel>
Can anyone lend a hand to rectify this problem for me?
The syntax <%# %> is a data binding syntax, not an inline expression syntax. You cannot use procedural code inside of it like you can in the inline code <% %> tags.
Data binding tags must contain a single Eval or Bind function. If you need to do conditional branching based on those functions, you will need to do it using inline code around the binding tags.