I want to set the placeholder value for my form element. Here is my controller:
class MyController extends Controller {
public function index(Request $request){
$start_time = $request->get('start_date');
return view('showChart',compact('start_time'));
}
}
The controller will send response to showChart.blade.php. Part of the showChart.blade.php is:
{!! Form::open(array('url' => '/', 'class' => 'form')) !!}
{!! Form::label('START DATE') !!}
{!! Form::text('start_date', null,
array('required',
'placeholder'=>{{start_time or 'default value'}}
))
!!}
{!! Form::close() !!}
It does not work as expected, it will output: , it seems that the code does not execute.
You can’t use template delimiters once inside an already-open set. You’ll have to do something like this instead:
{!! Form::text('start_date', null, ['placeholder' => empty($start_date) ? 'default value' : $start_date, 'required' ]) !!}
I'm not sure if i understand every thing but did you have a white screen ? That means a blade error.
{!! Form::text('start_date', null,
array('required',
'placeholder'=>{{start_time or 'default value'}}
))
!!}
Remove {{ }} and replace start_time by $start_time.
And about your default value, you can put it into $start_time in your controller or before in your view.
Related
I got this error when I access to router:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException No message
I tried to find out that but I couldn't fix it.
Router(web.php)
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
View(subdomains.account.pages.test.blade.php)
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Controller
namespace Ares\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
The problem is that code always use the GET method instead of POST.
How can I fix it?
EDIT: I just found cause this issue is because I don't use Laravel using php artisan serve, and I only have a virtual host in the public server, how I can solve this without using artisan serve?
FIX: (EDITED)
The issue is by forcing the trailing slash at the end of the URL.
Try This:
FormController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function store()
{
return "test";
}
}
subdomains/account/pages/test.blade.php
{!! Form::open(['action' => [ 'FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
web.php
Route::get('/test', function ()
{
return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController#store');
Output: When you use your route /test the test button will be displayed on the output page
When you click on the test button, it will be redirect to /testForm page and get the output test
You should try this:
{!! Form::open(['url' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Updated answer
Route::post('/testForm', 'FormController#store')->name('testform.store');
{!! Form::open(['route' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
Try to use code below for subdomains.account.pages.test
{!! Form::open(['action' => [ '\Ares\Http\Controllers\FormController#store' ], 'method' => 'POST']) !!}
{!! Form::submit('test') !!}
{!! Form::close(); !!}
You should remove closer from your routes,
Route::get('/test', 'FromController#index');
Route::post('/testForm', 'FormController#store');
then run,
php artisan route:clear
Then submit your form
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.
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
I can't seem to find a way to put image as submit button in blade, is there a way to do this?
{!! Form::submit('Search', array('class'=>'btn')) !!}
Form::input should do the trick:
{!! Form::input('image', 'Search', array('class'=>'btn', 'src' => '...')) !!}
You can create the submit with pure html instead of laravelcollective like that
<input type="image" src="sourse for image" width="48" height="48" alt="Submit" />
For Laravel Collective version 6.2 the solution This small example shows the sending of a variable by means of a button with an image (src='../path/to/file) using the 'submit' passing as parameters the dimensions of the button (30,30).
Note: var_to_send can have a string like $ansver - > 'Hello World' or another value and it will pass it from the controller and it will print it.
// new.blade.php
{!! Form::open([ 'action'=> 'StorageController#save', 'method' => 'POST', 'files' => true]) !!}
{!! Form::hidden('var_to_send', $answer) !!}
{!! Form::image(url('../img/A.png'), 'submit', ['width' => '30', 'height' => '30']) !!}
{!! Form::close() !!}
// StorageController.php
class StorageController extends Controller
{
public function save(Request $request)
{
return $request->input('var_to_send');
}
}
// route wep.php
Route::post('storage/create', 'StorageController#save');
I am implementing a simple controller for a mini-project of mine. For the simplicity of this question, only two views matter: the create song, and edit song views. Both of these views contain the same form fields, so I created a form partial called _form.
Since the forms have different purposes - despite having the same fields - I pass on to the partial a couple of variables to specify the value of the submit button label, and the cancel button route.
For example:
edit.blade.php:
(...)
{!! Form::model($song, ['route' => ['songs.update', $song->slug], 'method' => 'PATCH']) !!}
#include('songs._form', [
'submitButtonLabel' => 'Update',
'returnRoute' => 'song_path',
'params' => [$song->slug]
])
{!! Form::close() !!}
(...)
create.blade.php:
(...)
{!! Form::open(['route' => 'songs.store']) !!}
#include('songs._form', [
'submitButtonLabel' => 'Save',
'returnRoute' => 'songs_path'
])
{!! Form::close() !!}
(...)
And here is the _form.blade.php partial:
(...)
<div class="form-group">
{!! Form::submit($submitButtonLabel, ['class' => 'btn btn-success']) !!}
{!! link_to_route($returnRoute, 'Cancel', isset($params) ? $params : [], ['class' => 'btn btn-default', 'role' => 'button']) !!}
</div>
Now, my question is (finally):
As you can see, in the Cancel button of my form partial, I am using isset($params) ? $params : [] to default the $params variable to [] when it is not set.
Is there a better way to do this? Here, under Echoing Data After Checking For Existence, Laravel supports this alternative echo: {{ $name or 'Default' }}, but this does not work since I am trying to use it inside a {!! !!} block already...
So, is the ternary operator using the isset() function the best solution for this case? (The one I am currently using)
You can simply pass the variable $params an empty array ([]) in create.blade.php and remove the condition on your partial.
Then you can set the default value on your .blade files
As an alternative you can set a default value on your controller and send it as $params if they are not set (your slug).
Hope it helps