Thymeleaf th:each attribute failed on form element - thymeleaf

I find Thymeleaf th:each is not working properly on <form> element when ${items} is empty.
<form th:each="item,iterStat : ${items}" >
<span th:text="${iterStat.size}"> Hello </span>
</form>
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'size' cannot be found on null
But, it is ok on <div> element
<div th:each="item,iterStat : ${items}" >
<span th:text="${iterStat.size}"> Hello </span>
</div>
The Thymeleaf is included in Spring Boot v1.5.4.RELEASE
Why?

Related

how to compare the answer of authority.getAuthority() with a specific role 'ROLE_ADMIN' in thymeleaf?

I have tried the code below, but getting false even for ROLE_ADMIN
<th:block th:each="authority : ${#authentication.getAuthorities()}">
<span th:with="role=${authority.getAuthority()}">
<span th:text="${role}"></span>
<p th:with="res=${#strings.toString(role) == 'ROLE_ADMIN'}">
<div th:if="${res}">successtrial</div>
<div th:unless="${res}">failed trial</div>
<span th:text=${res}></span>
</span>
</p>
</th:block>
Getting ${role} - printed correct, but finally getting failed trial
You can use the hasRole method like this:
<div sec:authorize="hasRole('ADMIN')">
<div>success trial</div>
</div>
<div sec:authorize="!hasRole('ADMIN')">
<div>failed trial</div>
</div>
Be sure to have the org.thymeleaf.extras:thymeleaf-extras-springsecurity5 dependency in your application.

Do thymeleaf fragments need to exist inside a well-formed HTML file?

I'm creating a couple of thymeleaf fragments to use in other pages, and I was told they need to be inside a well-formed html page (with HTML, HEAD, BODY tags etc). Is this the case?
The fragments are only going to be used with th:include/th:replace in other places.
A fragment just needs to be a well formed html. You can start with a div
For e.g
<div th:fragment="formField (field, value, size)">
<div>
<label th:for="${#strings.toLowerCase(field)}"> <span
th:text="${field}">Field</span>
</label>
</div>
<div>
<input type="text" th:id="${#strings.toLowerCase(field)}"
th:name="${#strings.toLowerCase(field)}" th:value="${value}"
th:size="${size}">
</div>
Which you then include somewhere else
<body>
<header th:insert="fragments/general.html :: header"> </header>
<div th:replace="fragments/forms.html
:: formField(field='Name', value='John Doe',size='40')">
</div>
<div th:replace="fragments/general.html :: footer"></div>
I took these examples from here: https://www.baeldung.com/spring-thymeleaf-fragments

md-autocomplete "required" validation not applied to parent form

Following the provided documentation in angular material, I have setup a working autocomplete directive.
The required validation is being shown in the nested error message "div", but there is no effect on the parent form.
The parent form seems to be valid even for empty value in autocomplete.
<form ng-submit="ngSubmit()" layout="column" class="md-padding"
name="itemsForm" flex>
<md-input-container>
<label>Code *</label>
<input type="text" name="code" required ng-model="ngModel.code"/>
</md-input-container>
<md-autocomplete flex
required
md-input-name="author"
md-search-text="searchText"
md-search-text-change="ngModel.author = (searchText)"
md-selected-item-change="ngModel.author = item"
md-items="item in filterAutocomplete(searchText)"
md-item-text="item"
md-floating-label="Author *">
<md-item-template>
<span md-highlight-text="ctrl.searchText">{{item}}</span>
</md-item-template>
<div ng-messages="autocompleteForm.autocomplete.$error">
<div ng-message="required">This field is required</div>
</div>
</md-autocomplete>
<md-button class="md-primary" type="submit"
ng-disabled="itemsForm.$invalid"
ng-class="{'md-raised': itemsForm}">
Save
</md-button>
</form>
How can I achieve the similar behaviour to normal required input fields in side a form, when using md-autocomplete?
There have been some commits in the past few days that address this issue. It is fixed in master I believe.
https://github.com/angular/material/issues/7350
https://github.com/angular/material/issues/5896
You can try this version:
https://cdn.gitcdn.link/cdn/angular/bower-material/v1.0.6-master-3310aa9/angular-material.css
https://cdn.gitcdn.link/cdn/angular/bower-material/v1.0.6-master-3310aa9/angular-material.js

When I dynamically add HTML using ng-bind-html how can I initiate a compile for this HTML

My index.html contains the following element
...
<div class="col-md-6" ng-bind-html='module.docs.html'></div>
...
The module.docs.html property returns HTML like
<div ng-controller="accordion-demo-ctrl">
<label class="checkbox">
<input type="checkbox" ng-model="oneAtATime">
Open only one at a time
</label>
<accordion close-others="oneAtATime">
<accordion-group heading="Static Header, initially expanded" is-open="true">
This content is straight in the template.
</accordion-group>
<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
{{group.content}}
</accordion-group>
<accordion-group heading="Dynamic Body Content">
<p>The body of the accordion group grows to fit the contents</p>
<button class="btn btn-default btn-sm" ng-click="addItem()">Add Item</button>
<div ng-repeat="item in items">{{item}}</div>
</accordion-group>
<accordion-group is-open="isopen">
<accordion-heading>
I can have markup, too! <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isopen, 'glyphicon-chevron-right': !isopen}"></i>
</accordion-heading>
This is just some content to illustrate fancy headings.
</accordion-group>
</accordion>
</div>
The HTML is inserted but Angular.dart seems not to do anything with it (accordion-demo-ctrl is not instantiated)
Using the ng-include directive worked.
I changed my code so that I provided the URI of the HTML fragment instead of the source itself (fetched from the server anyway).
see also my answer to How to add a component programatically in Angular.Dart? for an alternative solution

Why am I getting this Javascript HIERARCHY_REQUEST_ERR with jQuery Mobile and Razor?

Dazed and confused here... The field in question is a boolean, and I want the UI to be a checkbox (or a yes/no or on/off jQuery slider). Each time I try to add in this checkbox input, I end up getting a
Microsoft JScript runtime error: DOM Exception: HIERARCHY_REQUEST_ERR (3)
error.
Here's the HTML+Razor
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>End Game:</legend>
#Html.TextBoxFor(model => model.HandicapSession.WinByTwo, new { #type="checkbox" })
<label for="WinByTwo">Win By Two?</label>
</fieldset>
</div>
Here's the generated HTML:
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>End Game:</legend>
<input data-val="true" data-val-required="The WinByTwo field is required." id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" type="checkbox" value="False" />
<label for="WinByTwo">Win By Two?</label>
</fieldset>
</div>
Apparently this error occurs when there are conflicting or overlapping id's as jQuery Mobile creates its checkbox widget
$.widget( "mobile.checkboxradio", $.mobile.widget, { ...etc...
But how do I use the HTML and/or razor to make a simple checkbox work in jQuery Mobile?
If the problem with jQuery Mobile really is duplicate names for the HTML tags, then you'll have to render your own input type=checkbox tag in HTML, as the ASP.NET MVC Html.CheckBoxFor helper method will render an input type=hidden tag with a duplicate name value. See this post for a discussion.
The hidden form tag is there because if you submit an unchecked checkbox to the server, the form value for that field isn't included. So a hidden input tag is included with value=false so that if the checkbox is unchecked, the value false is still submitted. The model binding process in MVC will filter out the duplicate form values for you, but if you're having a client-side problem with the duplicate name attributes, you'll have to render your own checkbox and label in HTML, then handle the fact that no value will be submitted for the HandicapSession.WinByTwo property when the box is unchecked. If no other property for HandicapSession is submitted, then that whole object will be null.
So, you can manually create the checkbox input and still load the checked and value attributes from your model, as desired, but you can run into model binding problems where the value for WinByTwo will still be false even when the box is checked.
Note also that the for attribute on your label doesn't match the ID of the checkbox in your sample. You need the full HandicapSession_WinByTwo.
The following manually creates the input tags:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
<div data-role="page">
<div data-role="content">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>End Game:</legend>
<input type="checkbox" id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" #(Model.HandicapSession.WinByTwo ? "checked=checked" : "") value="#(Model.HandicapSession.WinByTwo.ToString().ToLower())" />
<label for="HandicapSession_WinByTwo">Win By Two?</label>
</fieldset>
</div>
</div>
</div>
The HTML output is as follows for a checked checkbox on load:
<div data-role="page">
<div data-role="content">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>End Game:</legend>
<input type="checkbox" id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" checked=checked value="true" />
<label for="HandicapSession_WinByTwo">Win By Two?</label>
</fieldset>
</div>
</div>
</div>
The best would be just to use the MVC helper methods, so I'm not sure if you'd tried the following. The default Html.CheckBoxFor and Html.LabelFor helper methods work with jQuery Mobile 1.0a4.1 or 1.1.0 just fine. The following works for me:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
<div data-role="page">
<div data-role="content">
#using (Html.BeginForm())
{
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>End Game:</legend>
#Html.CheckBoxFor(m => m.HandicapSession.WinByTwo)
#Html.LabelFor(m => m.HandicapSession.WinByTwo, "Win By Two?")
<input type="submit" id="SubmitForm" value="submit" />
</fieldset>
</div>
}
</div>
</div>
This produces the HTML output:
<div data-role="page">
<div data-role="content">
<form action="/Mobile/Mobile" method="post">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>End Game:</legend>
<input checked="checked" data-val="true" data-val-required="The WinByTwo field is required." id="HandicapSession_WinByTwo" name="HandicapSession.WinByTwo" type="checkbox" value="true" />
<input name="HandicapSession.WinByTwo" type="hidden" value="false" />
<label for="HandicapSession_WinByTwo">Win By Two?</label>
<input type="submit" id="SubmitForm" value="submit" />
</fieldset>
</div>
</form>
</div>
</div>
Fix might be pretty simple.
Choice 1: Go scorched earth and turn off Ajax based navigation. This will ensure that unique IDs stay unique. This will ensure that you never encounter this issue again. Only downside is that you lose the pretty little transitions from page to page (someone call the whambulance). You can do this by setting up a global configuration script...
script src="jquery.js"
script src="custom-scripting.js"
script src="jquery-mobile.js"
inside that you'll override the ajax settings to disable them...
$(document).bind("mobileinit", function(){
$.mobile.ajaxEnabled = false;
});
Choice 2: Call pages that are going to require this uniqueness with a link that has an attribute of rel='external' or target='_black' which will accomplish the same thing without disabling all ajax based navigation. If you are reaching the page as a results of a form post, you can use data-ajax='false' on the form tag to accomplish a clean load.

Resources