populate select in Laravel form with value from model - php

I made form with select. Here is code:
{!! Form::model($customerAccount, ['route' => ['admin.customerAccount.update',
$customerAccount->id_customer_account], 'method' => 'PUT', 'class' => 'form-horizontal']) !!}
...
<div class="#if ($errors->has('cacct_active'))has-error has-feedback #endif form-group">
{!! Form::label('cacct_active', 'Status: *',['class' => 'col-md-4 control-label']) !!}
<div class="col-md-8">
{!! Form::select('cacct_active',$customerAccount->getStatusList(),null,['class'=>
'form-control', 'required' => 'required', 'autofocus' => 'none']) !!}
</div>
</div>
I have options loaded by $customerAccount->getStatusList() and want default selected option to be value from my object. But it does not happen, selected is just the first one from options.
EDIT
the problem was I was using 'true, false' values in db and then '0, 1' values in 'statusList' parameter - the easiest way to solve this was to use mutator 'casts' in model for this field

Related

laravel form submit uses wrong controller method

Im trying to use a form to submit reviews for products but I believe the submit button uses the incorrect controller store method. I have a controller for products and one for reviews. The products store works correctly and I can see the database being populated once submitted however when I go to submit a review for a product it will throw the custom error messages from the product store form. If I change the reviews form::open to the products form::open it will throw an error: The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Products form (works properly)
{!! Form::open(['action' => 'App\Http\Controllers\ProductsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
... labels and text ...
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
Reviews form
<div>
<p>Write a review</p>
<!-- submit review form -->
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
<div class="form-group">
{{ Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Write your message']) }}
</div>
<div class="form-group">
{{ Form::label('rating', 'Rating') }}
{{ Form::select('rating', ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'], '1') }}
</div>
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
</div>
ReviewsController store
public function store(Request $request, $id)
{
$this->validate($request, ['description' => 'nullable',
'rating' => 'nullable',
]);
$review = new Review;
$review->rating = $request->input('rating');
$review->reviewerid = auth()->user()->id;
$review->productid = $id;
$review->description = $request->input('description');
$review->save();
return redirect('/products/$id')->with('success', 'Review submitted');
}
Web.php file
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#abouts');
Route::get('/cart', 'App\Http\Controllers\PagesController#cart');
Route::get('/checkout', 'App\Http\Controllers\PagesController#checkout');
Route::get('/dashboard', 'App\Http\Controllers\PagesController#services');
Route::get('/categories/{Category}', 'App\Http\Controllers\PagesController#category');
Route::resource('reviews', 'App\Http\Controllers\ReviewsController');
Route::resource('products', 'App\Http\Controllers\ProductsController');
Auth::routes();
The error is because of this line:
Form::hidden('_method', 'PUT')
You're telling laravel to use put method. Delete it and it will work fine.
For your form action, I think you have type in this line:
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
Change it to :
{!! Form::open(['action' => 'App\Http\Controllers\ReviewsController#store']) !!}

Laravel multi-select form fields reset on request error

I've got a form on a Laravel 5.2 application that uses form request validation. When I submit a request and the validator rejects it it goes back to the form and displays the validator's errors. All the user inputs are still there except for the fields that have multi-select, they are cleared. I don't want them cleared, I want them to display what the user had intended.
I'm having a hell of a time trouble shooting this because my localhost version works fine. This problem only occurs on the production version of the application.
Here is the issue request:
class IssueRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$rules = [
'do' => 'required|date_format:"F d, Y"',
'issue_advocate' => 'required',
'issue_type_list' => 'required',
'file_stale_date' => 'date_format:"F d, Y"',
'level_of_service_list' => 'required_with:staff_hours',
'issue_outcome_list' => 'required_with:staff_hours',
'staff_hours' => 'numeric',
'date_closed' => 'date_format:"F d, Y"'
];
return $rules;
}
public function messages(){
return [
'issue_type_list.required' => 'Please select at least one issue type.',
'do.required' => 'The date opened field is required.',
'date_format' => 'The :attribute needs to be in the format "January 1, 2000".',
'issue_outcome_list.required_with' => 'Please select at least one outcome before entering staff hours (this marks the issue to be closed)',
'level_of_service_list.required_with' => 'Please select at least one level of service before entering staff hours (this marks the issue to be closed)',
];
}
}
Here is an excerpt of the form. The fields of concern are the issue_type_list, level_of_service_list and issue_outcome_list:
{!! Form::model($issue, array('method' => 'PATCH', 'url' => 'issues/'.$issue->id)) !!}
#include('issues.addIssueForm')
<div class="form-group">
{!! Form::label('issue_type_list[]', "Issue Type") !!}
{!! Form::select('issue_type_list[]', $issuetypes, null, ['multiple' => 'multiple', 'class' => 'form-control multi-select', 'id' => 'issue_type']) !!}
#if($errors->first('issue_type_list'))
<div class="error-item alert alert-danger">{{ $errors->first('issue_type_list') }}</div>
#endif
</div>
{!! Form::hidden('client_id',Input::get('client_id')) !!}
<div class="form-group">
{!! Form::label('level_of_service_list[]', "Level of Service") !!}
{!! Form::select('level_of_service_list[]', $level_of_service, null, ['multiple' => 'multiple', 'class' => 'form-control multi-select', 'id' => 'level_of_service']) !!}
</div>
#include('issues.referralForm')
<div class="form-group">
{!! Form::label('issue_outcome_list[]', "Outcome") !!}
{!! Form::select('issue_outcome_list[]', $outcome, null, ['multiple' => 'multiple', 'class' => 'form-control ']) !!}
</div>
<div class="form-group">
{!! Form::label('staff_hours', "Staff hours") !!}
{!! Form::input('number', 'staff_hours', null, ['class' => 'form-control', 'min'=>'0', 'step' => '0.1']) !!}
</div>
<div class="form-group">
{!! Form::submit('Edit Issue', ['class' => 'btn btn-primary form-control']) !!}
Cancel
</div>
{!! Form::close() !!}

No records selected in combobox after validation

I use Laravel Collective for my forms,
I using a combobox for show list of roles.
this is my code:
{!! Form::model($user, ['route' => ['admin.users', $user->id], 'method' => 'put', 'class' => 'form-horizontal', 'enctype' => 'multipart/form-data']) !!}
{!! Form::select('roles[]', $r_list, null, ['class' => 'form-control', 'multiple' => true]) !!}
{!! Form::close() !!}
When the page load for first time, all values will select witouht any problem, but after returning to form with validation no items selected in list.
what should I do?

laravel error exception in formbuilder.php undefined offset:1error

Am new in php laravel and am getting the following error when displaying a page that should be having a form
ErrorException in FormBuilder.php line 525: Undefined offset: 1 (View: E:\mysite\mysite\resources\views\predictions\create.blade.php)
Here is the form code:
#extends('layouts.master')
#section('content')
<h2>Create Predictions</h2>
{!! Form::open(array('route' => 'predictions.store')) !!}
<div class="form-group">
{!! Form::label('title')!!}
{!! Form::text('title',null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('body')!!}
{!! Form::textarea('body',null, array('class' => 'form-control', 'size' => '50*3')) !!}
</div>
{!! Form::token() !!}
{!! Form::submit(null, array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
Here is the formbuilder code with the exception:
protected function setQuickTextAreaSize($options)
{
$segments = explode('x', $options['size']);
return array_merge($options, ['cols' => $segments[0], 'rows' =>
$segments[1]]);
}
Thanks in advance
There is no size attribute for textarea input tag. Remove 'size' => '50*3' from below code
{!! Form::textarea('body',null, array('class' => 'form-control', 'size' => '50*3')) !!}
Use rows and cols attribute instead. Since I guess you're trying to set 3 rows and 50 columns as your textarea box size your code may like this:
{!! Form::textarea('body',null, array('class' => 'form-control', 'rows' => '3', 'cols' => '50')) !!}

how to make a functionality in a layout using laravel 5

Ok, so I have a partial contact form in the layout, and I'm trying to get the inputted data to pass to the full contact view form instead of submitting request via clicking the submit button. In my partial, it has name, email, and phone input. I want the info to populate the appropriate input and the remaining inputs in the full contact form and the rest to be blank waiting for users to input them. Then naturally on submit it sends out. the full contact form is already working, I just need to get this partial on the layout to work. The problem is it's redirecting it to a view that's a get method. I myself don't like this idea, but it's for my job and this is what they want. I would preferr to not have to make another view. This is what i have so far
the layout form:
{!! Form::open(array('url' => 'contact_index')) !!}
<div class="form-group">
{!! Form::label('Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => '', 'size' => '25']) !!}
</div>
<div class="form-group">
{!! Form::label('Email:') !!}
{!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => '', 'size' => '25']) !!}
</div>
<div class="form-group">
{!! Form::label('Phone:') !!}
{!! Form::text('phone', null, ['class' => 'form-control', 'placeholder' => '', 'size' => '25']) !!}
</div>
<br/>
<div class="form_group">
{!! Form::submit('Submit', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
this is the controller it redirects to
public function Index()
{
$email = Input::get('email');
$name = Input::get('name');
$phone = Input::get('phone');
session_start();
$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
$_SESSION['phone'] = $phone;
return View('contact_views.Index',compact('email','name','phone'));
}
This is the view of the full contact:
{!! Form::open(array('url' => 'Contact')) !!}
<div class="form-group">
{!! Form::label('Email:') !!}
{!! Form::email('email', null, ['class' => 'form-control',
'placeholder' => '', value =>'$_SESSION['email']', size' => '25']) !!}
</div>
<div class="form-group">
{!! Form::label('Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => '',
value =>'$_SESSION['name']', 'size' => '25']) !!}
</div>
<div class="form-group">
{!! Form::label('Phone:') !!}
{!! Form::text('phone', null, ['class' => 'form-control', 'placeholder' => '',
value =>'$_SESSION['phone']', 'size' => '25']) !!}
</div>
<div class="form-group">
{!! Form::label('Subject:') !!}
{!! Form::text('subject', null, ['class' => 'form-control', 'placeholder' => '',
value =>'', 'size' => '25']) !!}
</div>
<div class="form-group">
{!! Form::label('Message:') !!}
{!! Form::textarea('message', null, ['class' => 'form-control', 'placeholder' => '',
value =>'', 'size' => '25x12']) !!}
</div>
<br/>
<div class="form_group">
{!! Form::submit('Submit', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
As of now this is the error I'm getting FatalErrorException in b6da938076cfb151c583150cb7d0dec6 line 51:
syntax error, unexpected 'email' (T_STRING), expecting ']' .
You have an extra single quote on the line next to the "size" variable
'placeholder' => '', value =>'$_SESSION['email']', size' => '25']) !!}

Categories