Disable selected chosen options when it exists in database - laravel-5

How can I disable the multiple select options when the item already exists in the database? Here's my form so far. Each item was stored in the database per row. I am using Laravel 5.2. Thanks
<form class="profile-detail-specialist skills-form" role="form" id="edit-skills">
<select name="service_skillsreq[]" class="chosen-select" multiple style="width:350px;" tabindex="4">
#foreach($skills as $skill)
<option value="$skill->skills_name">{{$skill->skills_name}}</option>
#endforeach
</select>
<input type="submit" value="Save" class="skills-btn">
<button>Cancel</button>
{{csrf_field()}}
</form>

you can try to echo a disabled attribute to the option tag if the item exists in your database. Try:
<option value="$skill->skills_name" <?php if(*database check*){echo"disabled";} ?>
{{$skill->skills_name}}
</option>
Kindly replace the database check to an appropriate condition checking as I do not know the structure of your database.
Hope it helps =)

Related

Load a selectbox from a persisted list of objects and use that selectbox value to load the full object from the selectbox

I have a Hibernate table with my business object : campaign (Id, name, description, ...). Using Thymeleaf, I am preloading a select box with the campaign names. Once the user selects a name and I am trying to submit the campaign Id to the controller or since I have an arrays with all campaigns object, I would only pass the selected object from the selectbox.
What do you think would be the best way to do this ?
<div class="form-group blu-margin">
<form th:object="${campaign}" th:action="#{/campaign}" method="post">
Select the campaign you would like to execute :
<select
class="form-control" id="dropCampaign" name="dropCampaign">
<option value="0">select campaign</option>
<option th:each="INcampaign : ${campaigns}"
th:value="${INcampaign.id}"
th:text="${INcampaign.name}">
</option>
</select>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Reading various articles and trying different things, I think I got it. Sharing my solution in case it can help someone. The trick is to add id and name in the select tag. Then of course, in your controller you need to add #RequestParam Long id in your method signature.
But note this is not the usual way to do this. The more standard way is to call a URL such as /campaign/{id}/edit. But I wasn't sure about the security because not all users have access to all the campaigns so I didn't want to expose the URL to this edit possibility (even if I am sure you can secure this somehow). For me, that way seems easier.
<form name="f" th:action="#{/campaign}" th:object="${campaign}" method="post">
<select id="id" name="id">
<option value="0">select campaign</option>
<option th:each="campaign : ${campaigns}"
th:value="${campaign.id}"
th:text="${campaign.name}"
>
</option>
</select>
<button type="submit" class="btn btn-primary">Submit</button>

Spring how to use select with Thymeleaf [duplicate]

I want to create a drop down menu that allows a client to search users by a field specified in the drop down menu. For example, search by state, search by city, etc.
This is what I have so far:
<p>Search options:</p>
<form action="#" th:action="#{/get/{value}" method="get">
<select>
<option th:value="AllUsers">Search all users</option>
<option th:value="ByUsername">Search by user name</option>
<option th:value="ByFirstname">Search by first name</option>
<option th:value="ByLastname">Search by last name</option>
<option th:value="ByAddress">Search by address</option>
<option th:value="ByCity">Search by city</option>
<option th:value="ByState">Search by state</option>
<option th:value="ByZipCode">Search by zip code</option>
<option th:value="ByPhoneNumber">Search by phone number</option>
<option th:value="ByEmail">Search by email</option>
</select>
<input type="text" th:field="value" name="searchField"/>
<input type="submit" value="Search" name="searchButton"/>
</form>
I'm just not sure how to connect the action and the value tag of the currently selected item in the drop down list to specify the URI. If a user selects, search by state, and he enters in "Maryland", how do I specify the corresponding URI tag?
This would be my method in Spring that executes the action:
#RequestMapping(value = "/get/ByState", method = RequestMethod.GET)
public String getByState() {
// ...
}
#RequestMapping(value = "/get/ByCity", method = RequestMethod.GET)
public String getByCity() {
// ...
}
Because the accepted answer is not using Thymeleaf and this question is top in Google I'm adding my solution here.
In my situation statues is a list of Enum values. Model attribute is populated as usually you do in Spring:
mav.addObject("statuses", EnumSet.allOf(Status.class));
Group has a filed of type Status (the enum).
<div class="form-group row">
<select class="form-control" id="status" name="status">
<option th:each="stat : ${statuses}"
th:value="${stat}"
th:text="${stat}"
th:selected="${stat.equals(group.status)}"/>
</select>
</div>
This automatically populates the list and selects value that is selected in my Group instance:
You have just the required answer in this link:
http://fruzenshtein.com/spring-mvc-form-select-tag/

Laravel 5.4 passing form select option value to a Route::get

I am trying to do the following within my Laravel 5.4 project.
In a users.index blade page I am listing all users from a database in a table.
All the users belong to a district. Some districts have more users then the other.
At the top of this table view I created a form with a select field. The options hold the id's for the 10 districts:
<form class="form-horizontal" method="GET" action="/users/district">
<div class="form-group col-md-5">
<select name="district" class="form-control">
<option value="1">Amsterdam</option>
<option value="2">Arnhem</option>
<option value="3">Assen</option>
<option value="4">Groningen</option>
<option value="5">Leeuwarden</option>
<option value="6">Rotterdam</option>
<option value="7">Sittard</option>
<option value="8">Tilburg</option>
<option value="9">Utrecht</option>
<option value="10">Antillen</option>
</select>
</div>
<div class="form-group col-md-4 col-md-offset-1">
<button class="btn btn-md btn-primary">Haal gegevens op</button>
</div>
</form>
When I put the URL "http://localhost/users/district/5" in manually in the browser it shows me a table view with only users from the district with the ID of 5.
This is my UsersController:
public function perDistrict($district)
{
$users = User::where('district_id', $district)
->paginate(12);
$totalusers = User::count();
return view('users.index', compact('users', 'totalusers'));
}
I would like to use the form to get me the results but when I submit the form it show me this URL: "http://localhost/users/district?district=5" and it gives me no result of course.
How can I convert that to the URL "http://localhost/users/district/5" ?
This my route:
Route::get('users/district/{district}', 'UsersController#perDistrict');
Hope someone can help me.
https://www.codecourse.com/lessons/laravel-fundamentals/729 did the trick for me with some own customization.

How to re-populate form with old data if a validation error occurs?

The problem here is that the country field is not being re-populated whenever I submit the form. What I expect is for the user to submit the form and have the server validate it and redirect back with the errors and keep the data that the user input before submitting.
For some unknown reason, the country field is not getting the old input but the state field is getting the old input perfectly.
<select id="country" name ="country" class="form-control rounded input-lg text-center no-border" required autofocus <option> </option>
</select> </br>
<select name ="state" id ="state" class="form-control rounded input-lg text-center no-border" required autofocus
<option>{{ Request::old('state') }}</option>
</select> </br>
You can use just old() helper for that:
<input type="text" name="username" value="{{ old('username') }}">
Another way to do that is to use Laravel Collective form model binding:
{!! Form::model($user, ['route' => ['user.update', $user->id]]) !!}
In this case all form data will be re-populated automatically.

Load a specific option of SELECT tag while loading a template in Marionette

I'm trying to load the specific value of a drop down select tag when I'm loading that html inside the Marionette LayoutView.
The situation is
This is my html template which is passed to a Marionette LayoutView with a Model
<div class="span3">
<div class="control-group">
<label for="registeredIndicator">Registered</label>
<select class="input-medium" id="registeredIndicator">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
</div>
In that model a value registered indicator is retrieved as true or false.
My Goal is - When this template is rendered I want to display the value which is retrieved i.e. if its true, registeredIndicator should be Yes and vice-versa.
I accomplished this thing with help of jQuery inside the onRender function of LayoutView like
if(true === this.model.get('registeredIndicator')){
this.$('#registeredIndicator option:eq(1)').prop('selected', true);
}
but I got lot of drop downs in the HTML and it would not be a efficient way to check each every one with jquery.
I tried using if else inside the HTML with <%%> but this was not fruitful and is not correct also.
Is there any another approach available in which i can manipulate the data inside the template itself?
Thanks in advance!
Adding the conditional below should work:
<div class="span3">
<div class="control-group">
<label for="registeredIndicator">Registered</label>
<select class="input-medium" id="registeredIndicator">
<option value="">Select</option>
<option <%= registeredIndicator ? "selected" : "" %> value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
</div>
Specifically I added <%= registeredIndicator ? "selected" : "" %> to your template.

Resources