Laravel 8 Form validation dependent to other form - laravel

I have a select box that has the option of yes and no, if the selected option is yes the percentage form will be required otherwise the percentage is not required.
this is the select form
<select name="downpaymentrequired">
<option value="1">Yes</option>
<option selected="selected" value="0">No</option>
</select>
<span>
#error('downpaymentrequired'){{ $message }}#enderror
</span>
this is the percentage form
<input min="0" maxlength="3" disabled id="downPaymentPercentageForm" type="text" name="downPaymentPercentage">
<span class="text-danger">
#error('downPaymentPercentage'){{ $message }}#enderror
</span>
and this is my validation
'downpaymentrequired' => 'required|bool',
'downPaymentPercentage' => 'exclude_if:downpaymentrequired,false|required|numeric|between:1,100'
Hello guys, please help me i know it might be a duplicate but the problem is the existing answer doesn't work for me I hope you help me.

'downpaymentrequired' => 'required|bool',
'downPaymentPercentage' => 'required_if:downpaymentrequired,1|numeric|between:1,100'
I just see the answer. I decided not to delete this post because someone might have the same problem as this. I hope this code will help you. you're welcome.

Related

Livewire wire:model on Select Option not working properly

I have a dropdown using wire:model
<div class="form-group">
<label>Menu</label>
<select wire:model="chapter_id" class="form-control #error('chapter_id') is-invalid #enderror" placeholder="Menu">
#foreach ($chapters as $chapter)
<option value="{{ $chapter->id }}">{{ $chapter->name }}</option>
#endforeach
</select>
#error('chapter_id')
<div class="invalid-feedback">{{ $message }}</div>
#enderror
</div>
It works if I do change the option (the $chapter_id assigned successfully). But, if I leave it default (not change the option), or it has only an option, the $chapter_id will not assigned
$chapter_id not assigned
Can you guys help me to solve this issue?
I see 2 ways to work around this. As tell the other before, one have an option for advice the user to select an option
<option value="">Please select</option>
and the other have a defaul value for this property
public $chapter_id = '1';
if this value is required on submit you have to refer it in the rules
protected $rules = [
'chapter_id' => 'required'
];
wire:model is only set or updated on change. Hence if you are already on the item (or there is only one), it will not be triggered. You could work around this using wire:init, but I don't see much benefit compared to setting it directly in your mount-method of the component.
My solution is to add an "Please select" entry as the first one. This enforces to trigger a change by selecting the item in question (even if only one option is available).

Laravel and selectize add option

I use laravel 5.6 and selectize, everything work great but I won't be able to add option or to set value with jQuery.
Here my code :
var select_user, $select_user;
$select_user = $('#select-user').selectize();
select_user = $select_user[0].selectize;
select_user.addOption({id: 10, name: 'name'});
And my html :
<select id="select-user" name="user" class="form-control" placeholder="Sélectionnez un utilisateur" required>
<option value="">Sans</option>
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name }}</option>
#endforeach
</select>
I have my users from my bdd but not my added one. No error message too.
Someone can tell me where I'm wrong ?
Thank for your help.

Laravel 5 dropdown box do not pass back old value upon validation fail

I have a quick question. I made a dropdown box for gender selection in my registration form that looks like this:
<div class="col-md-6 col-sm-6">
<select name="gender" value="{{ old('gender') }}" class="form-control">
<option value=null>------ Gender ------</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
{!! MessagerService::setInlineError($errors->first('gender')) !!}
</div>
So what I do is upon a validation fail, the selected gender is not passed back to the form. Any ideas what I have done wrong? Thank you.
You don't have to write plain HTML. You can take advantage of Laravel Form Builder. In Laravel 5 it is not bundled by default, like it was in 4.2, so first you need to require it. Go to your project root and in terminal write:
composer require "illuminate/html":"5.0.*"
Then go to config/app.php and under providers add
'Illuminate\Html\HtmlServiceProvider',
You can register also those two facades under aliases:
'Form'=> 'Illuminate\Html\FormFacade',
'HTML'=> 'Illuminate\Html\HtmlFacade',
After that you are ready to use Form facade to create your select with one simple line of code which will handle old input values:
{!! Form::select('gender', ['' => '--Gender--', 'male' => 'Male', 'female' => 'Female']) !!}
It is as simple as that. You can check the documentation to find out more useful methods of the Form facade.

Find which option is selected

I have this piece of code:
<select id="customizableonly" class=" select" name="product[customizableonly]">
<option value="1">Yes</option>
<option selected="selected" value="0">No</option>
</select>
And I need an xpath string that will find this <select id="customizableonly"...> tag and see which of its 2 options has the attribute selected="selected". Therefore, return the value of the "selected" option (Yes or No). If someone can give a quick solution - I'd appreciate that a lot!
I've got this far //select[#id='customizable'] for acquiring the main tag.
If I get it right, all you have to do is
//select[#id='customizableonly']/option[#selected='selected']/text()

jQuery Prev() Question

Sorry guys, I should have posted the script directly without cleaning it. Please check the updated script, it should now clear things up. Thanks!
Consider the following JavaScript :
var selected = 0;
var options = 0;
$('.newListSelected').each(function() {
selected = $(this).children('.selectedTxt');
selected = selected.text();
/* Everything before this line works completely fine */
options = $(this).prev();
options.find('option[value=' +selected+ ']').attr('selected', 'selected');
}).remove();
And HTML :
<select name="type[]" style="display: none;">
<optgroup>
<option value="none">Select</option>
</optgroup>
<optgroup label="First Group">
<option value="1">One</option>
<option value="2">Two</option>
</optgroup>
<optgroup label="Second Group">
<option value="10">Ten</option>
<option value="20">Twenty</option>
</optgroup>
</select>
<div class="newListSelected">
<div class="selectedTxt">20</div>
<ul class="newList">
<!-- Other stuff here -->
</ul>
</div>
What I'm trying to do actually is adding the selected attribute to the corresponding select option that has the same value as the text in .selectedTxt. In the code above, it should add selected="selected" to <option value="20">Twenty</option>.
However its not performing as expected, I also tried adding alert(option); below prev(); but it didn't output anything useful.
Thanks.
The Javascript works fine, as can be seen here: http://jsfiddle.net/4HsXp/
If you need to be able to select multiple items in a select element, you need to set the multiple and size attribute, like this:
<select multiple="multiple" size="2">
<option>1</option>
<option>2</option>
</select>
See: http://reference.sitepoint.com/html/select
Edit:
Attaching console.log statements to the new code still does not any problem. Running it on jsfiddle gave me the correct output:
jQuery(select)
Original Value: none
New Value: 20
I'm not sure what you are trying to do, but why couldn't you just select all option elements with proper selectors? If you want to select only some options, then you have to add extra selector attrbiutes.
$('select option').each(function() {
$(this).attr('selected', 'selected');
});

Resources