Old value in multiple select option in laravel 9 blade - laravel-blade

It's about the issue "Old value in multiple select option in laravel blade".
After validation, the selected values are no longer displayed.
#dd($values)
^ array:8 [▼
0 => "Auswahl 1"
1 => "Auswahl 2"
2 => "Auswahl 3"
3 => "Auswahl 4"
4 => "Auswahl 5"
5 => "Auswahl 6"
6 => "Auswahl 7"
7 => "Auswahl 8"
]
Code:
<select class='form-select #error($detail_id) is-invalid #enderror' id="{{ $detail_id }}"
name="{{ $detail_id }}[]" {{ $size }} {{ $multiple }} {{ $disabled }} required="required">
#foreach ($values as $key => $value)
<option value="{{ $value }}" {{ (old($detail_id) == $value ? "selected" : "") }}>{{ $value }}</option>
#endforeach
</select>

do this , it's work on my projects
{{ (collect(old($detail_id))->contains($value)) ? 'selected':'' }}

Related

Laravel 9 Prevent trims on Old value

I have an old value containing the person's name, but when I put it in the input value it automatically trims (only the first word), I've tried adding exceptions in the AppServiceProvider and Trim middleware but it's not working, does anyone have a solution?
Top : The Old Value, Bottom : The Input Value
EDIT 1 (ADD CODE)
// File : input component (form-field.blade.php)
#props([
'label' => 'undefined',
'placeholder' => '',
'type' => 'text',
'disabled' => false,
'required' => true,
'value' => old($name) ?? '',
'min' => '',
'max' => '',
'name' => '',
'items' => [],
])
<label class="text-sm font-semibold" for={{ $name }}>{{ $label }} {!! $required ? '<span class="text-primary">*</span>' : '' !!}</label>
<div class="bg-gray-300 rounded-md #error($name) border-primary border-1 #enderror">
<input
class="flex-grow bg-transparent border-gray-300 border-2 rounded-md w-full py-2 px-2 focus:border-black focus:outline-none"
name={{ $name }}
type={{ $type }}
id={{ $name }}
min={{ $min }}
max={{ $max }}
value={{ $value ?? old($name) }}
{{ $disabled ? "disabled" : "" }}
>
</div>
// File : View Form Page
<x-form-field
:name="'name'"
:label="'Nama'"
:value="session()->get('data')->employee_name ?? null"
/>
If you view the source, you'll see <input ... value=Prof Smiling ...>.
value={{ $value ?? old($name) }} without quotation marks is your problem, because Smiling gets treated as a different HTML attribute, as if you wrote <input value="Prof" Smiling="">.
value="{{ $value ?? old($name) }}" will avoid breaking when there are spaces.
(You should fix this everywhere, not just for this particular occurrence.)

Display all rows in Symfony 3

I have a form which has EntityType:class below in Symfony 3 or SF3, as you can see I am calling the function from method findAllActiveBuyingCurrencies from BranchCurrencyRepository.
->add('currency', EntityType::class, [
'class' => BranchCurrency::class,
'choice_value' => 'rate',
'choice_label' => function($currency){
return $currency->getName() . ' (' . $currency->getCode() .') - '.$currency->getRate().'';
},
'placeholder' => 'Choose currency',
'query_builder' => function (BranchCurrencyRepository $er) {
return $er->findAllActiveBuyingCurrencies();
},
])
The functions does it's thing and displays all currencies inside the form (Option)
But, I wanted to display all currencies and my problem is, only 25 currencies are displayed.
Please see my code displaying all currencies
// branchCurrenciesRepository
public function findAllActiveBuyingCurrencies() {
return $this->createQueryBuilder('currency')
->where('currency.type = :type')
->setParameter('type', BranchCurrencyConstant::Buying);
}
View
<div class="col-3">
<label for="" class="required">
<strong> Currency <span class="highlight-red"></span></strong>
</label>
{{ form_widget(form.currency) }}
{% if not form.currency.vars.valid %}
<p class="mt10" style="color: #DC2B1B;">{{ form.currency.vars.errors[0].message }}</p>
{% endif %}
</div>

Set class on label attribute of Symfony3 checkbox

I have a symfony3 project where I'm adding a custom class to some form fields via twig. This is all working fine except for the checkbox label. My custom class does not show in the label for the checkbox as expected.
EDIT: adding my form builder:
class ReferralFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('modality', EntityType::class, [
'class' => Modality::class,
'placeholder' => 'Choose modality',
'choice_label' => 'name',
])
->add('referralFromDoctor', EntityType::class, [
'class' => Doctor::class,
'placeholder' => 'Choose a doctor:',
'choice_label' => 'name',
])
->add('isFollowup', CheckboxType::class, [
'label' => 'Follow up visit?',
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Referral::class
]);
}
}
Here's my twig template:
{{ form_start(form) }}
<div id="js-order-wrapper">
{{ form_label(form.modality, 'Type', { 'label_attr': {'class': 'js-hidden-row'} }) }}
{{ form_widget(form.modality, { 'attr': {'class': 'js-hidden-row'} }) }}
{{ form_label(form.referralFromDoctor, 'Ordering clinician', { 'label_attr': {'class': 'js-hidden-row'} }) }}
{{ form_widget(form.referralFromDoctor, { 'attr': {'class': 'js-hidden-row'} }) }}
{{ form_label(form.isFollowup, 'Follow up?', { 'label_attr': {'class': 'js-hidden-row'} }) }}
{{ form_widget(form.isFollowup, { 'attr': {'class': 'js-hidden-row'} }) }}
</div>
{{ form_end(form) }}
and here is the rendered html:
<div id="js-order-wrapper">
<label class="js-hidden-row control-label required" for="referral_form_modality">Type</label>
<select id="referral_form_modality" name="referral_form[modality]" required="required" class="js-hidden-row form-control"><option value="">Choose modality</option></select>
<label class="js-hidden-row control-label required" for="referral_form_referralFromDoctor">Ordering clinician</label>
<select id="referral_form_referralFromDoctor" name="referral_form[referralFromDoctor]" required="required" class="js-hidden-row form-control"><option value="">Choose a doc</option></select
<div class="checkbox">
<label class="required">
<input type="checkbox" id="referral_form_isFollowup" name="referral_form[isFollowup]" required="required" class="js-hidden-row" value="1" /> Follow up visit?
</label>
</div>
</div>
As you can see, the class 'js-hidden-row' is correctly applied to the labels for the fields, but for the checkbox the class is not applied to the label. Seems like a bug to me.
Short of writing a custom block of code to render the checkbox, is there a better solution?

represent a variable in twig using for loop

In my Service I return an array which looks like this:
$array = [
'bible_anagram_word' => $result->getBibleAnagramWord(),
'word_1' => $result->getWord1(),
'word_2' => $result->getWord2(),
'word_3' => $result->getWord3(),
...
'word_22' => $result->getWord22(),
'word_23' => $result->getWord23(),
'word_24' => $result->getWord24(),
'word_25' => $result->getWord25(),
'Bible_verse_reference' => $result->getBibleVerseReference(),
'Bible_verse_text' => $result->getBibleVerseText(),
'Bible_translation' => $result->getBibleTranslation(),
];
How do I represent document.word_## in this for loop?
{% for i in 1..25 %}
{{ document.word_ ~ i|slice(0,1) }}
{% endfor %}
In PHP this code is:
substr ( $this->document['word_'.$i] , 0 , 1 )
My unsuccessful attempts include:
{{ document.word_ ~ i|slice(0,1) }}
{{ (document.word_ ~ i)|slice(0,1) }}
{{ 'document.word_ ~ i'|slice(0,1) }}
This should do the trick
{{ attribute(document, 'word_' ~ i) }}
More on attribute function

Symfony2: background color of date widget

For a template that includes a date field widget, non-date input fields will have their background colors changed per CSS. The same does not occur on the date widget. How should the background color of the date widget be effected? (Adding an attr array in the form class has no effect.)
A screenshot of the (small but real) difference:
Code samples:
Template:
<td>{{ form_widget(form.fname, {'attr': {'class':'smallform'}}) }}
<td>{{ form_widget(form.sname, {'attr': {'class':'smallform'}}) }}
<td>{{ form_widget(form.dateAdded, {'attr': {'class':'smallform'} }) }}
Form class:
->add('fname', null, array('required' => false))
->add('sname', null, array('required' => false))
->add('dateAdded', 'date', array(
'widget' => 'choice',
'format' => 'MM/dd/yyyy',
'pattern' => '{{ year }}-{{ month }}-{{ day }}',
'years' => range(Date('Y'), Date('Y') - 5),
'required' => false,
'data' => date_create(),
))
CSS:
.smallform {
background-color: #f5f5f5;
font-size: 9pt;
color: #000066;
border: 1px solid #CCCC99;
}
A less-than-subtle solution: modify the date widget in my application's customized copy of fields.html.twig. The more precise solution would be to define a specific widget for this, or figure out how to retain the class attribute all the way into the date widget:
{% block date_widget %}
{% spaceless %}
{% if widget == 'single_text' %}
{{ block('form_widget_simple') }}
{% else %}
<div {{ block('widget_container_attributes') }}>
{{ date_pattern|replace({
'{{ year }}': form_widget(form.year, {'attr': {'class':'smallform'}}),
'{{ month }}': form_widget(form.month, {'attr': {'class':'smallform'}}),
'{{ day }}': form_widget(form.day, {'attr': {'class':'smallform'}}),
})|raw }}
</div>
{% endif %}
{% endspaceless %}
{% endblock date_widget %}
Try:
->add('fname', null, array(
'required' => false,
'attr' => array('class' => 'smallform')
))
->add('sname', null, array(
'required' => false,
'attr' => array('class' => 'smallform')
))
->add('dateAdded', 'date', array(
'attr' => array('class' => 'smallform'), // ADDED
'widget' => 'choice',
'format' => 'MM/dd/yyyy',
'pattern' => '{{ year }}-{{ month }}-{{ day }}',
'years' => range(Date('Y'), Date('Y') - 5),
'required' => false,
'data' => date_create(),
))
also, view-source to make sure the classes are actually set in the html tags, it could be a problem with your CSS.
There is a problem with these lines:
<td>{{ form_widget(form.fname, {'attr': {'class':'smallform'}}) }}</td>
I dont think you can set a class on the entire widget, only for individual rows.
you can try this if you need to set the class via twig.
<form action="" method="">
{{ form_errors(form) }}
{{ form_row(form.fname, { 'attr': {'class': 'smallform' } }) }}
{{ form_row(form.sname, { 'attr': { 'class': 'smallform' } }) }}
<div class="smallform">
{{ form_row(form.dateAdded) }}
</div>
{{ form_rest(form) }}
<input type="submit" name="submit" value="Submit" />
</form>

Resources