Laravel, html value '&' signs - php

I have database with types such as:
Entertainment & Food,
Health & Care
etc.
I insert it into a dropdown like this:
<div class="form-group{{ $errors->has('type') ? ' has-error' : '' }}">
<div class="type_message form-control alert-warning" style="display: none;"></div>
<label id="type2" for="type" class="col-md-4 control-label">Type</label>
<div class="col-md-6">
<select class="form-control" name="type" id="type">
<option selected value="{{ $entity->type }}">{{ $entity->type }}</option>
#foreach ($entities as $entity_select)
<option value={{ $entity_select->type}}>{{ $entity_select->type }}</option>
#endforeach
</select>
#if ($errors->has('type'))
<span class="help-block">
<strong>{{ $errors->first('type') }}</strong>
</span>
#endif
</div>
</div>
The problem is that when I check the value of the field it looks like this:
<option value="Entertainment & Nightlife" selected="">Entertainment & Nightlife</option>
Therefore in the database it only adds "Entertainment" how can I avoid that?

<option value="{!! $entity_select->type !!}">{!! $entity_select->type !!}</option>
https://laravel.com/docs/5.4/blade#displaying-data
As mentionned in the documentation :
Be very careful when echoing content that is supplied by users of your
application. Always use the escaped, double curly brace syntax to
prevent XSS attacks when displaying user supplied data.

Related

How to handle validation of multiple hidden fields

I have created a form where in many cases some fields are not displayed until user select a certain option that will make the particular field displayed, this is controlled by HTML onchange Event Attribute with functions in script. To this point everything works fine.
Problem:
In the Request class I am implementing some rules for validation , for all displayed fields and for particular none displayed fields if only their options are selected then they should be validated. In my case I have 8 of this none displayed fields are a_text, b_text, c_text, d_text, e_text, f_text,g_text, h_text. If this fields were 2 it would be easy to write an If statement to choose which should be validated and which should not be validated like below:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
class StoreSectionERequest extends FormRequest
public function rules(Request $request)
{
$a = $request->a;
$b = $request->b;
if($a==5 && $b==3){
return [
//for none displayed fields only validate the below
'a_text' =>'required',
'b_text' =>'required',
//..and other displayed fields will be valuated.
];
}elseif(!$a==5 && $b==3){
return [
//for none displayed fields only validate the below
'b_text' =>'required',
//..and other displayed fields will be valuated.
];
}elseif($a==5 && !$b==3){
return [
//for none displayed fields only validate the below
'a_text' =>'required',
//..and other displayed fields will be valuated.
];
}
// in case all none displayed field are not selected only check other fields below
return [
// validaying all none displyed field here]; }}
In my case I have 8 fields which are none displayed, of which I have to handle all cases of fields if it's selected and not, this will lead to 100's or a lot of if...elseif..statements. What is the wright approach to handle this kind of task.
Form
<form method="POST" action="{{ route('......') }}">#csrf
<div class="row">
<div class="col-md-5" id="lackingSkills">
<label for="">Which skills and competencies graduates are lacking? </label>
<select name="a" multiple="multiple" id="skills" class="form-control select2 #error('a')
is-invalid
#enderror" onchange="myFunction()">
<option value="1" #if (old('a')=="1" ) {{ 'selected' }} #endif>Information and communication skills</option>
<option value="2" #if (old('a')=="2" ) {{ 'selected' }} #endif>Interpersonal skill</option>
<option value="3" #if (old('a')=="3" ) {{ 'selected' }} #endif>System management skills</option>
<option value="4" #if (old('a')=="4" ) {{ 'selected' }} #endif>Innovation skills</option>
<option value="5" #if (old('a')=="5" ) {{ 'selected' }} #endif>Others. Specify</option>
</select>
<small class="text-primary">If necessary, choose more than one option.</small>
#error('a') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-6" id="skillReason" {!! $errors->has('a_text') || old('a') == 5 ? '':' style="display: none"' !!} >
<div class="form-floating mb-3">
<textarea rows="2" class="form-control #error('a_text')#enderror" name="e2_10" id="skillReason" placeholder="Type here.."></textarea>
</div>
</div>
<div class="col-md-6">
<select name="b" id="b" class="form-control" onchange="myFunction2()">
<option value="" selected disabled>Select</option>
<option value="1" #if (old('b')=="1" ) {{ 'selected' }} #endif>Most of them match the requirements</option>
<option value="2" #if (old('b')=="2" ) {{ 'selected' }} #endif>Only some match the requirements</option>
<option value="3" #if (old('b')=="3" ) {{ 'selected' }} #endif>Other, specify</option>
</select>
#error('b') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-6 mt-2" {!! $errors->has('e3_5') || old('b') == 3 ? '':' style="display: none"' !!} >
<div class="form-floating mb-3">
<textarea rows="2" class="form-control" id="b_text" name="b_text" placeholder="Type here.."></textarea>
<label for="floatingInput" id="specify">Specify here..</label>
</div>
</div>
<div class="col-md-6">
<select name="c" id="graduatesPreference" class="form-control" onchange="myFunction3()">
<option value="" selected disabled>Select</option>
<option value="1" #if (old('c')=="1" ) {{ 'selected' }} #endif>Yes</option>
<option value="2" #if (old('c')=="2" ) {{ 'selected' }} #endif>No</option>
</select>
#error('c') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-6" id="preferenceReason" {!! $errors->has('c_text') || old('c') == 1 ? '':' style="display: none"' !!}>
<label for="">Provide the reason, why you have a preference for a specific university?</label>
<textarea name="c_text" id="" rows="3" class="form-control" placeholder="Provide the reason here ..."></textarea>
</div>
<div class="col-md-6">
<select name="d" id="qualification" class="form-control #error('d')is-invalid #enderror" onchange="myFunction4()">
<option value="" selected disabled>Select</option>
<option value="1" #if (old('d')=="1" ) {{ 'selected' }} #endif>Bachelor’s degree</option>
<option value="2" #if (old('d')=="2" ) {{ 'selected' }} #endif>Post-graduate</option>
<option value="3" #if (old('d')=="3" ) {{ 'selected' }} #endif>Other, specify</option>
</select>
#error('d') <span class="invalid-feedback" role="alert">{{ $message }}</span> #enderror
</div>
<div class="col-md-12" id="d_text" {!! $errors->has('d_text') || old('d') == 3 ? '':' style="display: none"' !!}>
<label for="">Explain why?</label>
<textarea name="d_text" id="d_text" rows="3" class="form-control" placeholder="Provide the reason here ..."></textarea>
</div>
////.............others fields like e,f,g,h, all have the same functionality like the above ones.
<div class="action-button d-flex justify-content-center bg-white pt-2 pb-2">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
<script>
/// various function for onchange
myFunction()
myFunction2()
myFunction3()
myFunction4()
</script>
My question: what is the wright approach to handle the above scenario without writing 100's of if..elseif statements. Thanks in advance.
In form validation u have rules like required with or without, u can see more here
Little bit lower on the docs u can see Conditionally Adding Rules
So basically u will be doing same validation but without too much mess in your code.

How to make dropdown list ascending order in PHP Laravel

This code from product_listing.blade.php and here the dropdown filter. I need all the item name in ascending orde. Right now the list is in order how I can see it in a database table..
<div class="box-title text-center">
{{ translate('Filter by CarType')}}
</div>
<div class="box-content">
<div class="filter-checkbox">
<select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
<option value="">Select CarType</option>
#foreach (\App\CarType::all() as $cartype)
<option value="{{ $cartype->id }}" #isset($cartype_id) #if ($cartype_id == $cartype->id) selected #endif #endisset>{{ $cartype->name }}</option>
#endforeach
</select>
</div>
</div>
You can use collection's sortBy() method:
<div class="box-title text-center">
{{ translate('Filter by CarType')}}
</div>
<div class="box-content">
<div class="filter-checkbox">
<select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
<option value="">Select CarType</option>
#foreach (\App\CarType::all()->sortBy('name') as $cartype)
<option value="{{ $cartype->id }}" #isset($cartype_id) #if ($cartype_id == $cartype->id) selected #endif #endisset>{{ $cartype->name }}</option>
#endforeach
</select>
</div>
</div>

How to show div based on data sent to blade in Laravel

i have two divs (inputbarang and inputkategori). I want to make one of the divs disappear when there's no data in it. For example, when the $produk's value is not null, the inputbarang's div will appear, and the inputkategori's div will be hidden. Whereas, if $produk is empty, then the inputbarang's div will be hidden and the inputkategori's div will appear. I try like this. When $produk is null, the inputbarang's div still appears and the inputkategori's div doesn't appear
Blade.php:
#if($produk !== null)
<div class="form-group row " id="inputbarang" >
<label class="col-form-label col-lg-3 col-sm-12">Barang</label>
<div class="col-lg-4 col-md-9 col-sm-12">
<select class="form-control m-select2" width="500px" id="kt_select2_1" name="id_product[]" multiple="multiple">
#foreach($productall as $p)
#if(in_array($p->id, $productbyIds))
<option value="{{ $p->id }}" selected="true">{{ $p->product }}</option>
#else
<option value="{{ $p->id }}">{{ $p->product }}</option>
#endif
#endforeach
</select>
</div>
</div>
#else
<div class="form-group row" id="inputkategori" >
<label class="col-form-label col-lg-3 col-sm-12">Kategori</label>
<div class="col-lg-9 col-md-9 col-sm-12">
<select class="form-control m-select2" width="500px" id="kt_select2_3" name="id_kategori[]" multiple="multiple">
#foreach($kategoriall as $k)
#if(in_array($k->id, $kategoribyIds))
<option value="{{ $k->id }}" selected="true">{{ $k->kategori }}</option>
#else
<option value="{{ $k->id }}">{{ $k->kategori }}</option>
#endif
#endforeach
</select>
</div>
</div>
#endif
Controller:
$produk = Produk::where('m_product.delete', 0)
->join('tb_promo_detail', 'tb_promo_detail.id_product','=','m_product.id')
->select('m_product.id', 'm_product.product')
->where('tb_promo_detail.id_promo',$id)
->get();
Please correct me if its wrong.. Thank you so much!
Since you are sending a collection to your blade from controller you will need to check if that collection is empty or not. Just checking if it is null or empty array will not give you the right conditional effect.
So use:
#if($produk->first())
or you can also use
#if(!$produk->isEmpty())

Access old("value") in form edit

I'm currently working on an edit page. My edit perfectly shows the old (product name) input value, like so:
<div class="form-group">
<label for="product-input">#lang('product.name')</label>
<input id="product-input" type="text" name="name" class="form-control" value="{{ isset($product) ? $product->name : '' }}">
</div>
My question is, how can I do this with a select? How can I get the old selected option value when editing?
My code:
<div class="form-group">
<label>#lang('product.group')</label>
<select name="product_group_id" id="product_group_id" class="form-control" ng-model="productGroup" ng-change="changedType(val)">
<option style="display: none" value="">#lang('product.choose_group')</option>
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
</select>
</div>
Solution:
Added ng-selected to the option element, like so:
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}" ng-selected="{{ isset($product->product_group_id) ? $product->product_group_id == $product_group->id : ''}}">{{$product_group->name}}</option>
#endforeach
You need to check if the old value equals option value and then use selected attribute:
#foreach ($product_groups as $product_group)
<option #if(old('product_group_id') == $product_group->id) selected #endif value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
Even better considering the first time with default value:
<option
value="{{$product_group->id}}"
{{ old('product_group_id', $product->id) != $product_group->id?: 'selected' }}>
{{$product_group->name}}
</option>

Alternative to calling models in the views - Laravel

Trying to improve the code of my app and just migrated to L5. I used to call models in my views and I know this is not best practice - I should separate data calling and views completely.
However how do you deal with a situation like this:
<div class="field">
<label for="country">Country<sup>*</sup></label>
<select class="" id="country" name="country">
<option value="{{{ old('country') ? old('country') : '' }}}">{{{ old('country') ? App\Country::find(old('country'))->country_name : '' }}}</option>
#foreach ($countries as $studio_country)
<option value="{{ e($studio_country->id) }}">{{ e($studio_country->country_name) }}</option>
#endforeach
</select>
#if ($errors->has('country'))
<div class="alert alert-warning" role="alert">
{{ $errors->first('country') }}
</div>
#endif
<br>
</div>
Basically if there is an input and the input did not pass the validation the page refreshes with the old input and the error message. I need to extract the name of the country from the DB since I only have its ID.
You would want to do something like this:
<select id="country" name="country">
#foreach ($countries as $studio_country)
<option
value="{{ $studio_country->id }}"
{{ $studio_country->id === old('country') ? 'selected' : '' }}>
{{ $studio_country->country_name }}
</option>
#endforeach
</select>

Categories