1.How can i get correct routing into form open phrase.
2.I wanted to generate the URL: dgrs/2014-31-01.
form in only view file: dgrs/show.blade.php
{{ Form::open(array('action'=>'DgrsController#ddgr')) }}
Select Date:
{{ Form::input('date', 'dgrdate', $dt, array('class' => 'input-md')) }}
{{ Form::submit('View', array('class'=>'btn btn-primary')) }}
{{ Form::close() }}
routes.php
Route::match(array('GET', 'POST'), 'dgrs/(:date)', ['as'=>'ddaily', 'uses'=>'DgrsController#ddgr']);
DgrsController.php
public function ddgr($date)
{
$dt=isset($date) ? $date : date("Y-m-d"); //date selection from user
...
return View::make('dgrs.show', compact('dfinal', 'dt'));
//dfinal is db query and dt is selected date back to show.blade.php
}
view is the form file: dgrs/show.blade.php
Please advise.
Here is a solution with jQuery
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
{{ Form::open(array('action'=>'DgrsController#ddgr' , 'id' => 'myFrm')) }}
Select Date:
{{ Form::input('date', 'dgrdate', $dt, array('class' => 'input-md' , 'id' => 'txtDate')) }}
{{ Form::submit('View', array('class'=>'btn btn-primary')) }}
{{ Form::close() }}
<script>
$(document).ready(function(){
$("#txtDate").change(function(){
var baseUrl = "{{ URL::to("dgrs") }}" + "/" + $(this).val();
$("#myFrm").attr("action",baseUrl);
});
});
</script>
so what i have done is when ever date changes form action url is changes according to that date.
Related
This is my code :
Route:
Route::get('/editposts/{id}', function ($id) {
$showpost = Posts::where('id', $id)->get();
return view('editposts', compact('showpost'));
});
Route::post('/editposts', array('uses'=>'PostController#Update'));
Controller :
public function Update($id)
{
$Posts = Posts::find($id);
$Posts->Title = 10;
$Posts->Content = 10;
$Posts->save();
//return Redirect()->back(); Input::get('Title')
}
and View:
#foreach($showpost as $showpost)
<h1>Edit Posts :</h1>
{{ Form::open(array('url'=>'editposts', 'method'=>'post')) }}
Title : {{ Form::text('Title', $showpost->Title) }} <br> Content : {{ Form::text('Content', $showpost->Content ) }} <br> {{ Form::submit('Update') }}
{{ Form::close() }}
#endforeach
but when I want to Update my data i receive an error :
http://localhost:8000/editposts/1
Missing argument 1 for App\Http\Controllers\PostController::Update()
You need to change route:
Route::post('editposts/{id}', 'PostController#Update');
Then the form to:
{{ Form::open(['url' => 'editposts/' . $showpost->id, 'method'=>'post']) }}
Change your post route to:
Route::post('/editposts/{id}', 'PostController#Update');
Done!
Correct the route,specify a parameter
Route::post('editposts/{id}', 'PostController#Update');
Pass the post'id as paramater
{{ Form::open(array('url'=>'editposts/'.$post->id, 'method'=>'post')) }}
Title : {{ Form::text('Title', $showpost->Title) }} <br> Content : {{ Form::text('Content', $showpost->Content ) }} <br> {{
Form::submit('Update') }}
{{ Form::close() }}
Notice $post->id
First declare your route:
Route::post('/editposts/{id}', array('uses'=>'PostController#Update'));
Then update your form url:
{{ Form::open(['url' => url()->action('PostController#Update', [ "id" => $showpost->id ]), 'method'=>'post']) }}
This is assuming your model's id column is id
(Optional) You can also use implicit model binding :
public function Update(Posts $id) {
//No need to find it Laravel will do that
$id->Title = 10;
$id->Content = 10;
$id->save();
}
i have this form in view:
{{ Form::open(array('action' => 'StudentrecordController#viewSRS')) }}
<span><strong>Select School Year & Quarter</strong></span>
<div class="form-group">
{{ Form::select('sy', [null=> 'Select School Year'] + $schoolYearID , Input::old('modules'), array('class'=>'form-control') ) }}
</div>
<div class="form-group">
{{ Form::select('sq', [null=> 'Select Quarter'] + $schoolQuarterID , Input::old('modules'), array('class'=>'form-control') ) }}
</div>
{{ Form::submit('Sort', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
my route for this is
Route::get('sortsRec', 'StudentrecordController#viewSRS');
when i clicked the submit button it gives out a method not allowed exception.i think the form is sending out a post method but the route accepts get. how can i address this? any idea what i can do?
By default, a POST method will be assumed; however, you are free to specify another method:
{{ Form::open(['method' => 'get', 'action' => 'StudentrecordController#viewSRS']) }}
From the docs.
In the blade view file, I have something like this:
{{ Form::text('contact_name', null, ['class' => 'form-control']) }}
#if ($errors->has('contact_name'))
<div class="error-block">{{ $errors->first('contact_name') }}</div>
#endif
{{ Form::text('contact_email', null, ['class' => 'form-control']) }}
#if ($errors->has('contact_email'))
<div class="error-block">{{ $errors->first('contact_email') }}</div>
#endif
When user press submit, it will check inputs validation in the controller. However, if there is an error with the validation, it will then redirect back to a form and populate it with error messages {{ $errors->first() }}
Is there a way to exclude {{ $errors->first() }} in the view file and still show error messages if validation failed? So combine Form::text and $errors->hasinto one function or something like that?
Use a Form Macro to do this
Form::macro('myText', function($field)
{
$string = Form::text($field, null, ['class' => 'form-control']);
if ($errors->has($field)) {
$string .= $errors->first($field);
}
return $string;
});
Then in your view
{{ Form::myText('contact_email') }}
One of the coolest Laravel feature is, Laravel pre-filled the form fields if validation error occurred. However, if a page contain more than one form, and form fields have same name, Laravel pre-filling all forms fields.
For example:
I have a page where i have two forms to create new users or whatever.
<h1>Create user1</h2>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
Controller
class UsersController extends BaseController
{
public function store()
{
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails()) {
return Redirect::back()->withInput()->withErrors($validation);
}
}
}
As i didn't fill up the email, Laravel will throw validation error and pre-filling the forms as following:
How to tell Laravel that do not fill-up the second form?
There's no Laravel way of doing this, but you can use HTML basic form arrays to make it work. You need to understand that you have to identify your forms and fields so Laravel knows exactly where the data came from and where to send it back to. If all your fields have the same name how could it possibly know?
This is a proof of concept that will work straight from your routes.php file.
As I did it all and tested here before posting the answer I used Route::get() and Route::post(), to not have to create a controller and a view just to test something I will not use. While developing this you will have to put this logic in a controller and in a view, where I think they are alredy in.
To test it the way it is, you just have to point your browser to the following routes:
http://yourserver/form
and when you push a button it will automatically POST tho the route:
http://yourserver/post
I'm basically giving all forms a number and giving the buttons the number that we will usin in Laravel to get the form data and validate it.
Route::get('form', function()
{
return Form::open(array('url' => URL::to('post'))).
Form::text('form[1][name]', null).
Form::email('form[1][email]', null).
'<button type="submit" name="button" value="1">submit</button>'.
Form::close().
Form::open(array('url' => URL::to('post'))).
Form::text('form[2][name]', null).
Form::email('form[2][email]', null).
'<button type="submit" name="button" value="2">submit</button>'.
Form::close();
});
And here we get the data, select the form and pass all of it to the validator:
Route::post('post', function()
{
$input = Input::all();
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make($input['form'][$input['button']], $rules);
return Redirect::back()->withInput();
});
This is how you use it in a Blade view, now using 3 forms instead of 2 and you can have as many forms as you need:
<h1>Create user1</h2>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[1][name]', null) }}
{{ Form::email('form[1][email]', null) }}
<button type="submit" name="button" value="1">submit</button>
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[2][name]', null) }}
{{ Form::email('form[2][email]', null) }}
<button type="submit" name="button" value="2">submit</button>
{{ Form::close() }}
</h1>Create user3</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[3][name]', null) }}
{{ Form::email('form[3][email]', null) }}
<button type="submit" name="button" value="3">submit</button>
{{ Form::close() }}
And you can even use a loop to create 100 forms in blade:
#for ($i=1; $i <= 100; $i++)
User {{$i}}
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text("form[$i][name]", null) }}
{{ Form::email("form[$i][email]", null) }}
<button type="submit" name="button" value="{{$i}}">submit</button>
{{ Form::close() }}
#endfor
Use old input with $request->flash().
https://laravel.com/docs/5.2/requests#old-input
I'm trying to populate a select box based on a previous select box value in Laravel 4. Here's what I have so far:
My JS:
var url = document.location.hostname + '/cream/public/list-contacts';
var contacts;
$.ajax({
async: false,
type: 'GET',
url: url,
dataType: 'json',
success : function(data) { contacts = data; }
});
$('#account_id').change(function() {
alert(url);
label = "<label class='control-label'>Contacts</label>";
select = $("<select name='contact_id[]' id='contact_id'>");
console.log(contacts);
for(var i in contacts) {
alert(contacts[i]['account_id']);
if(contacts[i]['account_id'] == $(this).val()) {
select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>');
}
}
$('.contacts').html(select).prepend(label);
});
My list-contacts route declaration:
Route::get('list-contacts', 'ContactListController#contacts');
My contacts() method in my ContactListController:
public function contacts()
{
return Contact::select('contacts.id', 'contacts.account_id', DB::raw('concat(contacts.first_name," ",contacts.last_name) AS name'))->get()->toArray();
}
The form in my view:
{{ Form::open(array('action' => 'DelegatesController#store', 'class' => 'view-only pull-left form-inline')) }}
{{ Form::label('account_id', 'Account', array('class' => 'control-label')) }}
{{ Form::select('account_id', $accounts) }}
<div class="contacts"></div>
{{ Form::label('delegate_status_id', 'Status', array('class' => 'control-label')) }}
{{ Form::select('delegate_status_id', $delegate_statuses) }}
{{ Form::label('price', 'Price', array('class' => 'control-label')) }}
{{ Form::text('price', '', array('class' => 'input-small')) }}
{{ Form::hidden('event_id', $event->id) }}
{{ Form::submit('Add Delegate', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
EDIT: I've modified my code above. When I visit /list-contacts it gets the correct data I need, it's just not assigning that data to the contacts variable in my AJAX request in my JS? Any help would be appreciated.
Error: This is the error that is shown in my console log for the contacts variable:
file: "/Applications/MAMP/htdocs/cream/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php"
line: 290
message: ""
type: "Symfony\Component\HttpKernel\Exception\NotFoundHttpException"
I now have this working. It was to do with the generated URL in the AJAX request. I removed the document.location.hostname and hard coded the url without localhost.
Here's the working code for those interested:
My JS:
var url = '/cream/public/list-contacts';
var contacts;
$.ajax({
async: false,
type: 'GET',
url: url,
dataType: 'json',
success : function(data) { contacts = data; }
});
$('#account_id').change(function() {
select = $("<select name='contact_id' id='contact_id'>");
for(var i in contacts) {
if(contacts[i]['account_id'] == $(this).val()) {
select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>');
}
}
$('.delegates .contacts').show();
$('.delegates .contacts .controls').html(select);
});
My list-contacts route declaration:
Route::get('list-contacts', 'ContactListController#contacts');
My contacts() method in my ContactListController:
public function contacts()
{
return Contact::select('contacts.id', 'contacts.account_id', DB::raw('concat(contacts.first_name," ",contacts.last_name) AS name'))->get();
}
The form in my view:
{{ Form::open(array('action' => 'DelegatesController#store', 'class' => 'delegates pull-left form-horizontal add-delegate')) }}
<div class="control-group">
{{ Form::label('account_id', 'Account', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::select('account_id', $accounts) }}
</div>
</div>
<div class="control-group contacts">
{{ Form::label('contact_id', 'Contacts', array('class' => 'control-label')) }}
<div class="controls">
</div>
</div>
<div class="control-group">
{{ Form::label('delegate_status_id', 'Status', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::select('delegate_status_id', $delegate_statuses) }}
</div>
</div>
<div class="control-group">
{{ Form::label('price', 'Price', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::text('price', '', array('class' => 'input-small')) }}
</div>
</div>
{{ Form::hidden('event_id', $event->id) }}
{{ Form::close() }}