)
I have been struggling with a form for my Laravel App....
I have 3 fields which are checkboxes and therefore is getting POSTed as arrays and then I have a textarea field and somehow SOMETIMES one of the 3 checkbox inputs get flooded with data from the textarea input field... It is very weird and only happens sometime and therefore I get a data to long truncated error in the database...
But I DD the input at the $request level and can see already there the array from the checkbox is flooded with the data from the textarea field... I can not wrap my head around why this is, as it's only sometimes...
And I can even get the error and go back a page in the browser and submit again with all the same data and then it can be working.. WHAT can cause this weird behavioer
This is my Laravel blade where the form is
#extends('layouts.app')
#section('head')
#endsection
#section('content')
#php
// dd($errors);
#endphp
<h2 class="page-title">Opret Artist Side</h2>
<div class="artist-box">
<form class="formgrid" method="POST" action="{{ route('artist.store') }}">
#csrf
<div class="formleft">
<label for="artist_name" class="label">Artist navn</label>
<input type="text" id="artist_name" name="artist_name" class="formitem #error('artist_name') is-invalid #enderror" value="{{ old('artist_name', $post->artist_name ?? null) }}" />
#error('artist_name')
<div class="invalid-feedback">
#foreach($errors->get('artist_name') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="artist_name_help">Jeres band/artist navn. <span class="text-danger">OBS! Kan IKKE ændres</span></small>
#enderror
</div>
<div class="formright">
<label for="contact_person" class="label">Kontakt person</label>
<input type="text" name="contact_person" id="contact_person" class="formitem #error('contact_person') is-invalid #enderror" value="{{ old('contact_person', $post->content ?? null) }}" />
#error('contact_person')
<div class="invalid-feedback">
#foreach($errors->get('contact_person') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="contact_person_help">Kontakt persons navn</small>
#enderror
</div>
<div class="formleft">
<label for="email" class="label">Email</label>
<input type="text" name="email" id="email" class="formitem #error('email') is-invalid #enderror" value="{{ old('email', $post->email ?? null) }}" />
#error('email')
<div class="invalid-feedback">
#foreach($errors->get('email') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="email_help">Kontakt email</small>
#enderror
</div>
<div class="formright">
<label for="phone" class="label">Tlf nummer</label>
<input type="text" name="phone" id="phone" class="formitem #error('phone') is-invalid #enderror" value="{{ old('phone', $post->phone ?? null) }}" />
#error('phone')
<div class="invalid-feedback">
#foreach($errors->get('phone') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="phone_help">Kontakt tlf nummer</small>
#enderror
</div>
<div class="formleft">
<label for="hometown" class="label">Hjemby</label>
<input type="text" name="hometown" id="hometown" class="formitem #error('hometown') is-invalid #enderror" value="{{ old('hometown', $post->hometown ?? null) }}" />
#error('hometown')
<div class="invalid-feedback">
#foreach($errors->get('hometown') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="hometown_help">Jeres Hjemby</small>
#enderror
</div>
<div class="formright">
<label for="category" class="label">Kategori</label>
<select name="category" id="category" class="formitem #error('category') is-invalid #enderror">
{{-- Show a non selectable if with info of none selected --}}
#if(old('category') === null)
<option disabled="disabled" selected value="null">Vælg en kategori</option>
#endif
#foreach($categories as $category)
<option #if(old('category') !== null && old('category') == $category->id) selected #endif value="{{ $category->id }}"
>{{ $category->artist_category }}</option>
#endforeach
</select>
#error('category')
<div class="invalid-feedback">
#foreach($errors->get('category') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Hvilken kategori hører i under</small>
#enderror
</div>
<div class="formfull">
<label for="budget" class="label">Budget i spiller for</label>
<ul class="ks-cboxtags">
#foreach($budgets as $budget)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('budget') !== null && in_array($budget->id,old('budget'))) checked #endif
type="checkbox" name="budget[]" id="budget{{ $budget->id }}" class="form-check-input #error('budget') is-invalid #enderror" value="{{ $budget->id }}" />
<label class="form-check-label" for="budget{{ $budget->id }}">{{ $budget->artist_budget }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('budget')
<div class="invalid-feedback d-block">
#foreach($errors->get('budget') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="budget_help">Vælg hvilket budget i optræder for, gerne flere</small>
#enderror
</div>
{{-- --}}
<div class="formfull">
<label for="genres" class="label">Genre</label>
<ul class="ks-cboxtags">
#foreach($genres as $genre)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('genres') !== null && in_array($genre->id,old('genres'))) checked #endif
type="checkbox" name="genres[]" id="genres{{ $genre->id }}" value="{{ $genre->id }}" class="form-check-input #error('genres') is-invalid #enderror" />
<label class="form-check-label" for="genres{{ $genre->id }}">{{ $genre->genre }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('genres')
<div class="invalid-feedback d-block">
#foreach($errors->get('genres') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Hvike genrer hører i under</small>
#enderror
</div>
{{-- --}}
<div class="formfull">
<label for="area" class="label">Områder hvor i optræder</label>
<ul class="ks-cboxtags">
#foreach($areas as $area)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('area') !== null && in_array($area->id,old('area'))) checked #endif
type="checkbox" name="area[]" id="area{{ $area->id }}" value="{{ $area->id }}" class="form-check-input #error('area') is-invalid #enderror" />
<label class="form-check-label" for="area{{ $area->id }}">{{ $area->artist_area }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('area')
<div class="invalid-feedback d-block">
#foreach($errors->get('area') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Vælg hvilke områder i optræder i</small>
#enderror
</div>
<div class="formleft">
<label for="desc" class="label">Beskriv jer selv</label>
<textarea name="description" id="description" class="formitem #error('description') is-invalid #enderror">{{ old('description', $post->description ?? null) }}</textarea>
{{-- <input id="description" value="{{ old('description', $post->description ?? null) }}" class="form-control #error('description') is-invalid #enderror" name="description" type="hidden">
#trix(\App\ArtistPage::class, 'trixinput',
[
'id' => 'description',
// 'class' => 'form-control',
'hideTools' => ['file-tools'],
'hideButtonIcons' => ['attach', 'link', 'code', 'strike', 'heading-1']
]) --}}
{{-- <input id="desc" value="{{ old('description', $post->description ?? null) }}" type="hidden" name="description">
<trix-editor input="desc" class="#error('description') is-invalid #enderror"></trix-editor> --}}
#error('description')
<div class="invalid-feedback">
#foreach($errors->get('description') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="description_help">Giv en god beskrivelse af jer selv og hvad i tilbyder<br>
Tilladte tags er <strong>{{ '<br><b><i><li><ul><ol><hr><strong><p>' }}</strong>
</small>
#enderror
</div>
<button type="submit" class="btn btn-primary">Opret</button>
</form>
</div>
#endsection
See also this image of the DD dump on the $request
Hmmmm Think I figured it out :-)
But it has been such a sporadic bug so I'm not sure yet... But seems like it has gone away :-)
Seems like it was this value line on each field I have gotten messed up
The value line structure on the input fields I had was
value="{{ old('artist_name', $post->artist_name ?? null) }}"
And it should be
value="{{ old('artist_name') ?? $post->artist_name ?? null }}"
Dont know why I ended up with such an obscure Value part on every input field :-)
But sometimes the eyes just gets blind on code :-)
Related
How can I export the data when I open a foreach in the application I developed in Laravel. For example, I want to filter after some id sync.
Blade/View
#foreach ($parts as $item)
#foreach ($country->getPerson ?? [] as $person)
#if ($country->id == $person->country_id)
#endif
#endforeach
#if ($loop->iteration == $person->part_id)
#continue
#endif
<div class="col-sm-6 col-lg-4">
<div class="service-item my-3">
<div class="service-wrapper m-0 bg-white">
<div class="service-content mt-3">
<h5 class="mb-3"> {{ $item->name }}</h5>
<a data-bs-toggle="modal" data-bs- target="#service1"
href="#" class="text-primary">
Alert!
<i class="fas fa-angle-right ms-2"> </i>
</a>
<br> <br>
<form class="call-to-action-form m-auto" action="{{ route('add.person') }}"
enctype="multipart/form-data" method="POST">
#csrf
<div class="input-group">
<input type="text" class="form-control" name="name"
placeholder="Adınız Soyadınız" required> <br>
<input type="text" class="form- control" name="email"
placeholder="E-Mail Adresiniz" required>
</div>
<input type="hidden" name="country_id" value="{{ $country->id }}" required>
<input type="hidden" name="part_id" value="{{ $item->part_id }}" required>
<br>
<button class="btn btn-success" type="submit">Cüzü Al
</button>
</form>
</div>
</div>
</div>
#endforeach
I want to return it here.
what I want to do is
{{person->part_id}}
by assigning its value outside the foreach
#if ($loop->iteration == $person->part_id)
#continue
#endif
My associated models run regularly. Now I need to filter the ids corresponding to my person table in my part table. The code sequence I shared above only takes the latest id. I cannot get other corresponding ids, so it only filters once.
i have this form, what i wanted to do is if the data for this id is exist, my submit button turn into update function.
i still use store data function for my controller because i didnt know how to do the logic.
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'category_page_id' => 'required',
]);
$seo = new Seo;
$seo->category_page_id = $request->input('category_page_id');
$seo->title = $request->input('title') ?? '';
$seo->description = $request->input('description');
$seo->save();
return back()->withStatus(__('SEO successfully updated.'));
}
and this is the form for my update function
<form method="post" action="{{ route('seo.update') }}" autocomplete="off">
#csrf
#method('put')
<h3 class="heading-small mb-4">{{ __('PAGE SEO') }}</h3>
#if (session('status'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('status') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#endif
<div class="pl-lg-4">
<div class="form-group{{ $errors->has('category_page_id') ? ' has-danger' : '' }}">
<div class="form-group form-box">
<label for="category_page_id">Page</label>
<select class="form-control form-control-alternative{{ $errors->has('category_page_id') ? ' is-invalid' : '' }}" name="category_page_id">
#foreach ($categorypages->slice(0, 1) as $key => $value)
<option value="{{ $value->id }}" #if (old('category_page_id') == $value->id) {{ 'selected' }} #endif>{{ $value->name }}</option>
#endforeach
</select>
#error('category_page_id')
<small class="text-red">{{ $message }}</small>
#enderror
</div>
</div>
<div class="form-group{{ $errors->has('title') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-title">{{ __('SEO Title') }}</label>
<input type="text" name="title" id="input-title" class="form-control form-control-alternative{{ $errors->has('title') ? ' is-invalid' : '' }}" placeholder="{{ __('title') }}" value="{{ old('title', $seo->title) }}" required autofocus>
#if ($errors->has('title'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('title') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('description') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-description">{{ __('Description') }}</label>
<input type="description" name="description" id="input-description" class="form-control form-control-alternative{{ $errors->has('description') ? ' is-invalid' : '' }}" placeholder="{{ __('Description') }}" value="{{ old('description', $seo->description) }}" required>
#if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
<div class="text-center">
<button type="submit" class="btn btn-success mt-4">{{ __('Save') }}</button>
</div>
</div>
</form>
what can i do to resolve my problem? is there any way to do this properly?
There are many ways to do
#if($seo_data != null) //here you can check the data is present or not
<form action="{{ route('seo_data.store')" method="post" >
#csrf
<input type="text" value="{{ isset($seo_data) ? $seo_data->title : '' }}">
<button type="submit" class="btn btn-primary">submit </button>
</form>
#else //if data is present then it goes to here
<form action="{{ route('seo_data.update',encrypt($seo_data->id))" method="post">
#csrf
#method('PATCH')
<input type="text" value="{{ isset($seo_data) ? $seo_data->title : '' }}">
<button type="submit" class="btn btn-primary">Update </button>
</form>
#endif
otherwise if you dont want to hit anther route then check it into store function
hello my radiobutton disappeared after a laravel configuration they are used to modify user roles they worked for a moment and then disappeared
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Modifier <strong> {{ $user->name }}</strong></div>
<div class="card-body">
<form action="{{ route('admin.user.update', $user) }}" method="POST">
#csrf
#method('PATCH')
#foreach ($roles as $role)
<div class="form-group form-check">
#foreach ($user->roles as $userRole)
#if ($userRole->id == $role->id)
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}" checked>
#else
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}">
#endif
#endforeach
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach
<button type="submit" class="btn btn-primary">Modifier les roles</button>
</form>
You can use contains() with key and value defined:
The contains() method determines whether the collection contains a given item:
#foreach ($roles as $role)
<div class="form-group form-check">
<input type="radio" class="form-check-input" name="roles[]" value="{{ $role->id }}" {{ $user->roles->contains('id', $role->id) ? 'checked' : '' }} >
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach
I am trying to follow this template design:Template and I seem to be having some issue regarding the card height and padding with my current login page. Here is the code:
#section('content')
<div class="container">
<div class="card row h-100">
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="row">
<div class="col">
<img class="img-fluid rounded mx-auto d-block" src="{{asset('svg/MegaDeskLogo.svg')}}" alt="logo" />
</div>
<div class="col">
<h1>{{ __('Login') }}</h1>
<input id="email" type="email" placeholder="{{ __('E-Mail Address') }}" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}"
name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>
{{ $errors->first('email') }}
</strong>
</span>
#endif
<input id="password" placeholder="{{ __('Password') }}" type="password" class="form-control {{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>
#if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>
{{ $errors->first('password') }}
</strong>
</span>
#endif
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old( 'remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
#if (Route::has('password.request'))
<a class="btn btn-link" href="{{route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
#endif
</div>
</div>
</form>
</div>
</div>
</div>
#endsection
Currently it's looking like: . I've tried reading several articles and reading the bootstrap documentation and still not able to find the answer. If anyone can offer any suggestions I would really appreciate it.
Make sure you're adding all the CSS and Javascript libraries and code you need for your card to look like the example, they are using more than just bootstrap.
Their CSS block:
Their Script block looks like:
Code in blade
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Name: <input type="text" name="name">
Phone: <input type="text" name="phone">
Email: <input type="text" name="email">
Validation Code
$data = $this->validate($request,[
'name' => 'required',
'phone' => 'required',
'email' => 'required',
]);
I wanna display each error under its input field.
You have to add error message after input field
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
<label for="name" class="col-sm-3 control-label">Name: </label>
<div class="col-sm-7">
<input class="form-control" required="required" name="name" type="text" id="name">
{{ $errors->first('name', '<p class="help-block">:message</p>') }}
</div>
</div>
To display each error seperatly you can just use $errors->first() and pass in the field name you're requesting. See the following example.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Name: <input type="text" name="name">
{{ $errors->first('name') }}
Phone: <input type="text" name="phone">
{{ $errors->first('phone') }}
Email: <input type="text" name="email">
{{ $errors->first('email') }}
Notice after each field I call $errors->first()
Name: <input type="text" name="name">
<small class="text-danger">{{ $errors->first('name') }}</small>
Phone: <input type="text" name="phone">
<small class="text-danger">{{ $errors->first('phone') }}</small>
Email: <input type="text" name="email">
{{ $errors->first('email') }}
If condition is not needed if u don't want to show all errors at a place.
Also, two types the error message can be display
{{ $errors->first('name') }}
<small class="text-danger">{{ $errors->first('phone') }}</small>