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
Related
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());
}
I am making a blogging application with Laravel 8 and Bootstrap 5.
I run into a problem with adding an article into the articles table.
In the migration file that generated the articles table, I have:
public function up() {
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedInteger('category_id');
$table->foreign('category_id')->references('id')->on('catergories');
$table->string('title');
$table->string('slug');
$table->string('short_description');
$table->longText('content');
$table->tinyInteger('featured')->default('0');
$table->string('image')->nullable();
$table->timestamps();
});
}
In the Article model, I have:
class Article extends Model {
use HasFactory;
protected $fillable = [
'user_id',
'category_id',
'title',
'slug',
'short_description',
'content',
'featured',
'image',
];
// Join users to articles
public function user() {
return $this->belongsTo(User::class);
}
// Join categories to articles
public function category() {
return $this->belongsTo(ArticleCategory::class);
}
}
In the controller, I have:
namespace App\Http\Controllers\Dashboard;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Models\ArticleCategory;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller {
private $rules = [
'category_id' => 'required|exists:article_categories,id',
'title' => 'required|string|max:255',
'short_description' => 'required|string|max:255',
'image' => 'image|mimes:jpeg,png,jpg|max:2048',
'content' => 'required|string',
'featured' => 'required'
];
private $messages = [
'category_id.required' => 'Please pick a category for the article',
'title.required' => 'Please provide a title for the article',
'short_description.required' => 'The article needs a short description',
'content.required' => 'Please add content'
];
public function categories() {
return ArticleCategory::all();
}
public function index() {
$articles = Article::paginate(10);
return view('dashboard/articles',
['articles' => $articles]
);
}
public function create() {
// Load the view and populate the form with categories
return view('dashboard/add-article',
['categories' => $this->categories()]
);
}
public function save(Request $request) {
// Validate form (with custom messages)
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors())->withInput();
}
$fields = $validator->validated();
// Upload article image
$current_user = Auth::user();
if (isset($request->image)) {
$imageName = md5(time()) . $current_user->id . '.' . $request->image->extension();
$request->image->move(public_path('images/articles'), $imageName);
}
// Data to be added
$form_data = [
'user_id' => Auth::user()->id,
'category_id' => $fields['category_id'],
'title' => $fields['title'],
'slug' => Str::slug($fields['title'], '-'),
'short_description' => $fields['short_description'],
'content' => $fields['content'],
'image' => $fields['image'],
'featured' => $fields['featured']
];
// Insert data in the 'articles' table
$query = Article::create($form_data);
if ($query) {
return redirect()->route('dashboard.articles')->with('success', 'Article added');
} else {
return redirect()->back()->with('error', 'Adding article failed');
}
}
}
The form:
<form method="POST" action="{{ route('dashboard.articles.add') }}" enctype="multipart/form-data" novalidate>
#csrf
<div class="row mb-2">
<label for="title" class="col-md-12">{{ __('Title') }}</label>
<div class="col-md-12 #error('title') has-error #enderror">
<input id="title" type="text" placeholder="Title" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="short_description" class="col-md-12">{{ __('Short description') }}</label>
<div class="col-md-12 #error('short_description') has-error #enderror">
<input id="short_description" type="text" placeholder="Short description" class="form-control #error('short_description') is-invalid #enderror" name="short_description" value="{{ old('short_description') }}" autocomplete="short_description" autofocus>
#error('short_description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="category" class="col-md-12">{{ __('Category') }}</label>
<div class="col-md-12 #error('category_id') has-error #enderror">
<select name="category_id" id="category" class="form-control #error('category_id') is-invalid #enderror">
<option value="0">Pick a category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<div class="col-md-12 d-flex align-items-center switch-toggle">
<p class="mb-0 me-3">Featured article?</p>
<input class="mt-1" type="checkbox" name="featured" id="featured">
<label class="px-1" for="featured">{{ __('Toggle') }}</label>
</div>
</div>
<div class="row mb-2">
<label for="image" class="col-md-12">{{ __('Article image') }}</label>
<div class="col-md-12 #error('image') has-error #enderror">
<input type="file" name="image" id="file" class="file-upload-btn">
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="content" class="col-md-12">{{ __('Content') }}</label>
<div class="col-md-12 #error('content') has-error #enderror">
<textarea name="content" id="content" class="form-control #error('content') is-invalid #enderror" placeholder="Content" cols="30" rows="6">{{ old('content') }}</textarea>
#error('content')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-0">
<div class="col-md-12">
<button type="submit" class="w-100 btn btn-primary">
{{ __('Save') }}
</button>
</div>
</div>
</form>
The routes:
// Article routes
Route::group(['prefix' => 'articles'], function() {
Route::get('/', [ArticleController::class, 'index'])->name('dashboard.articles');
Route::get('/new', [ArticleController::class, 'create'])->name('dashboard.articles.new');
Route::post('/add', [ArticleController::class, 'save'])->name('dashboard.articles.add');
});
The problem:
Even though the form passes validation, and a redirect occurs, the record is not actually added to the articles table.
Laravel throws the error General error: 1366 Incorrect integer value: 'on' for column 'featured' at row 1.
What is my mistake?
When dealing with checkboxes you need to know that;
a) you cannot use required since the checkbox is not sent with the form data if it is unchecked.
b) the default value is 'on' but actually, the presence of the field in the data means it was checked so what I normally do is like;
'featured' => $request->has('featured')
this will return a boolean true or false, suitable for storing in your db
So you could write
// Turn the 'featured' field value into a tiny integer
//$fields['featured'] = $request->get('featured') == 'on' ? 1 : 0;
// If no image is uploaded, use default.jpg
$fields['image'] = $request->get('image') == '' ? 'default.jpg' : $imageName;
// Data to be added
$form_data = [
'user_id' => Auth::user()->id,
'category_id' => $fields['category_id'],
'title' => $fields['title'],
'slug' => Str::slug($fields['title'], '-'),
'short_description' => $fields['short_description'],
'content' => $fields['content'],
'featured' => $request->has('featured'),
'image' => $fields['image']
];
Your column is defined as a tinyInteger that can only accept a single digit, but you seem to be passing a string on to it.
$table->tinyInteger('featured')->default('0');
General error: 1366 Incorrect integer value: 'on' for column
'featured' at row 1
Try setting the value of your checkbox to 1.
<input class="mt-1" type="checkbox" name="featured" id="featured" value="1">
Edit, make sure to add boolean to your validation rules after adding the value to your input element.
private $rules = [
'category_id' => 'required|exists:article_categories,id',
'title' => 'required|string|max:255',
'short_description' => 'required|string|max:255',
'image' => 'image|mimes:jpeg,png,jpg|max:2048',
'content' => 'required|string',
'featured' => 'required|boolean'
];
In case it helps anyone, here is the working code for creating an article:
The routes:
// Article routes
Route::group(['prefix' => 'articles'], function() {
Route::get('/', [ArticleController::class, 'index'])->name('dashboard.articles');
Route::get('/new', [ArticleController::class, 'create'])->name('dashboard.articles.new');
Route::post('/add', [ArticleController::class, 'save'])->name('dashboard.articles.add');
});
In the controller:
namespace App\Http\Controllers\Dashboard;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Models\ArticleCategory;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
private $rules = [
'category_id' => 'required|exists:article_categories,id',
'title' => 'required|string|max:255',
'short_description' => 'required|string|max:255',
'image' => 'image|mimes:jpeg,png,jpg|max:2048',
'content' => 'required|string'
];
private $messages = [
'category_id.required' => 'Please pick a category for the article',
'title.required' => 'Please provide a title for the article',
'short_description.required' => 'The article needs a short description',
'content.required' => 'Please add content'
];
public function categories() {
return ArticleCategory::all();
}
public function index() {
$articles = Article::orderBy('id', 'desc')->paginate(10);
return view('dashboard/articles',
['articles' => $articles]
);
}
public function create() {
// Load the view and populate the form with categories
return view('dashboard/add-article',
['categories' => $this->categories()]
);
}
public function save(Request $request) {
// Validate form (with custom messages)
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors())->withInput();
}
$fields = $validator->validated();
// Upload article image
$current_user = Auth::user();
if (isset($request->image)) {
$imageName = md5(time()) . $current_user->id . '.' . $request->image->extension();
$request->image->move(public_path('images/articles'), $imageName);
}
// Turn the 'featured' field value into a tiny integer
$fields['featured'] = $request->get('featured') == 'on' ? 1 : 0;
// If no image is uploaded, use default.jpg
$fields['image'] = $request->get('image') == '' ? 'default.jpg' : $imageName;
// Data to be added
$form_data = [
'user_id' => Auth::user()->id,
'category_id' => $fields['category_id'],
'title' => $fields['title'],
'slug' => Str::slug($fields['title'], '-'),
'short_description' => $fields['short_description'],
'content' => $fields['content'],
'featured' => $fields['featured'],
'image' => $fields['image']
];
// Insert data in the 'articles' table
$query = Article::create($form_data);
if ($query) {
return redirect()->route('dashboard.articles')->with('success', 'Article added');
} else {
return redirect()->back()->with('error', 'Adding article failed');
}
}
}
I'm working on a contact form with Codeigniter 4 and SQL Database. The form will submit through the button, so whenever it is clicked, it is validating, and if all of the fields are filled out, it has no trouble displaying the message and save the information to the database, but when the fields are empty, it does not display the error message, and it continues to state that the file does not exist.
Well, I'm stuck with the error message now. I'm not sure what wrong with the code. Am I missing something?
Can anybody help me with this? I appreciate all the help I can get.
Below are my codes:
App/config/Routes.php
$routes->get('contact', 'Contact::contact');
$routes->post('contact/save', 'Contact::save');
App/Controller/Contact.php
<?php
namespace App\Controllers;
use App\Models\ContactModel;
class Contact extends BaseController
{
public function __construct()
{
helper(['url', 'form']);
}
//CONTACT PAGE
public function contact()
{
$data = [
'meta_title' => 'Contact | MFD',
];
return view('page_templates/contact', $data);
}
//SAVE
public function save()
{
$validation = $this->validate([
'name' => [
'rules' => 'required',
'errors' => [
'required' => 'Your full name is required'
]
],
'email' => [
'rules' => 'required|valid_email|is_unique[users.email]',
'errors' => [
'required' => 'Email is required',
'valid_email' => 'You must enter a valid email',
]
],
'title' => [
'rules' => 'required',
'errors' => [
'required' => 'Title is required',
]
],
'content' => [
'rules' => 'required',
'errors' => [
'required' => 'Content is required',
]
],
]);
if (!$validation) {
return view('contact', ['validation' => $this->validator]);
} else {
// Let's Register user into db
$name = $this->request->getPost('name');
$email = $this->request->getPost('email');
$title = $this->request->getPost('title');
$content = $this->request->getPost('content');
$values = [
'name' => $name,
'email' => $email,
'title' => $title,
'content' => $content,
];
$contactModel = new ContactModel();
$query = $contactModel->insert($values);
if ($query) {
return redirect()->to('contact')->with('success', 'Your message are successful sent');
} else {
return redirect()->to('contact')->with('fail', 'Something went wrong');
}
}
}
}
App/Views/page_templates/contact.php
<form action="<?= base_url('contact/save'); ?>" method="post" role="form" class="php-email-form">
<?= csrf_field(); ?>
<?php if (!empty(session()->getFlashdata('error'))) : ?>
<div class="alert alert-danger"><?= session()->getFlashdata('error'); ?></div>
<?php endif ?>
<?php if (!empty(session()->getFlashdata('success'))) : ?>
<div class="alert alert-success"><?= session()->getFlashdata('success'); ?></div>
<?php endif ?>
<div class="row">
<div class="col-md-6 form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" value="<?= set_value('name'); ?>">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'name') : '' ?></span>
</div>
<div class="col-md-6 form-group mt-3 mt-md-0">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" value="<?= set_value('email'); ?>">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'email') : '' ?></span>
</div>
</div>
<div class="form-group mt-3">
<input type="text" class="form-control" name="title" id="title" placeholder="Title">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'title') : '' ?></span>
</div>
<div class="form-group mt-3">
<textarea class="form-control" name="content" rows="5" wrap="hard" placeholder="Message"></textarea>
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'content') : '' ?></span>
</div>
<div style="height: 10px;"></div>
<div class="text-left button">
<button type="submit" name="submit">Send Message</button>
</div>
<div style="height: 10px;"></div>
</form>
Okay, I have fixed the problem with my code. It's really is a small mistake or I could say careless mistake haha feel so dumb right now but all that I need to change and make it work.
Is this part: return view('contact', ['validation' => $this->validator]);
Instead of doing it like that I actually miss placing the file to call the view page properly so the code I only added my the folder name that is
page_templates
and if I include it in the code it should look like this:-
return view('page_template/contact', ['validation' => $this->validator]);
and it works after that haha.
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',]);
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.