How keep value from input select after request? - url

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?

Related

With Rails 5, how to think about naming a form field to later be used be a Rails Controller#create method

In Rails 5, I have the following model:
Talents.rb
id | Title
1 | Jumping
2 | Skipping
3 | Running
My API, returns Talents#Index like so:
[
{"id":1,"title":"Jumping"},
{"id":2,"title":"Skipping Rope"},
{"id":3,"title":"Running"},
{"id":4,"title":"Something Else"}
]
I then want to use the respond to build a form:
<div>
<label>Jumping</label>
<select name="Jumping">
<option></option>
<option value="1">XXX</option>
<option value="2">YYY</option>
</select>
</div>
....
My question is how should I be thinking about NAMING the talents in the form so that I can properly post the form to Rails to then record the user's response?
Do I need another field from the API with some type of Key? Or for select name, should I be using the ID?
I will do something like this:
<form name="my-form" method="post" action="/create">
<div>
<label>Jumping</label>
<input type="hidden" name="ratings[]talent_id" value="1"></input>
<select name="ratings[]rating">
<option></option>
<option value="1">Rating 1</option>
<option value="2">Rating 2</option>
</select>
</div>
<div>
<label>Skipping</label>
<input type="hidden" name="ratings[]talent_id" value="2"></input>
<select name="ratings[]rating">
<option></option>
<option value="1">Rating 1</option>
<option value="2">Rating 2</option>
</select>
</div>
<div>
<label>Running</label>
<input type="hidden" name="ratings[]talent_id" value="3"></input>
<select name="ratings[]rating">
<option></option>
<option value="1">Rating 1</option>
<option value="2">Rating 2</option>
</select>
</div>
<input type="submit" name="submit" value="Send">
</form>
Using ratings[] will create an array, which enables you tu use the same name for each select.
Adding an input with type hidden, will allow to specify which talent is being rated (the value of each input is the talent_id to be rated in that select).
For example, if a user selects the following:
Rating 2 for Jumper
Rating 1 for Jumping
Rating 1 for Skipping
Then the following parameters will be sent to your controller:
Parameters: {
"ratings"=>[
{"talent_id"=>"1", "rating"=>"2"},
{"talent_id"=>"2", "rating"=>"1"},
{"talent_id"=>"3", "rating"=>"1"}
]
}
Now, in your controller you could just iterate over params[:ratings] to create each object, maybe something like this:
params[:ratings].each do |rating|
rating_attributes = {
user_id: user, # use the appropriate `user_id`
rated_by: rater, # use the appropriate `rated_by`
talent_id: rating[:talent_id],
rating: rating[:rating]
}
Rating.create!(rating_attributes)
end
Of course you should optimize this method by, for example, handling errors instead of raising an exception (as create! does); but this will get you going.

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>

How to select label text in GEB?

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"

Submitting form elements with the same name

I have a form which allows the user to create extra "rows" using JQuery (using .clone) so that they can decide how many of the same information they need to submit. My issue is that I cannot work out how to access these form items within my controller.
the form that is being submitted may look like this
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
Basically, the block between input and the select could be repeated an infinite number of times. When I submit to the controller I am then using FormCollection form to access the form elements. from there I am unsure how I can access the items that have been submitted. I thought of using a for loop and then accessing them via something like form["Amount"][i] but obviously that is not going to work.
Am I going about this the right way and if so, does anyone have any suggestions about how this might work?
Thanks in advance.
Old question, but still...
You can get the posted values as an array by calling Request.Form.GetValues, or Request.QueryString.GetValues. For example:
string[] amounts = Request.Form.GetValues("Amount");
And the amounts array will contain the correct values, so you can post values containing comas, dots, whatever, and don't worry about splitting/parsing it.
Of course if you are running MVC, use the modelbinder to do it. But you can use this if you are using webforms, a generic handler, etc...
Check out Model Binding To A List. Your Action method should be:
public ActionResult MyAction(string[] Amount, int[] Item){
// ...
}
However this will make you need to "link" the items. Alternatively create a "Item" class:
public class Item {
public string Amount { get; set; }
public int Item { get; set; }
}
And
public ActionResult MyAction(IList<Item> items){
// ...
}
And your markup should be:
<input type="hidden" name="items.Index" value="0" />
<input type="text" name="items[0].Amount" id="items[0].Amount">
<select name="items[0].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="hidden" name="items.Index" value="1" />
<input type="text" name="items[1].Amount" id="items[1].Amount">
<select name="items[1].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
Etc...
I believe if you have multiple fields named Amount the values will be comma delimited.
To access each one just try:
string[] amounts = Request.Form["Amount"].Split(new char[] { ',' });
Keep in mind though, the inputs are not cleaned on submission so if someone enters a comma into the text box it's going to cause issues.
Hence I'd recommend numbering them.
I ended up realising that (blush) the mechanism which JQuery uses to find the string within the cloned row (to replace) is basically regex. Thus I just needed to escape the square brackets and period. Once I did this I was able use JQuery to create form as Phil Haack's blog suggested.
Onto my next issue...!
I would number them Amount1, Amount2, Amount3 etc.
You can change the id and name attribute of the input to something like this "Amount[1]","Amount[2]","Amount[3]" (yes, the id and name attribute can contain the special chars "[" or "]"). Then in the controller, write a http request parameter parser to get back the Amounts as collections.

Resources