Laravel not validate html select box - laravel

I have problem with validating html select. I put rules and all other form controls work fine but only select not show validate message.
Model
class Post extends Model
{
protected $table = 'post';
static $rules = [
'title' => 'required',
'slug' => 'required',
'user_id' => 'required|not_in:0',
'meta_title' => 'required',
'meta_description' => 'required',
'post_category_id' => 'required'
];
}
Blade
<div class="form-group">
{{ Form::label('post_category_id', 'Category') }}
<select name="post_category_id" class="form-control">
<option value="0">Select Category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $post->post_category_id ? 'selected' : ''}}>{{ $category->name }}</option>
#endforeach
</select> {!! $errors->first('post_category_id', '<div class="invalid-feedback">:message</div>') !!}
</div>
Controller
public function store(Request $request)
{
request()->validate(Post::$rules);
$post = Post::create($request->all());
Alert::alert('Success', 'Post created successfully.', 'Type');
return redirect()->route('admin.post.index');
}
What i do wrong here?

Even if the option has the value 0 it is still present so, no error will be triggered for "required" rule
Use a disabled option instead
<select name="post_category_id" class="form-control">
<option disabled>Select a Category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $post->post_category_id ? 'selected' : ''}}>{{ $category->name }}</option>
#endforeach
</select>

As far as I see, you have a default value set on your placeholder of the select, so the input will always be valid since its always sent to the backend.
take a look at this line : Select Category
you're setting a default value, remove it and try again:
in your blade do this:
<div class="form-group">
{{ Form::label('post_category_id', 'Category') }}
<select name="post_category_id" class="form-control">
<option>Select Category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $post->post_category_id ? 'selected' : ''}}>{{ $category->name }}</option>
#endforeach
</select> {!! $errors->first('post_category_id', '<div class="invalid-feedback">:message</div>') !!}
</div>

Related

How get old value on select in laravel blade?

i have filter form using select dropdown , but i cant get this old value after i submit this filter like input like type text . someone can help ?
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
i add my function controller :
public function search_filter_alkes(Request $request)
{
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$user_id = $request->user_id;
$alat = Alat::with('users')->where('user_id',$user_id)
->where('jenis', 'Alkes')->
get();
session()->put('user_id',$user_id);
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
->with('user_id', $user_id)
;
}
as per your code use this and make sure you use withInput()
return redirect()->back()->withErrors($validator)->withInput();
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}" {{ old('user_id') == $id ? "selected" :""}}>{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
EDITED
When you use redirect() after post method you have to use withInput() as i mention above example
return redirect('route')->withInput();
when you use view() you have to pass data like as array like your code then use same variable name
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
<option value="{{ $id }}" {{ $user_id == $id ? "selected" :""}}>{{ $nama_unit }}</option>
You wil have to compare old value with input key. So replace the below code inside your for loop
#if (Input::old('user_id') == $id)
<option value="{{ $id }}" selected>{{ $nama_unit }}</option>
#else
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endif
You have multiple options:
<select name="tags[]" class="form-control select-tag" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
#endforeach
</select>
OR
<select name="gender" class="form-control" id="gender">
<option value="">Select Gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
</select>
This worked for me in laravel 8:
<select class="form-control" id="gender" name="gender" required>
<option value="">Select gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
<option value="O" #if (old('gender') == "O") {{ 'selected' }} #endif>Other</option>
</select>

How can I show the old value in option fields in Laravel?

I want to show the old select value in this select field when I want to edit. How can I show my old value in these fields?
https://github.com/shakibzaman/book-library-laravel
you can show my GitHub repo-
Here is my rules edit page:
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<label for="chap_id">{{ trans('cruds.chapters.title_singular') }}*</label>
<select name="chap_id" id="chap_id" class="form-control select2">
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}">{{ $chapter }}</option>
#endforeach
</select>
#if($errors->has('name'))
<em class="invalid-feedback">
{{ $errors->first('name') }}
</em>
#endif
<p class="helper-block">
{{ trans('cruds.permission.fields.title_helper') }}
</p>
</div>
editController
public function edit($id)
{
if (!Gate::allows('users_manage')) {
return abort(401);
}
$rule = Rule::find($id);
$chapters = Chapter::all()->pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');
return view('admin.rules.edit', compact('rule', 'chapters'));
}
You can use the old() function to get the old value of a field. This function requires one input and that is the name of the field: old('chap_id').
You could use this one:
#foreach($chapters as $id => $chapter)
#if(null !== old('chap_id') && old('chap_id') === $id)
<option value="{{ $id }}" selected>{{ $chapter }}</option>
#else
<option value="{{ $id }}">{{ $chapter }}</option>
#endif
#endforeach
Or the shorter one:
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}" {{ old('chap_id') === $id ? 'selected' : '' }}>{{ $chapter }}</option>
#endforeach

categories appear in one post laravel

why all categories appear in one post, I just want to display the value category that matches the post.
public function edit(Post $post)
{
$categories = Category::all();
$tags = Tag::all();
return view('admin.post.edit', compact('post', 'categories', 'tags'));
}
<select id="category" name="categories[]" style="width: 100%" multiple>
#foreach($categories as $category)
<option
#foreach ($categories as $postCategory)
{{ $postCategory->id == $category->id ? 'selected' : '' }}
#endforeach
value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
<script>
$(document).ready(function () {
$("#category").select2({
placeholder: "Please Select"
});
});
</script>
Assuming $post will have a category_id you just need to check that field against the id of the current $category being iterated:
<select id="category" name="categories[]" style="width: 100%" multiple>
#foreach($categories as $category)
<option {{ $post->category_id == $category->id ? 'selected' : '' }}
value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>

Laravel select optgroup and separated values from database

I write form to send values to database:
{!! Form::model($product, ['method'=>'PATCH','class'=>'form-horizontal','action'=>['ProductsController#updatechangeStatus', $product->id]]) !!}
{{ Form::select('userstan_id', array(
'Użytkownicy' => array($userList),
'Magazyny' => array($warehouseList),
), null, ['class' => 'form-control'])}}
{!! Form::submit('Tak, przenieś', ['class' => 'btn btn-primary']) !!}
{!! link_to('warehouse/'. $product->id, $title = 'Anuluj', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
public function changestatus($id){
$product = Product::find($id);
$userList = User::lists('name', 'id');
$warehouseList = warehouse::lists('name_warehouse', 'id');
return view('pages.changestatus', compact('userList', 'product', 'warehouseList'));
}
Laravel returned values in Form::select like this: http://iv.pl/images/97320689105137896288.png
but I want to return a result like this:
http://iv.pl/images/48707724105931389272.png
How can I do to be returned only name user and name warehouse and value was id number in Laravel (I tring with foreach, but in array foreach not working), like this:
<select>
<optgroup label="Użytkownicy">
<option value="1">michal</option>
<option value="2">mateusz</option>
</optgroup>
<optgroup label="Magazyny">
<option value="1">kosz</option>
<option value="2">zaginione</option>
</optgroup>
</select>
OK I write this: (now Laravel returned records correct look: klik!)
<select name="userstan_id" class="form-control">
<optgroup label="Użytkownicy">
#foreach($userListName as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
#endforeach
</optgroup>
<optgroup label="Magazyny">
#foreach($warehouseList as $warehouse)
<option value="{{ $warehouse->id }}">{{ $warehouse->name_warehouse }}</option>
#endforeach
</optgroup>
</select>
Where name="userstan_id" is name of column where you want save or update record ;)

how to display both id and value in dropdown list in laravel?

This is my code in my View
{{ Form::label('supplier_list', 'Supplier', array('class' => 'control-label')) }}
{{ Form::select('supplier', $supplier_list, null, array('class' => 'form-control')) }}
This is my code in controller
$supplier_list = Supplier::lists('supplier_name', 'id');
My ouput is a Dropdown of SupplierName, what would I need is a Dropdown of displaying both SupplierName and ID, How to do it in laravel 4.2 ?
Try this:
In Controller
$supplier_list = Supplier::lists('supplier_name', 'id')->get();
In Template
<select name="id" class="form-control">
<option value="">Select Supplier Name</option>
#foreach ($supplier_list as $key => $v)
<option value="{!! $v['id'] !!}">{!! $v['supplier_name'] !!}{!! $v['id'] !!}</option>
#endforeach
</select>

Resources