How to select label text in GEB? - grails

I have a select drop down which will display an error if a value is not selected.
<div class="field contain" >
<select name="myselect" id="myselect" class="error" >
<option value="0">--select a value--</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<label for="myselect" generated="true" class="error" style>Please select at least one option</label>
</div>
I want to validate if this error label has been displayed using GEB. Tried to select it with 'error' selector does not work. Any suggestions would be helpful.
Thanks,
Abhijith

This doesn't work?
assert $("select.error option").text() == "--select a value--"

Try
assert $("label", for:"myselect").text() == "Please select at least one option"

Try CSS selector.
$('label[for=myselect]').text()
This one selects label tag with value of the "for" attribute = myselect. Then "text()" method returns it's body: "Please select at least one option"

Related

How keep value from input select after request?

I'm doing a search form in my site. And what i want is to keep on my inputs, the value selected by the user. It's ok for my checkbox, but i can't do it for select.
I show you my form first :
<select name="garde" id="garde">
<option value="">Choisir un type de garde</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
<div class="flex items-center">
<input id="chats" name="chats" value="1" id="chats" type="checkbox"
#if(request()->chats) {{ 'checked' }} #endif>
<label for="chats" class="ml-3 text-sm text-gray-600">Chat</label>
</div>
I tried to use some :
<select blablabla>
<option value="{{$garde->id}}" #if(request()->garde) {{'selected'}} #endif/>{{$garde->garde}}</option>
</select>
So it's certainly good for the value, but it doesn't display the good option name. Always sending me the last value from the database (which is 3 in this case).
My controller
$garde = request()->input("garde");
$chats = request()->input("chats");
$g = Garde::where('id', 'like', "%$garde%")->pluck('id');
Annonce::when($g, function ($s) use ($g) {
return $s->where('garde_id', $g);})
->when($chats, function ($s) use ($chats) {
return $s->where('chats', $chats);})
Edit : Here the "%$garde%" was after trying another option, using "$garde" doesn't changed anything.
As i said, it's ok for checkbox, but for select i can not keep this value without doing a "bidouille" as we say in french ^^
<select name="garde" id="garde">
<option value="{{ request()->garde ?? '' }}">
#if(request()->garde == 1) Chez le Pet-Sitter
#elseif(request()->garde == 2) Visite à domicile
#elseif(request()->garde == 3) Chez le Pet-Sitter/En visite
#endif
</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
But this is not a good practice i know it, and it's display 2 times the selected value ofc..
Have you got some ideas?
Maybe not using the pluck('id) in my controller is the key? But what is other way to do it?

how to set selected value in drop down menu depending on conditions

I have code with drop down menu. The object for this menu has few fields. Two of them are playerPosition and isFirstSquadPlayer.
If isFirstSquadPlayer is TRUE I need to display in menu player.playerPosition.
Trying on few ways but failed.
My controller:
public String players(#PathVariable long clubId, Model model) {
Club club = this.clubRepository.findByClubId(clubId);
model.addAttribute("players", this.playerRepository.findAllByPlayerClub(club));
return "players";
}
My HTML:
<select name="playerposition"
id="createnewplayerposition" th:value="${player.playerPosition}" required>
<option value="0">Select position for player</option>
<!--HERE NEED PROPER REQUEST -->
<option th:selected="${player.playerPosition}" th:text="${player.playerPosition}"></option>
<option value="GK">Goalkeeper</option>
<option value="RWB">Right Wingback</option>
<option value="RCB">Right Centreback</option>
(...)
</select>
as per kindly advice:
ok, tried and found such request with thymeleaf conditional:
<option th:if="${player.playerPosition!=null}" th:selected="${player.playerPosition}" th:text="${player.playerPosition}"></option>
it's not excactly how I expected but gives some solution –

HTML select box, show updated value

please help me this issues
I got this select box
<select name="company">
<g:each in="${grailsApplication.config.Companies['No']}" var="no" status="index">
<option value="${no}">${no}: ${grailsApplication.config.Companies['Name'][index]}</option>
</g:each>
</select>
the select box has value like this
option1 01: abc
option2 02:def
Then, I used form update, when I choose option2, it saves value to db, but on select box, default value is 01:abc, how can i change it to 02:def after update.
Assuming that you are sending saved value in savedValue:
<select name="company">
<g:each in="${grailsApplication.config.company}" var="company" status="index">
<g:if test="${grailsApplication.config.company['no'][index] == savedValue}">
<option value="${grailsApplication.config.company['no'][index]}" selected>${grailsApplication.config.company['name'][index]}</option>
</g:if>
<g:else>
<option value="${grailsApplication.config.company['no'][index]}">${grailsApplication.config.company['name'][index]}</option>
</g:else>
</g:each>
</select>

How to select option in HTML select using TAL in ZPT?

I got the following drop-down list - SELECT element - construction in my ZPT template:
<select id="record_selector">
<option tal:repeat="record view/records" tal:attributes="value record/id">
<span tal:replace="record/name"></span>
</option>
</select>
How make it possible to have selected OPTION which value is equal to one from the corresponding view property (i.e. for example, OPTION tag value == view/currentRecordId then make it selected).
Using the sdupton's clue, I got the following solution:
<select id="record_selector">
<tal:block tal:repeat="record view/records">
<option tal:condition="python: record['id'] != view.recordId"
tal:attributes="value record/id"
tal:content="record/name">
</option>
<option tal:condition="python: record['id'] == view.recordId"
tal:attributes="value record/id"
tal:content="record/name"
selected>
</option>
</tal:block>
</select>
TAL conditionals are awesome :)
I found another solution here: https://old.zope.org/Members/peterbe/DTML2ZPT/index.html#example14
This still works with Zope 5.3 on Python 3.
<select id="record_selector">
<option
tal:repeat="record view/records"
tal:attributes="
value record/id;
selected python: record['id'] == view.currentRecordId">
<span tal:replace="record/name"></span>
</option>
</select>

Step definition for drop down list

I want to write step in Cucumber Scenario for selecting option "Grandfather's middle Name"
from drop down list of security_questions.
<td align="left">
<select id="security_question" class="sign-up-security Signup_red_text" name="user_detail[secret_question]">
<option value="Please Select"> Please Select</option>
<option value="grandfather middle name"> Grandfather's middle Name</option>
<option value="first date last name"> First Date's Last Name</option>
<option value="favorite place"> Favorite Place</option>
</select>
<br>
<div id="questionError" class="sign-up-ht"></div>
</td>
Please help me to write a step for this.
I have step definition for the same as
When /^I select "(.*)" from "(.*)"$/ do |value, field|
select(value, :from => field)
end
and i am writing this as
I select "grandfather middle name" from "user_detail[secret_question]"
But it is giving me error while running features as
Could not find field labeled "user_detail[secret_question]" (Webrat::NotFoundError)
Suggest any way.
Webrat is expecting a <label> tag. You should make that too. You should also be able to reference the <select> element by using the id-attribute.
As a side note: Using HTML names inside your cucumber features is not done. See Dan North's post on that.

Resources