Text Area and Null, Laravel Collective - laravel

I've been using Laravel Collective for my forms and I seem to have encountered an issue with textareas. One that won't let me update null textarea fields with the same code I would use for a text field. I think the issue is with 'null' as it allows me to change the field if the textarea has text loaded. Does anyone know how to fix this so I can change null fields with textareas?
{!! Form::label ('otherinfo', 'Other information:') !!}
{!! Form::textarea ('otherinfo', null, array('class' => 'form-control', 'required' => '', 'maxlength' =>'1500') ) !!}

Your example should work fine. Make sure you update your Controller to accept and save the value that is present in $request->input('otherinfo').
<?php
$otherinfo = 'Hello World';
?>
<div class="form-group">
{!! Form::label('otherinfo', 'Other information:') !!}
{!! Form::textarea('otherinfo', $otherinfo, ['class' => 'form-control', 'size' => '50x3']) !!}
</div>

Related

Unsupported operand types (View: C:\Users\10User\fyp2.2\resources\views\route\editRoute.blade.php)

Routes
Route::get('/editRoute/{id?}',['uses'=>'RouteController#edit' , 'as' =>'route.editRoute']);
Edit View blade
{!! Form::select('driver_id', ['$driver_id' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Edit Controller
$routes = Route::find($id);
return view('route.editRoute', compact('routes'));
You can change that in one of two ways:
Add placeholder:
{!! Form::select('driver_id', $drivers, null, ['class' => 'form-control', 'placeholder'=>'---Select Driver---']) !!}
Change current select list:
{!! Form::select('driver_id', ['' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Both option will work but first is my preferred :)

get the request values in Laravel

I wish to make search query by datepicker and select field.
How could I get the requests values from below view file to controller?
Where could I modify in the code? thanks.
index.blade.php
<div class="form-group col-sm-6">
{!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/pdfs/job_finished_search'))) !!}
{!! Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) !!}
{!! Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) !!}
{!! Form::select('customer_name', $jobs->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
{!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!}
{!! Form::close() !!}
</div>
Controller.php
public function job_finished_search(Request $request, Job $jobs)
{
$jobs = Job::onlyTrashed()
->whereBetween('created_at', array(
(Carbon::parse($request->input('datepicker_from'))->startOfDay()),
(Carbon::parse($request->input('datepicker_to'))->endOfDay())))
->where('customer_name', 'like', '%'.$request->customer_name.'%')
->orderBy('deleted_at', 'desc')
->paginate(15);
if (empty($jobs)){
Flash::error('Search result not found');
}
return view('pdfs.index', ['jobs' => $jobs]);
}
There are multiple way to get the request data e.g to get datepicker_from value you can use any of the below
$request->datepicker_from
$request->input('datepicker_from')
$request->get('datepicker_from')
choose the one you like the most
refer to https://laravel.com/docs/5.5/requests
To get request values you can use the get method, try:
$customer = $request->get('customer_name','default_value');
To get request values, you can set an object like $input= $request->all(). Then you can make use of the object which is an array to get at specific fields, e.g to access your date picker, you can write $input['datepicker_from']. You need to place $input= $request->all() before you declare the $jobs object in your code.

Laravel validation with input field as array "title[]" throws always the same error

No matter which validation rule i brake as long as i have an array notation in input name like this
<div class="form-group">
{!! Form::label('titile', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
</div>
i get this error:
I have tried to use simple plain html input like this
<input type="text" name="title[]" />
and even like this
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required']) !!}
But nothing works.
Only if i make the input field without array notation [] the validation works properly...
my validation rule is this
$this->validate($request, [
'title' => 'required|min:2',
]);
I don't know what else to do, if anyone had similar problem please help.
UPDATE:
i have tried it like this now with only one form input:
<div class="form-group">
{!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required', 'placeholder' => 'z.B. Deutscher Filmpreis']) !!}
</div>
-
public function rules()
{
$rules = [
];
foreach($this->get('title') as $key => $val)
{
$rules['title.'.$key] = 'numeric';
}
return $rules;
}
Write your validation in individual request file. This ('title' => 'required|min:2') validation does not work for array input. Try this technique for dynamic field validation.
public function rules()
{
$rules = [
];
foreach($this->request->get('title') as $key => $val)
{
$rules['title.'.$key] = 'required|min:2';
}
return $rules;
}
Very Good example at laravel news site.
https://laravel-news.com/2015/11/laravel-5-2-a-look-at-whats-coming/
OK i finally solved this. In Laravel 5.3 something is changed and you can't put empty array brackets for field name.
You must declare indices inside...for example:
this doesn't work
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
but this works
{!! Form::text('title[0]', null, ['class' => 'form-control', 'required']) !!}
UPDATE
So after solving this now i know that if you put empty [ ] brackets this will work if you have multiple input fields with same name...but if you put empty brackets and you have an option to add new fields dynamically like i did...then that single field with empty array brackets will fail because you actually don't have an array...and you must put [0] some indices inside...
and then it works

Shorthand IF within Laravel HTML Form

I have a form input set up using the HTML facade:
{!! Form::text('name', NULL, ['class' => 'form-control ($errors->has("name") ? " has-error" : "")', 'placeholder' => 'Enter your name'] ) !!}
As you can I have put the $error->has inside the class but it is just printing the if statement as I know it would.
Is there anyway I can do what I am trying but keep on using the Form facade?
Try to put the if statement outside the single quotes:
{!! Form::text('name', NULL, ['class' => 'form-control'.($errors->has("name") ? " has-error" : "").'', 'placeholder' => 'Enter your name'] ) !!}

Laravel Form Model binding not showing data

I am trying to build a site and I get to the point where I would like to exploit the Form model binding
I set in the boot method of the RouteServiceProvider the binding between 'material' and 'App\Material' class
This is the creation Form:
{!! Form::open(array('url'=>'material', 'files' => true, 'class' => 'form-horizontal')) !!}
#include('admin_panel.partials.material_form',['submitButtonText' => 'Add'])
{!! Form::close() !!}
This is the include file:
{!! Form::label('title','Title',array('class' => 'control-label col-sm-2')) !!}
{!! Form::text('title','',array('class' => 'form-control')) !!}
{!! Form::label('published_at', 'Ready on:') !!}
{!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
{!! Form::label('file','Choose the material: ',array('class' => 'col-sm-2')) !!}
{!! Form::file('file') !!}
{!! Form::submit($submitButtonText, array('class' => 'btn btn-warning')) !!}
The operation of creating the material and save it to the db works perfectly.
When i want to edit a material before calling the edit view in the edit method of the MaterialController I use dd($material) to check if it is the correct object. The attributes (title, published_at,...) in the object on the screen are the correct one, so I am sure that the object I am passing to the edit view is the right one.
When i call the Edit View I print {!! $material->title !!} before the Form and the string is correct. This is the Form:
{!! Form::model($material, array('method' => 'PATCH', 'route'=>array('material.update', $material), 'files' => true, 'class' => 'form-horizontal')) !!}
#include('admin_panel.partials.material_form',['submitButtonText' => 'Update']);
{!! Form::close() !!}
The problem is that i don't see anything in the fields of the form...
I don't know where I made a mistake.
Thank you.
Well I made a stupid mistake.
In the Form::text (and I think also the other fields) of the include form I forced the second argument (the text displaying in the input text form to be empty instead of NULL, thus overriding the value of the bind model.
my mistake.

Resources