this problem occured recently on a website on my development server, which runs Laravel 5.2.45. I have a form which I use to pass data through a POST request on a controller. On the form there are 2 multiselects, one for the available locations and one for the selected locations. The user can pass a location from the available locations to the selected locations. The problem is that once the form submits, the selected locations do not appear anywhere on the request parameters, but the available locations appear. This was working for a long time and suddendly it stopped working.
Available locations multiselect
<div class="form-group clearfix{{ $errors->has('available') ? ' has-error' : '' }}">
{!! Form::label('available', 'Available Locations', ['class'=> 'col-xs-12 pl-0 pr-0']) !!}
<div class="col-xs-12 pl-0 pr-0">
{{Form::select('available[]', $taskgroup['available'], null, ['id'=> 'multiselect', 'class'=> 'form-control', 'size'=> 8, 'multiple' => 'multiple']) }}
</div>
#if ($errors->has('available'))
<span class="help-block">{{ $errors->first('available') }}</span>
#endif
Selected locations multiselect
<div class="form-group clearfix{{ $errors->has('locations') ? ' has-error' : '' }}">
{!! Form::label('locations', 'Selected Locations', ['class'=> 'col-xs-12 pl-0 pr-0']) !!}
<div class="col-xs-12 pl-0 pr-0">
{{Form::select('locations[]', $taskgroup['locations'], null, ['id'=> 'multiselect_to', 'class'=> 'form-control', 'size'=> 8, 'multiple' => 'multiple']) }}
</div>
#if ($errors->has('locations'))
<span class="help-block">{{ $errors->first('locations') }}</span>
#endif
The part which handles the request in the controller
$taskgroup = Taskgroup::with('locations')->findOrFail($id);
$input = Input::all();
//dd($request,$input);
unset($input['available']);
if(!empty(array_values($input['media_url'])[0]['title'])){
$media_url = Taskgroup::sortArray($input['media_url'], 'order');
$input['media_url'] = json_encode(array('media' => $media_url));
} else {
unset($input['media_url']);
}
$messages = ['between' => 'The :attribute must be between :min - :max.'];
$validation = Validator::make($input, array_merge(Taskgroup::$rules, array('locations'=>'required|between:1,'.$input['max_users'])), $messages);
The uncommented data dump of the input data
"_method" => "PUT"
"_token" => "Token Here"
"name" => "Beer"
"short_description" => "Short Description<br>"
"_wysihtml5_mode" => "1"
"long_description" => "<div>Long Description</div>"
"pin_color" => "red"
"start_date" => "2018-12-03 09:00"
"end_date" => "2019-12-31 20:00"
"media_url" => array:1
"file_names" => array:1
"delete-file-85" => "0"
"delete-file-86" => "0"
"max_users" => "32"
"max_arrival_time" => "120"
"max_completion_time" => "30"
"client_id" => "22"
"q" => ""
"available" => array:977
"files" => array:1
This is my first post on the website, so please excuse any formatting errors. Thank you in advance.
Try to use
{!!Form::select(....)!!}
Related
I want to have a add another form button that will duplicate all the fields of my form but will still remain one submit button and pass all the data of that into the controller to insert it using one single query.
Here is the create.blade.php that opens the form
#extends('encoder-dashboard.layouts.app')
#section('css')
<link rel="stylesheet" href="/css/datepicker.css">
#endsection
#section('content')
<section class="content-header">
<h1>
Add New Analysis Request
</h1>
</section>
<div class="content">
#include('adminlte-templates::common.errors')
<div class="box box-primary">
<div class="box-body">
<div class="row">
{!! Form::open(['route' => 'encoder.analysis-request.store']) !!}
#include('encoder-dashboard.analysis-request.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script src="/js/datepicker.js"></script>
<script>
$('#datepicker').datepicker({
autoclose: true
});
</script>
#endsection
And here are the fields listed in fields.blade.php
<!-- Client Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('client_id', 'Client Name:') !!}
{!! Form::select('client_id[]', $client, null, ['class' => 'form-control','required'])!!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('sample_code', 'Sample Code:') !!}
{!! Form::text('sample_code[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('sample_description', 'Description:') !!}
{!! Form::text('sample_description[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('quantity', 'Quantity:') !!}
{!! Form::number('quantity[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('analysis_requested', 'Analysis Request:') !!}
{!! Form::text('analysis_requested[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('special_instruction', 'Special Instruction:') !!}
{!! Form::text('special_instruction[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-success', 'onClick' => 'this.form.submit(); this.disabled=true; this.value="Saving…";']) !!}
Cancel
</div>
All the fields above from the form will be duplicated and pass it as an array right? How do I do that maybe using jQuery or simple vanila JS as well?
and if I pass this data it would look like these.
array:8 [▼
"_token" => "xEGtr4h0f4gim0sLvABZivvq36UNaRHWJ08PMWTI"
"client_id" => array:1 [▼
0 => "1"
]
"sample_code" => array:1 [▼
0 => "eqwewqeqewqrwereqwe"
]
"sample_description" => array:1 [▼
0 => "dasdsadsadsadasd"
]
"quantity" => array:1 [▼
0 => "232"
]
"analysis_requested" => array:1 [▼
0 => "dsadsadsadsadsad"
]
"special_instruction" => array:1 [▼
0 => "dwqewqewqewqewqeq"
]
]
This is my query below when inserting one single record.
$analysis_request = $this->analysisrequestRepository->create([
'client_id' => $input['client_id'],
'sample_code' => $input['sample_code'],
'sample_description' => $input['sample_description'],
'quantity' => $input['quantity'],
'analysis_requested' => $input['analysis_requested'],
'special_instruction' => $input['special_instruction'],
'status' => 'for_testing'
]);
$id = $analysis_request->id;
$request_actors = RequestActors::create([
'request_id' => $id,
'encoder_id' => Auth::guard('encoder')->user()->id,
'microbiologist_id' => null
]);
this is my query above for inserting one single record. I also performed another query to insert the id of the request which has a relationship to the analysis request that is inserted in the first query.
Now if I will make it to insert many I will do an array inside an array and insert it each record right? Like this.
$data = array(
array(
'client_id' => $input['client_id'],
'sample_code' => $input['sample_code'],
'sample_description' => $input['sample_description'],
'quantity' => $input['quantity'],
'analysis_requested' => $input['analysis_requested'],
'special_instruction' => $input['special_instruction'],
'status' => 'for_testing'
),
// and so on.
);
AnalysisRequest::insert($data);
But I had an error in the code above it says that Array to string conversion
Or will it be better if I do an for each?
My only goal here is to perform an insert many query depends on the entries to add.
Appreciate if someone can help.
Thanks in advance.
You are getting an array of array and you want to put (for example) into client_id one array [1]
For that reason you have "Array to String conversion".
You can do
$data = array(
array(
'client_id' => $input['client_id'][0],
'sample_code' => $input['sample_code'][0],
'sample_description' => $input['sample_description'][0],
'quantity' => $input['quantity'][0],
'analysis_requested' => $input['analysis_requested'][0],
'special_instruction' => $input['special_instruction'][0],
'status' => 'for_testing'
),
// and so on.
);
AnalysisRequest::insert($data);
I have a database users. It has an email field.
I have two input fields with names users[email] and manager.email. I want the validation of uniqueness to be on users table. Here is what have I tried it.
<div class="row">
<div class="form-group col-md-12">
<div class="col-md-8">{{ Form::input('text', 'users[email]', '', ['class' => "form-control", 'maxlength' => 255, 'id' => "userEmail", 'placeholder' => "Email Address"]) }}</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<div class="col-md-8">{{ Form::input('text', 'manager[email]', '', ['class' => "form-control", 'maxlength' => 255, 'id' => "email", 'placeholder' => "Manager Email Address"]) }}</div>
</div>
</div>
My validator is as follows:
$validationRules = ['users.email' => 'email|required|unique:users',
'manager.email' => 'email|required|unique:users',
];
So, when I validator it returns a message stating :
Unknown column 'manager.email' in 'where clause' (SQL: select count(*) as aggregate from `users` where `manager`.`email` = 'xyz#email.com')
Please guide me through this or suggest me a better option to resolve this problem.
I would imagine your issue is because you're not specifying the column name in your unique rule.
https://laravel.com/docs/5.2/validation#rule-unique (look under Specifying A Custom Column Name)
Try changing:
'manager.email' => 'email|required|unique:users'
to:
'manager.email' => 'email|required|unique:users,email'
Hope this helps!
I'm using Laravel 5.3 auth:make registration form, I have a multiple select in my form, which returns the count of the selected rows instead of array with the values.
Thats what coming back when I dd($data):
array:10 [▼
"_token" => "mBTBYC4yHsI0Ph1Hmj7p6MSstOCRM3cDkCoSZH4b"
"first_name" => "User"
"last_name" => "Family"
"class" => "Software Engineer"
"year" => "0"
"tags" => "1" -> that should be an array.
"phone" => "55555555"
"email" => "test#test.com"
"password" => "123123"
"password_confirmation" => "123123"
]
The are I would like to fix looks like this in the html:
<div class="form-group{{ $errors->has('tags') ? ' has-error' : '' }}">
{!! Form::select('tags[]', $tags , null , ['class' => 'form-control' , 'multiple' , 'id'=>'tags' , 'name' => 'tags']) !!}
#if ($errors->has('tags'))
<span class="help-block">
<strong>{{ $errors->first('tags') }}</strong>
</span>
#endif
</div>
i think the problem is that you are not give them ("tags[]") a key to save multiple data into the array. this is what I handle the tags array with a checkbox {!! Form::checkbox('tag_id[]',$tag->id, null, ['class'=>'tag_id']) !!} check the second argument, I load the value into Id inside of the tag_id[] array.
I need to bind the data to the related field using Form data binding , but the data does not showed on the fields at all.I don't know what the problem exactly that prevent the data to appear.
Controller:
public function edit($id){
$datum = RatingDatum::findorfail(1);
return view('rating.index',compact('datum'));
}
View:
<div class="plan bg-plan">
{!! Form::model($datums,['method' => 'PATCH','action'=>'RatingDataController#update'],$datums->id]) !!}
<div class="radio">
<label>
{!! Form::radio('customize_question_id')!!}{{$datums->value}}
</label>
</div>
<div class="form-group">
{!! Form::label('comment','Comment :') !!}
{!! Form::textarea('comment' ,null,['class'=>'form-control', 'rows' => 4]) !!}
{!! Form::label('reference','Reference:') !!}
{!! Form::textarea('reference',null,['class'=>'form-control', 'rows' => 1]) !!}
</div>
{!! Form::submit('Submit Data', ['class' => 'btn btn-success submit']) !!}
{!! Form::close() !!}
</div>
dd($datum);
#attributes: array:15 [▼
"id" => 1
"organisation_id" => 8
"sector_id" => 1
"country_id" => 1
"dimension_id" => 12
"question_angle_id" => 1
"customize_criteria_id" => 33
"customize_question_id" => 7591
"question_weight" => 20
"actual_score" => 75
"value" => "The company has made a formal commitment to promoting voluntary community initiatives and has set up quantitative targets in this regard."
"comment" => ""
"reference" => ""
"created_at" => "2015-12-21 11:28:38"
"updated_at" => "2015-12-21 12:22:25"
]
The data is not showing up in the form is because you are referencing the wrong variable.
You are passing $datum and in the view form, you are accessing $datums. That is the reason why your form is empty.
Solution:
Do either of the following:
Update controller method's $datum to $datums
OR
Update form model $datums to $datum
I have a form, one field is for a file named attachment, in my request the validation for this field requires it to be present when another field named requestType is set to the value of sick by using the required_if validation rule.
The issue I'm facing is that even when I upload a file for the input field in question the validation rule from the request class still gets triggered: The attachment field is required when request type is sick.
Here's my code:
Please note that the html required attribute on the attachment field is not what is causing the issue, on the page load it's set to disabled and when requestType is set to sick the disabled attribute is removed.
View
{!! Form::open(['route' => 'employee.request.store', 'class' => 'form-horizontal', 'id' => '', 'files' => 'true']) !!}
<div class="form-group {{ $errors->first('requestType', 'has-error') }}">
<label for="" class="col-sm-2 control-label"> {{ Lang::get('employee_request_contractor_create.request_type') }} *</label>
<div class="col-sm-3">
{!!
Form::select('requestType',
['' => 'Select', 'normal' => 'Normal', 'emergency' => 'Emergency', 'sick' => 'Sick'],
'',
['class' => 'form-control', 'id' => 'requestType', 'required' => 'required']
)
!!}
</div>
{!! $errors->first('requestType', '<label class="col-sm-3 control-label" for="">:message</label>') !!}
</div>
<div class="form-group {{ $errors->first('attachment', 'has-error') }}" id="attachmentFormGroup">
<label for="" class="col-sm-2 control-label"> {{ Lang::get('employee_request_contractor_create.attachment') }} <small>(Sick only)</small> </label>
<div class="col-sm-3">
<input type="file" name="attachment" id="attachment" required="required">
<label>(Please provide HR with original copy)</label>
</div>
{!! $errors->first('attachment', '<label class="col-sm-3 control-label" for="">:message</label>') !!}
</div>
<!-- other form inputs and submit button -->
{!! Form::close() !!}
Request
public function rules()
{
return [
'requestType' => 'required|max:255',
'attachment' => 'required_if:requestType,sick|mimes:pdf,jpg,png,gif,jpeg|max:512',
/* other rules */
];
}
If I remove required_if:requestType the attachment uploads just fine and if I output in my controller:
if(\Input::hasFile('attachment') echo 'true';
I will see true.
When I dd($request) in my controller store method I see the following (relevant parts):
+request: ParameterBag {#227 ▼
#parameters: array:10 [▼
"_token" => "XkQwP608M5WQ4qtHCYN0dIVETDeqzL0E5ZI99iSf"
"requestType" => "sick"
"manager" => "2"
"dateFrom" => "01-06-2015"
"dateFromHijri" => "1436-08-14"
"dateTo" => "02-06-2015"
"dateToHijri" => "1436-08-15"
"noOfDays" => "2"
"location" => "London"
"contactNumber" => "123456"
]
}
and...
+files: FileBag {#221 ▼
#parameters: array:1 [▼
"attachment" => UploadedFile {#27 ▼
-test: false
-originalName: "test_doc.pdf"
-mimeType: "application/pdf"
-size: 82584
-error: 0
}
]
}
Is the rule getting fired because attachment is not showing up along with the other request attributes?
Update: Error messages:
["errors"]=>
object(Illuminate\Support\ViewErrorBag)#178 (1) {
["bags":protected]=>
array(1) {
["default"]=>
object(Illuminate\Support\MessageBag)#179 (2) {
["messages":protected]=>
array(1) {
["attachment"]=>
array(1) {
[0]=>
string(59) "The attachment field is required when request type is sick."
}
}
["format":protected]=>
string(8) ":message"
}
}
}
Any help would be much appreciated. Thanks!
Since the input is disabled when the requestType is not set to sick, the attachment is not included in the POST request sent to server. That is why Laravel's Request validation file triggers, because it doesn't even see the value in the POST data. To solve that, just add sometimes in the validation string for the attachment filed. The code should look like this:
public function rules()
{
return [
'requestType' => 'required|max:255',
'attachment' => 'sometimes|required_if:requestType,sick|mimes:pdf,jpg,png,gif,jpeg|max:512',
/* other rules */
];
}
Here is an excerpt from the Laravel docs regarding this issue:
In some situations, you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the sometimes rule to your rule list:
$v = Validator::make($data, [
'email' => 'sometimes|required|email',
]);
In the example above, the email field will only be validated if it is present in the $data array.