Validate select form in laravel - php

I have this html on my contact form:
<div class="form-group">
<label class="col-md-2 control-label" for="type">Type of website?</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type">
<option>Business</option>
<option>Company</option>
<option>Personal</option>
<option>Online Shopping</option>
<option>Other</option>
</select>
</div>
</div>
</div>
How can I validate and pass data of this field in email?
Currently I have this:
<!-- Text input-->
<div class="form-group">
<label class="col-md-2 control-label">Type</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<input name="type" placeholder="eg. Business, Company, Personal, Online Shopping, etc." class="form-control" type="text">
</div>
</div>
</div>
And I validate and pass it like this:
$data = array(
'type' => $request->type,
);
but that's input field what I want is my first sample (select box), so I'm not sure how to do it!
thanks.

First Give THe Name To Select drop down fields, then after Simplest Way Is use in Validation Rules with the name of select input fields. that you give required
Example:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
Validation rules
// Validation rules somewhere...
$rules = [
...
'Business_Type' => 'required',
...
];

This will work perfectly.
$rules = [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
];

You can do it like this :
<div class="form-group">
<label class="col-md-2 control-label" for="type">Type of website?</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="typeSelection">
<option>--Select type--</option>
<option>Business</option>
<option>Company</option>
<option>Personal</option>
<option>Online Shopping</option>
<option>Other</option>
</select>
</div>
</div>
</div>
For the rules :
$rules = [
'typeSelection' => 'required|not_in:0'
];
not_in:0 to avoid submission of the first choice witch is --Select type-- :)
And to pass infos to the validation just use $request->all() because it will use the field name in this case typeSelection no need for ctreating a new array !

In Laravel 8 i use this, i found out that if you change the value to an other value trough inspect element 'yes i know this sounds funky' still can be inserted into the validator and pass. Here's the solution
Select:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option></select>
Validator:
$this->validate($request, [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
]);
Just enter the values that the validator must pass, all other values will not pass

You need to validate your select with required and for that you need to setup select like:
Select into blade:
<select class="form-control" id="type" name="selType">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
Validation rules somewhere:
$rules = [
'selType' => 'required'
];

if you want to check if the selected value is exist in your table's column you can check it like this example
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
for above selection
'Business_Type' => 'required|my_business ,type'
This will make sure the selected value exists for example in the my_business table on the type column.
if you select the first option it will return the error
The selected type is invalid.

You should put Value in each option
The first one should be
value=""
and the rest put the values of them like this
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>

Related

How to use an if statement on the blade template. error: Undefined variable $category

I have created a function to retrieve a specific column in the database using eloquent, on my productsController
public function create()
{
$category = Product::select('category')->get();
return view('products.create');
}
And Now I want to use an if statement on the create.blade.php to check whether the value in the database is equal to 'liquor',
<div class="row mb-4">
<label for="category" class="col-md-4 col-form-label text-md-end">Category</label>
<div class="col-md-6">
<select name="category" id="category" class="form-select col-md-6">
<option value="liquor">liquor</option>
<option value= "food">food</option>
<option value="DIY">DIY</option>
<option value= "thrift shops">thrift shops</option>
<option value= "home decor">home decor</option>
<option value= "phones and tablets">phones and tablets</option>
<option value= "computing">computing</option>
<option value= "electronics">electronics</option>
<option value= "beauty products">beauty products</option>
<option value= "others">others</option>
</select>
</div>
</div>
#if($category == 'liquor')
<div class="row mb-4">
<label for="subcategory" class="col-md-4 col-form-label text-md-end">Sub Category</label>
<div class="col-md-6">
<select name="subcategory" id="subcategory" class="form-select col-md-6">
<option value="null">Select subcategory</option>
<option value="wine">Wine</option>
<option value="whisky">Whisky</option>
<option value="brandy">Brandy</option>
<option value="scotch">Scotch</option>
<option value="spirit">Spirit</option>
<option value="gin">Gin</option>
<option value="vodka">Vodka</option>
<option value="beer">Beer</option>
<option value="rum">Rum</option>
<option value="mixers">Mixers</option>
<option value="bourbon">Bourbon</option>
<option value="cognac">Cognac</option>
<option value="other">Other</option>
</select>
</div>
</div>
<div class="row mb-4">
<label for="volume" class="col-md-4 col-form-label text-md-end">Volume</label>
<div class="col-md-6">
<select name="volume" id="volume" class="form-select col-md-6">
<option value="null">Choose volume</option>
<option value="5ltr">5ltr</option>
<option value="1ltr">1ltr</option>
<option value="750ml">750ml</option>
<option value="500ml">500ml</option>
<option value="250ml">250ml</option>
<option value="other">Other</option>
</select>
</div>
</div>
#endif
However, I get the following error: Undefined variable $category
What could be the issue?
I think you forgot to send the $category to the view:
return view('products.create', compact("category"));
or like this:
return view('products.create')
->with('category', $category)
->with('title', 'New Title');
or like this:
return view('products.create', ['category' => $category]);
You can add as many vars/collections/or whatever as you wish both ways.
use this
#if(isset($category) && $category == 'liquor')
//your code
#endif
Also check this link for validation
you need to send $category from controller to your blade like this
public function create()
{
$category = Product::select('category')->get();
return view('products.create')->with('category',$category);
}
you can also do this with another way
public function create()
{
$category = Product::select('category')->get();
return view('products.create',compact('category'));
}

Check old data is null or not or check by isset() from input laravel Blade

How can I check old data is available or not after validation from controller?
i used this but it shows error
#if(\Illuminate\Http\Request::old('status')!=null)
<option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
#endif
also used this one
#if(isset(old('status')))
<option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
#endif
full form is
<form action="{{route('add-expense-store')}}" method="post" class="new-added-form">
{{ #csrf_field() }}
<div class="row">
<div class="col-xl-8 col-lg-8 col-12 form-group">
<label>Expense Type</label>
<select class="select2" name="type">
<option value="">Please Select</option>
{{--<option value="Teacher's Salary">Teacher's Salary</option>
<option value="Staff's Salary">Staff's Salary</option>--}}
<option value="Transport">Transport</option>
<option value="Mobile Bill">Mobile Bill</option>
<option value="Utility Bill">Utility Bill</option>
<option value="Stationary">Stationary</option>
<option value="Print & Press">Print & Press</option>
<option value="Photocopy & Compose">Photocopy & Compose</option>
<option value="Entertainment & Hospitality">Entertainment & Hospitality</option>
<option value="Donation">Donation</option>
<option value="Program">Program</option>
<option value="Personal or Owner">Personal or Owner</option>
</select>
</div>
<div class="col-xl-4 col-lg-4 col-12 form-group">
<label>Status</label>
<select class="select2" name="status">
#if(isset(old('status')))
<option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
#endif
<option value="">Please Select</option>
<option value="Paid">Paid</option>
<option value="Due">Due</option>
</select>
</div>
<div class="col-xl-6 col-lg-6 col-12 form-group">
<label>Holder Name</label>
<input type="text" placeholder="" class="form-control" name="holder_name"
value="{{ old('holder_name') }}">
</div>
<div class="col-xl-6 col-lg-6 col-12 form-group">
<label>Phone</label>
<input type="number" placeholder="" class="form-control" name="phone" {{ old('phone') }}>
</div>
<div class="col-12 form-group mg-t-8">
<button type="submit" class="btn-fill-lg btn-gradient-yellow btn-hover-bluedark float-right">Save
</button>
</div>
</div>
</form>
controller
$this->validate($request, [
'type' => 'required',
'status' => 'required'
]);
both are getting error. How can I check old value us available or not in blade?
Now I include my form data. here I wants to hidden if old value is not found. please help with this one. check isset old data in blade.
You can check it in this way. Remember to return it in this way from controller
$validator = $this->validate($request, [
'type' => 'required',
'status' => 'required'
]);
if ( $validator->fails() ) {
return back()->withErrors( $validator )->withInput();
}
In your view do this
<select class="select2" name="status">
<option value="">Please Select</option>
<option value="Paid" {{ old('status') == 'Paid' ? 'selected' : '' }}>Paid</option>
<option value="Due" {{ old('status') == 'Due' ? 'selected' : '' }}>Due</option>
</select>

Laravel - if condition for select

My if condition for selected Input is not showing anyhing. Here is my create.blade.php:
...
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST']) !!}
<div class="column">
<label for="title">Title</label></br>
<input name="title" type="text" placeholder="" class="form-control">
</div>
</br>
<div class="column">
<label for="brand">Brand</label></br>
<select class="form-control" id="soflow" name="brand">
<option value="" disabled selected>Select your option</option>
<option value="Volkswagen">Volkswagen</option>
<option value="BMW">BMW</option>
<option value="Audi">Audi</option>
</select>
</div>
</br>
#if(Input::has('brand') == 'Volkswagen')
<div class="column">
<label for="model">Model</label></br>
<select class="form-control" id="soflow" name="model">
<option value="" disabled selected>Select your option</option>
<option value="Golf1">Golf 1</option>
<option value="Golf2">Golf 2</option>
<option value="Golf3">Golf 3</option>
</select>
</div>
#endif
{!! Form::close() !!}
...
But after this my select for model is not showing at all and I just need to show specific models if select input(brand) is for Volkswagen. Any solutions? Please help. Thank you!
i think the solution for your problem can be chained js https://appelsiini.net/projects/chained/ . I am not sure if this works for you. Let me know if it worked.
Input::has('brand')
shall return T/F if there is an input field with name 'brand' or not. The line,
Input::has('brand') == 'Volkswagen'
in your logic, evaluates to
true == 'Volkswagen'
Try
#if(Input::get('brand')=='Volkswagen')

How can I get the selected index of a select?

I am in Laravel 5.7, I will wish to get the index of a select. I have tried this but without success so far.
<div class="form-group">
<label for="company-content">Ville</label>
<select name="fk_localite" id="" class="form-control">
<option value="">Selectionner votre ville</option>
#foreach($localites as $localite)
<option value="{{$localite->id}}">{{$localite->ville}}
</option>
#endforeach
</select>
Here is the solution !
<div class="form-group">
<label for="company-content">Ville</label>
<select name="fk_localite" id="" class="form-control">
#foreach($localites as $localite)
<option value="{{$localite->id}}" #if($eleves->fk_localite == $localite->id) selected #endif>{{ $localite->ville }}
</option>
#endforeach
</select>
</div>

jQuery dependent fields (reset value), add to MySQL same name attribute fields

I have setup a 4phases multiple dependent dropdown fields. I want to select region then country, owned or franchise and finally store name.
My issues:
1) Show/hide works OK but doesn’t reset values when I choose “-”. This mean that I can choose one store, then change my opinion, choose another option from upper field category and choose a second store. The POST will have both values. I want to reset the first value when I choose another option
2) Store names have all name attribute store_name. I can’t managed to make it record correctly on mysql db while I used implode().
jQuery:
$( document ).ready(function() {
$("#region").bind("change",function(){
var selectedValue=$(this).val().toLowerCase();
switch(selectedValue){
case "asia":
$("#country_asia").show();
$("#country_america").hide()
$("#country_europe").hide()
break;
case "america":
$("#country_asia").hide();
$("#country_america").show();
$("#country_europe").hide();
break;
case "europe":
$("#country_asia").hide();
$("#country_america").hide();
$("#country_europe").show();
break;
case "-":
$("#country_asia").hide()
$("#country_america").hide()
$("#country_europe").hide()
break;
}
});
$("#country_europe_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "greece")
{
$("#owned_gr").show();
}
else if(selectedValue == "-")
{
$("#owned_gr").hide();
}
})
$("#owned_gr_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "own")
{
$("#store_name").show();
$("#store_name_greece_fran").hide();
}
else if(selectedValue == "fran")
{
$("#store_name_greece_fran").show();
$("#store_name").hide();
}
else if(selectedValue == "-")
{
$("#store_name").hide();
$("#store_name_greece_fran").hide();
}
})
});
</script>
Form:
<div class="form-group region required" data-cid="region">
<label class="control-label" for="region">Region</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" onChange="displayForm(this)" id="region" name="region">
<option value="-">- Select -</option>
<option value="america">America</option>
<option value="asia">Asia</option>
<option value="europe">Europe</option>
</select>
</div>
</div>
<div id="country_europe" class="form-group country_europe required" style="display: none" data-cid="country_europe">
<label class="control-label" for="country">Country</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" id="country_europe_dd" name="country">
<option value="-">- Select -</option>
<option value="cyprus">Cyprus</option>
<option value="france">France</option>
<option value="germany">Germany</option>
<option value="greece">Greece</option>
<option value="iceland">Iceland</option>
<option value="italy">Italy</option>
<option value="netherlands">Netherlands</option>
<option value="norway">Norway</option>
<option value="serbia">Serbia</option>
<option value="switzerland">Switzerland</option>
</select>
</div>
</div>
<div id="owned_gr" class="form-group owned_gr required" style="display: none" data-cid="owned_gr">
<label class="control-label" for="owned_gr">Owned or Franchise ?</label>
<div class="input-group"><span class="input-group-addon left"><i class="glyphicon glyphicon-pushpin"></i></span>
<select class="select form-control" id="owned_gr_dd" name="owned">
<option value="-">- Select -</option>
<option value="own">Owned</option>
<option value="fran">Franchise</option>
</select>
</div>
</div>
<div id="store_name" class="form-group store_name required" style="display: none" data-cid="store_name">
<label class="control-label" for="store_name">Store Name - Greece (Owned)</label>
<div class="input-group"><span class="input-group-addon left"><i class="fa fa-buysellads"></i> </span>
<select class="form-control" name="store_name[]" id="store_name_gr_owned"
data-rule-required="true" >
<option value="-">- Select -</option>
<option value="option1">Option1</option>
<option value="option2">Option2</option>
</select>
</div>
</div>
<div id="store_name_greece_fran" class="form-group store_name_greece_fran required" style="display: none" data-cid="store_name_greece_fran">
<label class="control-label" for="store_name_greece_fran">Store Name - Greece (Franchise)</label>
<div class="input-group"><span class="input-group-addon left"><i class="fa fa-buysellads"></i> </span>
<select class="form-control" name="store_name[]" id="store_name_greece_fran"
data-rule-required="true" >
<option value="">- Select -</option>
<option value="athens">Athens</option>
<option value="crete">Crete</option>
</select>
</div>
</div>
PHP:
$store_name = implode($_POST['store_name']);
EDIT: From your comment below, sounds like solving #1 will also solve #2.
For #1, Whenever you hide an element, set its value to - as well. E.g.
case "asia":
$("#country_asia").show();
$("#country_america").hide();
$("#country_america").val("-");
$("#country_europe").hide();
$("#country_europe").val("-");
break;
Or, using vanilla JS:
document.getElementById("country_europe_dd").value = '-';
For #2, I can't see anything wrong with your code. Can you elaborate? Maybe include sample output of what you're seeing for $store_name?
I found the solution.
I have to go one step further on jQuery and use val(). I reset other category values when i select specific values on the field i choose
$("#store_name_greece_fran_dd").change(function()
{
var selectedValue=$(this).val().toLowerCase();
if(selectedValue == "athens" || selectedValue == "athens" || selectedValue == "crete" || selectedValue == "crete")
{
$("#store_name_owned_dd").val('');
}
})

Categories