Django Forms replace integer value with String Value - django-forms

I have these choice types on my model:
AVAILABLE = [
(7, _("7 AM")),
(8, _("8 AM")),
(9, _("9 AM")),
(10, _("10 AM")),
(11, _("11 AM")),
(12, _("Noon")),
(13, _("1 PM")),
(14, _("2 PM")),
(15, _("3 PM")),
(16, _("4 PM")),
(17, _("5 PM")),
(18, _("6 PM")),
(19, _("7 PM")),
]
I rendered my form using the following:
<div class="fieldWrapper">
<select class="chosen-select" id="id_mon_start" name="mon_start">
<option selected="selected">{{availability_hours.mon_start }} </option>
{% for choice in form.fields.mon_start.choices %}
<option value="{{ choice.0 }}">{{choice.1}}</option>
{% endfor %}
</select>
<div class="error-block">{{ form.mon_start.errors }}</div>
</div>
My form works and updates as required with all the correct values, I would like to show the string value of my AVAILABLE choices instead of the integer on my option selected="selected" ,I'm having no look with tips I've googled to do this. Any help would be great!

This is what you are looking for:
<option selected="selected">{{ availability_hours.get_mon_start_display }} </option>

Related

If old value then select old value of Select Box otherwise select value coming from Controller

I have a select box in the edit form, and everything is working fine, but the problem is when there is an old value, it always selects the value from the database. I think the issue is loop indexing. When I choose an option down from the original value, it selects the old value, but it retains the initial value when selecting an option above the selected value.
<select class="form-control
searchableSelect {{ $errors -> has('Cash_expense_Account') ? 'is-invalid' : '' }}"
name="Cash_expense_Account">
<option value="">Search...</option>
#for($i = 0; $i < count($Cash_Accounts); $i++)
<option value="{{ $Cash_Accounts[$i]['id'] }}"
{{ ( $Cash_Accounts[$i]['id'] == $expense->coa_sub_account_id) ? 'selected' : '' }}
{{ old('Cash_expense_Account') == $Cash_Accounts[$i]['id'] ? 'selected' : '' }}>
{{ $Cash_Accounts[$i]['acc_sub_name'] }}
</option>
#endfor
</select>
Limit the comparison to only use old value if it exists, like:
<option value="{{ $Cash_Accounts[$i]['id'] }}" {{ ( (old('Cash_expense_Account')??$Cash_Accounts[$i]['id']) == $expense->coa_sub_account_id) ? 'selected' : '' }}>
{{ $Cash_Accounts[$i]['acc_sub_name'] }}
</option>
the ?? operator checks if old value exists otherwise use one from the loop.

Object of class Illuminate\Support\Collection could not be converted to int Laravel 8

I am having troubles with retrieving an integer value from Query builder of Laravel. It gives me this error as the title of the question:
Object of class Illuminate\Support\Collection could not be converted to int
i tried to use also the pluck() method but with the same error.
Here is the code portion of code:
Scegli Corso: <select name="tipo" id="selezionaCorsoIscrizione">
{{ $corsi_scii = DB::table('corsoscii')->select('idCorso','nome')->get() }}
{{ $post_corso = DB::table('corsoscii')->select('membriMax')->pluck('membriMax') }}
<option value="" selected="selected"> Seleziona Corso
</option>
#foreach($corsi_scii as $corso_scii)
<option value="">
#if($post_corso == 0)
{{ "Il corso ha raggiunto la capienza massima" }}
#else
{{ $corso_scii->idCorso." - ".$corso_scii->nome }}
#endif
</option>
#endforeach
pluck() returns a collection of the field you selected. You can't compare it to 0. Use value() instead, it returns the first available value or null.
Replace this line
{{ $post_corso = DB::table('corsoscii')->select('membriMax')->pluck('membriMax') }}
with
{{ $post_corso = DB::table('corsoscii')->select('membriMax')->value('membriMax') }}

How to set option value conditionally disabled when DB_values are greater than current time?

I'm able to fetch DB entries with their time, all the entries are getting into the select box properly entries which are having time lesser than current time are falling in if statement..., as well as entries with time greater than current time, are falling in Else Sattement...,
I want to show entries from both the if-else statement, but values falling in else statement should be disabled by nature, so that they won't get selected, here is my code please help
<select name="gameSelect" v-model="fields.game_id" >
<option v-for="gameinfo in gamesinfo" selected v-bind:key="gameinfo.id" v-bind:value="gameinfo.id">
<div v-if="gameinfo.time < timestamp">
{{ gameinfo.game_name }} / {{ gameinfo.time }}
</div>
<div v-else>
Disabled {{ gameinfo.game_name }} {{ gameinfo.time }}
</div>
</option>
</select>
Use :disabled prop to add your date condition to disable option. In below example replace YOUR CONDITION TO DISABLE with date condition.
<select name="gameSelect" v-model="fields.game_id" >
<option v-for="gameinfo in gamesinfo" selected :disabled="YOUR CONDITION TO DISABLE" v-bind:key="gameinfo.id" v-bind:value="gameinfo.id">
<div v-if="gameinfo.time < timestamp">
{{ gameinfo.game_name }} / {{ gameinfo.time }}
</div>
<div v-else>
{{ gameinfo.game_name }} {{ gameinfo.time }}
</div>
</option>
</select>

Laravel 5 - Pre-populate a HTML select using database value and old

I am trying to use 'old' in a Laravel 5 app to pre-populate a select form on an edit route like this...
<select id="category" name="category" required>
<option value="">Select</option>
<option value="animal" #if (old('category') == "animal") {{ 'selected' }} #endif>Animal</option>
<option value="vegetable" #if (old('category') == "vegetable") {{ 'selected' }} #endif>Vegetable</option>
<option value="mineral" #if (old('category') == "mineral") {{ 'selected' }} #endif>Mineral</option>
</select>
This works well and keeps the selected option if a validation fails, but I am trying to make it work so that it pre-populates when the page first loads.
How do I determine if this is the first load of the edit page, or if it has reloaded after a validation failure? Or is there a better way to do this?
Lets imagine you have sent the category value as $category.
So in every <option> tag,
<option value="animal"
{{ old('category') == 'animal' ? ' selected' :
$category == 'anumal' ? ' selected' : '' }}>
Animal
</option>
This is what is going on there:
if there is an old value and its matching, select this,
else if the original value is matching select this,
else do nothing.
Use the second parameter of the old function with a default/initial value:
<option value="animal" #if (old('category', 'animal') == "animal") {{ 'selected' }} #endif>Animal</option>
The second parameter will be returned as the function result if an old value cannot be found in the flashed input.

Check if current date lies between start date and end date (Laravel 5.3)

So, I have this view in my controller to show a button if the current date lies between the start date and the end date. Here is what I have done so far :
#if (Carbon\Carbon::now()->format('Y-m-d') < Carbon\Carbon::parse($edition->start)->format('Y-m-d') && Carbon\Carbon::now()->format('Y-m-d') > Carbon\Carbon::parse($edition->end)->format('Y-m-d'))
<p></p>
#else
<div class="tombol-nav">
Upload Jurnal Anda Sekarang!<br>
<p style="color: red;">Penting! Batas waktu terakhir pengunggahan Jurnal pada Edisi ini : {{ Carbon\Carbon::parse($edition->limit)->format('j F, Y') }}</p>
</div>
#endif
I don't know what's wrong with it, the button will still appear even though the end date has passed. Thank you for your help.
So I finally did it with someone help from another forum, I'm gonna write it down here in case some people need it.
#if (Carbon\Carbon::now()->between(Carbon\Carbon::parse($edition->start), Carbon\Carbon::parse($edition->limit)))
<div class="tombol-nav">
Upload Jurnal Anda Sekarang!<br>
<p style="color: red;">Penting! Batas waktu terakhir pengunggahan Jurnal pada Edisi ini : {{ Carbon\Carbon::parse($edition->limit)->format('j F, Y') }}</p>
</div>
#else
<p></p>
#endif
Please use this code as reference to check date between two dates in laravel using carbon.
$first = Carbon::create(2012, 9, 5, 1);
$second = Carbon::create(2012, 9, 5, 5);
var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false)
Also read more about Carbon here : http://carbon.nesbot.com/docs/
If condition does not work in case of objects.
Carbon\Carbon::now()->format('Y-m-d') returns an object.You have to convert it into string and then pass it in the if condition.
Convert carbon object into string : -
$carbonDateTimeObject = Carbon\Carbon::now()->toDateTimeString();
An easier way to deal with date comparison in Blade is to use Carbon's diffInSeconds()
For eg:
#if(\Carbon\Carbon::now()->diffInSeconds($edition->start, false) > 0 )
//$edition->start is in the future
#else
//$edition->start is in the past
#endif
See here for more information: http://carbon.nesbot.com/docs/#api-difference
So, your code could be rewritten as
#if (Carbon::now()->diffInSeconds($edition->start) < 0 && Carbon::now()->diffInSeconds($edition->end, false) > 0)
<p></p>
#else
<div class="tombol-nav">
Upload Jurnal Anda Sekarang!<br>
<p style="color: red;">Penting! Batas waktu terakhir pengunggahan Jurnal pada Edisi ini : {{ Carbon\Carbon::parse($edition->limit)->format('j F, Y') }}</p>
</div>
#endif
Depending on the precision that you need, you could also use diffInMinutes() or similar methods.

Resources