Select field validation - validation

<div class="form-group">
<label for="category-id">Category</label>
<select id="category-id" class="form-control {{$errors->get('category_id') ? 'is-invalid' : ''}}" name="category_id">
<option value="177">Select a category</option>
#foreach ($categories as $category)
<option value="{{$category['id']}}">{{$category['title']}}</option>
#endforeach
</select>
#if ($errors->has('category_id'))
<div class="invalid-feedback"><strong>{{$errors->first('category_id')}}</strong></div>
#endif
</div>
The category_id is defined as the required field in my Request php file ('category_id' => 'required')
But when I submit the form without any input to any field, all fields generate error messages by highlighting the field, except this SELECT field.

It passes the validation because your are sending a value in that field.
Change this
<option value="177">Select a category</option>
to
<option selected disabled>Select a category</option>
then it should not pass the validation.

Related

How to select an option from Laravel into a select?

I'm bringing all the data from the database and displaying in inputs for an edit page of my CRUD, but I can't figure out how to select an option based on the data.
With inputs I know that I can just do this:
<div class="col-md-6">
<label for="razao" class="form-label">Nome Empresarial</label>
<input type="text" name="razao" value="{{$cliente->razao ?? old('razao')}}" class="form-control" id="razao">
</div>
But i can't find anything about select, and how to set an option "selected" based on the data from the database.
The select:
<div class="col-md-3">
<label for="tipoContribuinteSelectCnpj" class="form-label">Tipo de Contribuição</label>
<select class="form-control select2-single" name="tipoContribuinteCnpj" id="tipoContribuinteSelectCnpj">
<option hidden></option>
<option value="I">ICMS</option>
<option value="X">Isento</option>
<option value="N">Não Contribuinte</option>
</select>
</div>
The data saved in the database is a single char that corresponds to the value of the option of the select.
Although it looks stupid, this is how i did in case you came here from google:
<div class="col-md-3">
<label for="tipoContribuinteSelectCnpj" class="form-label">Tipo de Contribuição</label>
<select class="form-control select2-single" name="tipoContribuinteCnpj" id="tipoContribuinteSelectCnpj">
<option hidden></option>
<option value="I" #if($cliente->tipoContribuinteCnpj == "I") selected #endif>ICMS</option>
<option value="X" #if($cliente->tipoContribuinteCnpj == "X") selected #endif>Isento</option>
<option value="N" #if($cliente->tipoContribuinteCnpj == "N") selected #endif>Não Contribuinte</option>
</select>
</div>
Give this a shot:
<option value="I" {{ $cliente->tipoContribuinteCnpj == 'I' ? 'selected' : '' }}>ICMS</option>

How to select only lessons which belong to a particular course in a create form using Laravel and Blade

I have Course and Lesson models and I want to first select a Course and then only the lessons which belong to that course to be available for selection on a dropdown list.
I have created the pivot tabla course_lesson and the relashionship works ok.
<div class="form-group">
<select name="course_id" class="browser-default custom-select">
<option selected>Choose course</option>
#foreach($courses as $course)
<option value="{{$course->id}}">{{$course->title}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="lesson_id" class="browser-default custom-select">
<option selected>Choose lesson</option>
#foreach($courses as $course)
#foreach($course->lesson as $lesson)
<option value="{{$lesson->id}}">{{$lesson->title}}</option>
#endforeach
#endforeach
</select>
</div>

How to set a null value inside a drop-down selection without "parent_id" required

I would like to make a drop-down selection with the first selection comes with " NO PARENT ID REFERRED " and set as null value and pass into database in laravel
Here is my code
<div class="form-group">
<label for="bit_app_policy_category_parent">Parent Category</label>
<select id="bit_app_policy_category_parent" name="parent_id" class="form-control">
<option selected disabled>Please select one option</option>
#foreach($parents as $parent)
#if ($parent-> status != 'Freeze')
<option value="{{ $parent->id}}">{{$parent->description}} </option>
#endif
#endforeach
</select>
</div>
<div class="form-group">
<label for="bit_app_policy_category_status">Status<span class="required">*</span></label>
<select id="bit_app_policy_category_status" name="status" class="form-control">
<option value="Active">Active</option>
<option value="Freeze">Freeze</option>
</select>
</div>
Dropdown selection`
Expected result =
Please select one option
-----No parent ID referred-----(able to choose and pass value into db)
description 11 and so on .
PLEASE NOTE THAT IM UNABLE TO USE FORM METHOD AS MY LRAVEL UNABLE TO UPDATE DUDE TO SPECIFIC PURPOSES
try to add another <option> before foreach.
example codes:
<option value="null" selected>Please select one option</option>
#foreach($parents as $parent)
#if ($parent-> status != 'Freeze')
<option value="{{ $parent->id}}">{{$parent->description}} </option>
#endif
#endforeach
</select>
```

show data of nested foreach loop in two different Select Tag in laravel

I have two Model(Category,Brand) are relate with one to many relation(hasMany) in laravel, Like category hasMany brands. now i want to display brand data in one select tag and category model data in another select tag.
Here is my Controller code
$category=Category::with('brands')->get();
return view('product.add')->with('category',$category);
Here is my view blade.
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($brands->brands as $brand)
<option value="{{$brand->id}}">{{$brand->name}}</option>
#endforeach
</select>
</div>
its not working
You can do something simple like this:
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $cat)
<option value="{{$cat>id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($category as $cat)
#foreach($cat->brands as $brand)
<option value="{{$brand->id}}" category="{{$cat->id}}">{{$brand->name}}</option>
#endforeach
#endforeach
</select>
</div>
and you need to show only the brands for the selected option using javascript(jquery)
$('[name="category_id"]').on('change',function() {
if($(this).val() != '') {
$('[name="brand_id"] option').hide();
$('[name="brand_id"] option[category="'+$(this).val()+'"]').show();
} else {
$('[name="brand_id"] option').show();
}
});

Can't make multiple select in registration form

I use the basic registration form but I want to add a multiple select, a User can have multiple Skills and select them when he register.
My problem is that when I select multiples values Laravel only take the last one, so I search on the web for a solution and they say to add [] after my class name, so this is what I do :
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" multiple="" class="form-control">
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
But now, when I submit the form the page reload and stay in the register form, and the user is not register... I don't know what to do, anyone of you have a solution ?
Here is how I check the competences in my registerController
$competences = $data['competences'];
foreach ($competences as $comp){
$user->competences()->save(Competence::find($comp));
}
Use multiple="multiple" OR just multiple in blade.php:
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" class="form-control" multiple>
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
then in your controller, verify that you're actually getting the data in the form of an array by just printing $data
return $data;

Resources