The 0 field is required. Laravel 5.5 Validation error - php

Am having a "The 0 field is required." error while trying save data into database when I have no field called 0. without validation from the Controller, the data saves but if I validate even just one field out of the six field I want to validate, I still get the error. How do I solve the issue. Please help out here is my view
<form method="post" action="{{ url('agent/add_tenantProperty') }}" data-toggle="validator">
{{ csrf_field() }}
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="txtMovieTitle">Tenant</label>
<select id="ddlGenge" class="form-control" name="tenant_id" required="">
#foreach($tenants as $tenant)
<option value="{{ $tenant->id }}">
{{ $tenant->designation }} {{ $tenant->firstname }} {{ $tenant->lastname }}
</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="ddlGenge">Asset Category</label>
<select id="ddlGenge" class="form-control" name="asset_id" required="">
<option>Choose a Property</option>
#foreach($assets as $asset)
<option value="{{ $asset->id }}">{{ $asset->category }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="txtDirector">Asset description</label>
<select id="ddlGenge" class="form-control" name="description" required="">
<option>Choose a Description</option>
#foreach($assets as $asset)
<option value="{{ $asset->description }}">{{ $asset->description }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="txtProducer">Location</label>
<select id="ddlGenge" class="form-control" name="address" required="">
<option>Choose an Address</option>
#foreach($assets as $asset)
<option value="{{ $asset->address }}">{{ $asset->address }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="txtWebsite">Standard price</label>
<input id="txtWebsite" type="text" class="form-control" name="price" required="">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="txtWriter">Date</label>
<input id="txtWriter" type="date" class="datepicker form-control" name="occupation_date"
required="">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<button type="submit" class="btn btn-outline btn-primary pull-right">Submit</button>
<br/>
</form>
and my controller
public function store(Request $request)
{
//validation
$this->validate($request, array([
'tenant_id' => 'required',
'asset_id' => 'required',
'description' => 'required',
'address' => 'required',
'price' => 'required',
'occupation_date' => 'required',
]));
//create and save new data
$tenantProperty = New TenantProperty();
$tenantProperty->tenant_id = $request->tenant_id;
$tenantProperty->asset_id = $request->asset_id;
$tenantProperty->description = $request->description;
$tenantProperty->address = $request->address;
$tenantProperty->price = $request->price;
$tenantProperty->occupation_date = $request->occupation_date;
$tenantProperty->save();
//redirect
return redirect('agent/tenantProperty_list');
}
with the route as follows
Route::get('add_tenantProperty', 'TenantPropertyController#create')->name('/add_tenantProperty');
Route::post('add_tenantProperty', 'TenantPropertyController#store');

When you just write $request, it passes the entire request object but the validate function expect both the arguments to be arrays.
So make a little change and you will be good to go:
$this->validate($request, array( // Removed `[]` from the array.
'tenant_id' => 'required',
'asset_id' => 'required',
'description' => 'required',
'address' => 'required',
'price' => 'required',
'occupation_date' => 'required',
));

The above answer is correct, this is another way to solve the validation problem on laravel 5.5 I asked
$validation = validator::make($request->all(), [
'tenant_id' => 'required',
'asset_id' => 'required',
'description' => 'required',
'address' => 'required',
'price' => 'required',
'occupation_date' => 'required',
]);
For more information visit https://laravel.com/docs/5.5/validation#manually-creating-validators

$request->validate([
'0'=>'',
'tenant_id' => 'required',
'asset_id' => 'required',
'description' => 'required',
'address' => 'required',
'price' => 'required',
'occupation_date' => 'required',]);

Related

I have problem with my store function in Laravel 9

basically every time I submit the form, the page refresh, and no items are stored in the DB. Here's the code.
PodcastController
public function store(Request $request) {
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,webp|max:2048',
'audio' => 'audio|mimes:audio/mpeg',
'category' => 'required',
]);
if ($request->image) {
$imageName = uniqid() . '.' . $request->image->extension();
$request->image->move(public_path('podcast/images'), $imageName);
}
if ($request->audio) {
$audioName = uniqid() . '.' . $request->audio->extension();
$request->audio->move(public_path('podcast/audios'), $audioName);
}
Podcast::create([
'title' => $request->title,
'description' => $request->description,
'image' => $imageName,
'audio' => $audioName,
'category' => $request->category,
]);
return redirect()->route('podcast.list')->with('Success', 'Podcast Created!');
}
Route
Route::resource(
'podcasts',
App\Http\Controllers\PodcastController::class
);
create.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="col-md-6">
<form action="{{ route('podcasts.store') }}" method="POST" enctype=multipart/form-data>
#csrf
<input class="form-control" type="text" name="title" placeholder="title">
<input class="form-control mt-4" type="textarea" name="description" placeholder="description">
<select name="category" class="form-control mt-4">
<option value="select" disabled>Are you a Creator or a User?</option>
<option value="news">News</option>
<option value="music">Music</option>
</select>
<label for="image" class="mt-3">Image</label>
<input type="file" class="form-control" id="image" name="image">
<label for="audio" class="mt-3">Audio</label>
<input type="file" class="form-control" id="video" name="audio">
<button type="submit" class="btn btn-primary mt-3">Submit</button>
</form>
</div>
</div>
#endsection
The page keep refreshing everytime I run submit. I don't know where is the problem.
You catching a failure while validating. After catching these validate errors, you should develop your code what to do.
https://laravel.com/api/9.x/Illuminate/Validation/ValidationException.html
try {
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,webp|max:2048',
'audio' => 'audio|mimes:audio/mpeg',
'category' => 'required',
]);
} catch (\Illuminate\Validation\ValidationException $e) {
// do something...
dd($e->errors());
}

Laravel 302 when i do a validation

When I do post form in laravel and then validate it from the controller I get this response :
302 found
nothing useful, I`ve tried everything but nothing worked with me.
My Form blade :
<form action="{{route('newitem')}}" method="post">
#csrf
<div class="mb-3">
<label for="item name" class="form-label">Email address</label>
<input type="text" class="form-control" id="item name" name="item_name" >
</div>
<div class="mb-3">
<label for="price" class="form-label">Price</label>
<input type="number" class="form-control" id="price" name="item_price">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
my controller :
public function new_item(Request $rq){
$validated = $rq->validate(
[
'item_name' => 'required|string|min:4|max:90',
'item_desc' => 'string|min:4|max:90',
'item_price' => 'required|integer|min:4'
]
);
UsrsItem::create([
'item_name' => $validated->item_title,
'item_price' => $validated->item_price,
]);
}
I hope someone can help me with that :<
Contrroller Code
public function new_item(Request $rq){
$validated = $rq->validate(
[
'item_name' => 'required|string|min:4|max:90',
'item_desc' => 'string|min:4|max:90',
'item_price' => 'required|integer|min:4'
]
);
if ($validator->fails())
{
return response()->json(['errors'=>$validator->errors()->all()]);
}
UsrsItem::create([
'item_name' => $validated->item_title,
'item_price' => $validated->item_price,
]);
return response()->json(['success'=>'Record is successfully added']);
}
Put This In Blade File
#if ($errors->has())
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
{{ $error }}<br>
#endforeach
</div>
#endif

Why is the section of my view disappears?

I have a view made with livewire and it contains two dynamically add sections for spoken languages and the other one for courses and certificates. The language section was working fine until I added the add certificates section. now when the user clicks on the add section for the certificate section to add a new one, one of the added language sections disappears and then will appear again once you add another one. I guess it will appear again once the view re-renders. I have been going back and force moving functions around from render method to update/updated/hydrate/dehydrate but got no luck. at first, I thought it had something to do with the $loop->index that I use in my view but after changing that I realized it was not it. I'm hitting a dead-end here and can't figure out what's going on here.
I also made a screen record of what is happening so it might help:
https://drive.google.com/file/d/1Hq2wTKcPvhs05SKFMICaRoOAzpSyj9Iz/view?usp=sharing
View section:
<!-- language section -->
<div class="card card-profile shadow-sm mt-4">
<div class="px-4 mt-4 mb-4">
<div class="h5 font-weight-bold mb-4">Spoken languages</div>
<div class="heading text-muted mb-4">you only can add 3 languages to your profile.</div>
#foreach ($languages as $lindex => $language)
<div class="card card-body mb-4" wire:key="{{ $lindex }}">
<div class="text-left"><span class="fa fa-trash text-gray language-delete" wire:click="removeLanguage({{ $lindex }}, {{ !empty($language['id']) ? $language['id'] : 0 }})"></span></div>
<div class="row">
<div class="form-group col-md-6">
<label class="" for="languageName">language</label>
<select class="form-control form-control-alternative" name="language-name" {{-- id="languageName" --}} wire:model="languages.{{ $lindex }}.language_name">
<option value="" class="form-control" selected disabled>select language</option>
#foreach ($language_names as $name)
<option value="{{ $name->abbreviation }}" class="form-control">{{ $name->language }}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-6">
<label class="" for="languageProficiency">proficiency level</label>
<select class="form-control form-control-alternative" name="language-proficiency" {{-- id="languageProficiency" --}} wire:model="languages.{{ $lindex }}.language_level">
<option value="" class="form-control" selected disabled>proficiency level</option>
#foreach ($language_levels as $level)
<option value="{{ $level->level }}" class="form-control">{{ $level->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
#endforeach
#error('languages.*.language_level')
<small class="text-warning">{{ $message }}</small>
#enderror
#error('languages.*.language_language')
<small class="text-warning">{{ $message }}</small>
#enderror
#if (count($languages) < 3)
<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-outline-secondary btn-round btn-block" wire:click="addLanguage"><span class="btn-inner--icon"><i class="fa fa-plus fa-2x"></i></span></button>
</div>
</div>
#endif
</div>
</div>
<!-- end language section -->
<!-- other certificates section -->
<div class="card card-profile shadow-sm mt-4">
<div class="px-4 mt-4 mb-4">
<div class="h5 font-weight-bold mb-4">Other related certificates</div>
<div class="heading text-muted mb-4">if you have other related certificates in your files you can add then here.</div>
#foreach ($certificates as $cindex => $certificate)
<div class="card card-body mb-4" wire:key="{{ $cindex }}">
<div class="row">
<div class="form-group col-md-6">
<label class="" for="other-certificates-name">certificate name</label>
<input type="text" class="form-control form-control-alternative" placeholder="" name="ptherCertificatesName">
</div>
<div class="form-group col-md-6">
<label class="" for="other-certificates-school-name">School name</label>
<input type="text" class="form-control form-control-alternative" placeholder="" name="otherCertificatesSchoolName">
</div>
<div class="form-group col-md-6">
<label class="" for="other-certificates-verification-link">URL<small>(optional)</small></label>
<input type="text" class="form-control form-control-alternative text-left" placeholder="" name="otherCertificatesVerificationLink">
</div>
<div class="form-group col" dir="ltr">
<label class="" for="other-certificates-grad-date" dir="rtl">finished date</label>
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-calendar-grid-58"></i></span>
</div>
<input type="text" class="form-control form-control-alternative datePicker" placeholder="" name="otherCertificatesGradDate" value="">
</div>
</div>
</div>
</div>
#endforeach
#if (count($certificates) < 5)
<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-outline-secondary btn-round btn-block" wire:click="addCertificate"><span class="btn-inner--icon"><i class="fa fa-plus fa-2x"></i></span></button>
</div>
</div>
#endif
</div>
</div>
<!-- end other certificates section -->
Livewire component controller:
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\CityName;
use App\Models\Language;
use App\Models\LanguageLevel;
use App\Models\User;
use App\Models\UserLanguage;
use App\Models\UserProfile;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
class UserInfo extends Component
{
public $name;
public $email;
public $phone;
public $states = [];
public $selectedstate;
public $cities = [];
public $selectedcity;
public $jobTitle;
public $aboutMe;
public $employer;
public $position;
public $edu_course;
public $edu_school;
public $edu_level;
public $edu_start_date;
public $edu_end_date;
public $employment_looking;
public $employment_hired;
public $twitter;
public $linkedin;
public $github;
public $instagram;
public $website;
public $languages = [];
public $language_names;
public $language_levels;
public $languageToDelete = [];
public $certificates = [];
public $certificate_name;
public $certificate_school;
public $certificate_link;
public $certificate_date;
public $certificateToDelete = [];
public $skillsQuery;
public $skillResults;
public $skills;
public function render()
{
$this->retriveStates();
$this->retriveCities();
$this->retriveLanguages();
return view('livewire.user-info');
}
public function retriveStates()
{
$this->states = CityName::distinct()->get(['state']);
}
public function retriveCities()
{
$this->cities = CityName::where('state', $this->selectedstate)->get(['city']);
}
public function updatedSkillsQuery()
{
$this->skillResults = $this->skills->where('name', 'like', $this->skillsQuery) ?? collect();
}
public function retriveLanguages()
{
$this->language_names = Language::all();
$this->language_levels = LanguageLevel::all();
}
public function addLanguage()
{
if (count($this->languages) <= 3)
{
array_push($this->languages, ['language_name'=>'', 'language_level'=>'', 'id'=>'']);
}
else
{
$this->sweetAlert('error', 'you only can add 3 languages to your profile.');
}
}
public function getUserLanguages()
{
$this->languages = auth()->user()->userLanguages->toArray();
}
public function removeLanguage($languagePosition, $languageId)
{
if (isset($languageId))
{
if ($languageId == 0)
{
array_splice($this->languages, $languagePosition, 1);
}
else
{
array_push($this->languageToDelete, $languageId);
array_splice($this->languages, $languagePosition, 1);
}
}
}
public function addCertificate()
{
if (count($this->certificates) <= 5)
{
array_push($this->certificates, ['name'=>'', 'school'=>'', 'link'=>'', 'date'=>'', 'id'=>'']);
/* dd($this->languages); */
}
else
{
$this->sweetAlert('error', 'you only can add 5 certificates to your profile.');
}
}
public function mount()
{
$this->skills = collect([
['name' => 'vue'],
['name' => 'vue'],
['name' => 'vue'],
['name' => 'laravel'],
['name' => 'laravel'],
['name' => 'laravel'],
]);
$this->skillResults= [];
$this->skillsQuery = '';
/* $this->retriveLanguages();
$this->retriveStates();
$this->retriveCities(); */
$this->getUserLanguages();
$this->name = auth()->user()->name;
$this->email = auth()->user()->email;
$this->phone = auth()->user()->phone;
$this->selectedstate = auth()->user()->userprofile->state;
$this->selectedcity = auth()->user()->userprofile->city;
$this->jobTitle = auth()->user()->userprofile->job_title;
$this->aboutMe = auth()->user()->userprofile->about_me;
$this->employer = auth()->user()->userprofile->employer;
$this->position = auth()->user()->userprofile->position;
$this->edu_course = auth()->user()->userprofile->edu_course;
$this->edu_school = auth()->user()->userprofile->edu_school;
$this->edu_level = auth()->user()->userprofile->edu_level;
$this->edu_start_date = auth()->user()->userprofile->edu_start_date;
$this->edu_end_date = auth()->user()->userprofile->edu_end_date;
$this->employment_looking = auth()->user()->userprofile->employment_looking;
$this->employment_hired = auth()->user()->userprofile->employment_hired;
$this->twitter = auth()->user()->userprofile->twitter;
$this->linkedin = auth()->user()->userprofile->linkedin;
$this->github = auth()->user()->userprofile->github;
$this->instagram = auth()->user()->userprofile->instagram;
$this->website = auth()->user()->userprofile->website;
}
public function rules()
{
return [
'name' => ['required', 'string', 'max:250'],
'email' => [
'required',
'email',
'max:250',
Rule::unique('users')->ignore(auth()->id()),
],
'phone' => ['required', 'digits:11'],
'selectedstate' => 'required',
'selectedcity' => 'required',
'jobTitle' => ['required', 'string', 'max:250'],
'aboutMe' => ['required', 'string', 'max:250'],
'employer' => ['string', 'max:250'],
'position' => ['string', 'max:250'],
'edu_course' => ['nullable', 'string', 'max:250'],
'edu_school' => ['nullable', 'string', 'max:250'],
'edu_level' => ['nullable', 'string', 'max:250'],
'edu_start_date' => ['nullable', 'string'],
'edu_end_date' => ['nullable', 'string'],
'employment_looking' => ['nullable', 'boolean'],
'employment_hired' => ['nullable', 'boolean'],
'twitter' => ['nullable', 'string', 'max:250'],
'linkedin' => ['nullable', 'string', 'max:250'],
'github' => ['nullable', 'string', 'max:250'],
'instagram' => ['nullable', 'string', 'max:250'],
'website' => ['nullable', 'string', 'max:250'],
'languages.*.language_name' => ['required', 'exists:App\Models\Language,abbreviation'],
'languages.*.language_level' => ['required', 'exists:App\Models\LanguageLevel,level'],
];
}
public function submit()
{
$user = Auth::user();
$this->validate();
User::where('id', auth()->id())->update([
'name' => $this->name,
'email' => $this->email,
'phone' => $this->phone,
]);
UserProfile::where('user_id', auth()->id())->update([
'state' => $this->selectedstate,
'city' => $this->selectedcity,
'job_title' => $this->jobTitle,
'about_me' => $this->aboutMe,
'employer' => $this->employer,
'position' => $this->position,
'edu_course' => $this->edu_course,
'edu_school' => $this->edu_school,
'edu_level' => $this->edu_level,
'edu_start_date' => $this->edu_start_date,
'edu_end_date' => $this->edu_end_date,
'employment_looking' => $this->employment_looking,
'employment_hired' => $this->employment_hired,
'twitter' => $this->twitter,
'linkedin' => $this->linkedin,
'github' => $this->github,
'instagram' => $this->instagram,
'website' => $this->website,
]);
if (!empty($this->languageToDelete))
{
/* $user = Auth::user(); */
foreach ($this->languageToDelete as $delete)
{
$user->userLanguages()->where('id', $delete)->delete();
}
}
foreach ($this->languages as $language)
{
/* $user = Auth::user(); */
$user->userLanguages()->updateOrCreate([
'language_name' => $language['language_name'],
],
[
'language_name' => $language['language_name'],
'language_level' => $language['language_level']
]
);
}
$this->getUserLanguages();
$this->sweetAlert('success', 'changes saved!');
}
public function sweetAlert($type, $message)
{
$this->alert($type, $message, [
'position' => 'bottom-end',
'timer' => 5000,
'toast' => true,
'text' => null,
'showCancelButton' => false,
'showConfirmButton' => false
]);
}
}
The issue has been solved. the problem indeed was a DOM issue caused by multiple loop index. as what I thought at first the $loop->index was causing it and I was on the right track to change it to something else like $languages as $lindex => $language but apparently even by doing that those two sections will still have the same wire:key because of the loop iterations which returns 0,1,2 and so on in order. so by appending a string to the key like lang{{ $loop->index }} they will be unique and therefore problem solved.

Validation in dynamic form Laravel 5.5

How can I create a validtion for a dynamic form?
//Controller
public function store(Request $request)
{
$rules = [
'companyName' => 'required',
'bannerName' => 'required',
'bannerDescription' => 'required',
'bannerURL' => 'required',
'bannerImg' => 'required',
];
$customMessages = [
'companyName.required' => 'Yo, what should I call you?',
'bannerName.required' => 'Yo, what should I call you?',
'bannerDescription.required' => 'Yo, what should I call you?',
'bannerURL.required' => 'Yo, what should I call you?',
'bannerImg.required' => 'Yo, what should I call you?',
];
$this->validate($request, $rules, $customMessages);
}
And here is my view, my "name" attr are arrays cause I'll store a bulk of data to DB. Clicking on "+ Add Banner" jquery will clone div with the same 4 inputs.
If I'll remove arrays everything will work ofc, but if no I'll get the following error
htmlspecialchars() expects parameter 1 to be string, array given
{!! Form::open(['action' => 'CompanyController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group{{ $errors->has('companyName') ? ' has-error' : '' }}">
<label for="companyName">Company URL Address</label>
<input type="text" class="form-control" value="{{old('companyName')}}" id="companyName" name="companyName" placeholder="example.com">
<small class="text-danger">{{ $errors->first('companyName') }}</small>
</div>
<hr>
<div data-sel-baner-box>
<div data-sel-baner-form>
<div class="panel-heading"><h4 style="text-align: center">New banner</h4></div>
<div class="form-group{{ $errors->has('bannerName') ? ' has-error' : '' }}">
<label for="bannerName">Title</label>
<input type="text" class="form-control" id="bannerName" value="{{old('bannerName')}}" name="bannerName[]" placeholder="Name">
<small class="text-danger">{{ $errors->first('bannerName') }}</small>
</div>
<div class="form-group{{ $errors->has('bannerDescription') ? ' has-error' : '' }}">
<label for="bannerDescription">Banner Description</label>
<input type="text" class="form-control" id="bannerDescription" value="{{old('bannerDescription')}}" name="bannerDescription[]" placeholder="Description">
<small class="text-danger">{{ $errors->first('bannerDescription') }}</small>
</div>
<div class="form-group{{ $errors->has('bannerURL') ? ' has-error' : '' }}">
<label for="bannerURL">Banner URL</label>
<input type="text" class="form-control" id="bannerURL" value="{{old('bannerURL')}}" name="bannerURL[]" placeholder="URL">
<small class="text-danger">{{ $errors->first('bannerURL') }}</small>
</div>
<div class="form-group{{ $errors->has('bannerImg') ? ' has-error' : '' }}">
<label for="bannerImg">File input</label>
<input type="file" class="form-control-file" id="bannerImg" name="bannerImg[]">
<small class="text-danger">{{ $errors->first('bannerImg') }}</small>
</div>
</div>
+ Add Banner
<button type="submit" class="btn btn-primary">Save Company</button>
{!! Form::close() !!}
Any hints please?
You have to use dot notation for array input validation rules as:
$rules = [
'companyName.*' => 'required',
'bannerName.*' => 'required',
'bannerDescription.*' => 'required',
'bannerURL.*' => 'required',
'bannerImg.*' => 'required',
];
You can see docs here.

How to keep multiple fields and label in one form group in yii2 bootstrap active form

I am working on yii2 bootstrap active form and I need to keep multiple inputs in one form group .
like
<div class="form-group field-phn required">
<label class="control-label" >Home Phone</label>
<select id="dialCode" class="form-control" name="AddPatientForm[home_dial_code]">
<option value="2">355,ALB</option>
<option value="3">213,DZA</option>
<option value="6">244,AGO</option>
<option value="224">971,ARE</option>
</select>
<input type="text" id="home_phn" class="form-control" name="AddPatientForm[home_phn]">
<p class="help-block help-block-error"></p>
</div>
php code I am trying is
<?php echo \yii\bootstrap\Html::activeLabel($objAddPatientFrm, 'home_phn') ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_dial_code', [ 'inputOptions' => [ 'id' => 'dialCode']])->dropDownList($dialCodeArray)->label(false); ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_phn', [ 'inputOptions' => [ 'id' => 'home_phn']]); ?>
But the output is
<label for="addpatientform-home_phn">Home Phn</label> <div class="form-group field-dialCode required">
<select id="dialCode" class="form-control" name="AddPatientForm[home_dial_code]">
<option value="2">355,ALB</option>
<option value="3">213,DZA</option>
<option value="6">244,AGO</option>
<option value="224">971,ARE</option>
</select>
<p class="help-block help-block-error"></p>
</div>
<div class="form-group field-home_phn required">
<input type="text" id="home_phn" class="form-control" name="AddPatientForm[home_phn]">
<p class="help-block help-block-error"></p>
</div>
keeping both inputs and label in seperate form group .
Please suggest what can I do ?
Use following way to display forms:
// With 'default' layout you would use 'template' to size a specific field:
echo $form->field($model, 'demo', [
'template' => '{label} <div class="row"><div class="col-sm-4">{input}{error}{hint}</div></div>'
]);
// Input group
echo $form->field($model, 'demo', [
'inputTemplate' => '<div class="input-group"><span class="input-group-addon">#</span>{input}</div>',
]);
Hope it will help..
My solution to this is to prevent the $form->field() from rendering its own form-group class. This is achieved by using the options property and setting options.class to an empty string (or to include any classes other than form-group). Then wrap the fields in a <div class="form-group">.
Here's the code:
<div class="form-group">
<?= $form->field($Model, 'attribute1', ['options' => ['class' => '']]); ?>
<?= $form->field($Model, 'attribute2', ['options' => ['class' => '']]); ?>
</div>
For the OP's example:
<div class="form-group field-phn required">
<?php echo \yii\bootstrap\Html::activeLabel($objAddPatientFrm, 'home_phn') ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_dial_code', [ 'inputOptions' => [ 'id' => 'dialCode'], 'options' => ['class' => '']])->dropDownList($dialCodeArray)->label(false); ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_phn', [ 'inputOptions' => [ 'id' => 'home_phn'], 'options' => ['class' => '']]); ?>
</div>

Categories