Request validation with laravel 5 - validation

I have problem when trying validate an input in the form, catch an error of "unique". Although I am modifying and not inserting a new record...
This eror is The element is already in use codigotiporiesgo
Look at the code: FORM:
<div class="form-group" id='test'>
{!!Form::label('codigoTipoRiesgo', 'Código', array('class' => 'col-sm-2 control-label')) !!}
<div class="col-sm-10">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-barcode"></i>
</span>
<input type="hidden" id="token" value="{{csrf_token()}}"/>
{!!Form::text('codigoTipoRiesgo',null,['class'=>'form-control','placeholder'=>'Ingresa el código del tipo de riesgo'])!!}
{!!Form::hidden('idTipoRiesgo', null, array('id' => 'idTipoRiesgo')) !!}
</div>
</div>
</div>
and REQUEST:
"codigoTipoRiesgo" => "required|string|max:20|unique:tiporiesgo,codigoTipoRiesgo,".$this->get('idTipoRiesgo') .",idTipoRiesgo",
"nombreTipoRiesgo" => "required|string|max:80",
"origenTipoRiesgo" => "required|string",
"ClasificacionRiesgo_idClasificacionRiesgo" => "required|numeric"

I believe your issue may be with $this->get('idTipoRiesgo') and it is returning null. Try dd($this->get('idTipoRiesgo')) and see what you get.
Try querying for the model before you rules.
$model = Model::find($this->idTipoRiesgo);
then your rule would be pretty much what you posted, but now you should have the $model->id to work with.
'field_name' => 'unique:table_name,column_name,' . $model->id . ',primary_key_column'

Related

Laravel 8.x PDF file validation is not working correctly

I am working on Laravel 8.x. My task is to validate .PDF file on server side. Everyting working fine but when I upload a valid PDF file it is not validating and returning with an error.
My source code is following. Please correct my mistakes if you find
Thanks
blade file
<form class="w-100" id="addnotes" method="post" enctype="multipart/form-data" action="{{ route('upload-member-reports') }}">
{{ csrf_field() }}
<div class="modal-header">
<h5 class="modal-title" name="report-file" id="exampleModalLongTitle">Upload Reports</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<label class="file ">
<input type="file" name="reportfile" id="reportfile" aria-label="File browser example">
<span class="file-custom"></span>
</label>
#if($errors->has('reportfile'))
<div class="error text-danger">{{ $errors->first('reportfile') }}</div>
#endif
<input type="date" name="reportdate" class="form-control mt-20px" value="{{ old('reportdate') }}" placeholder="Select Date">
<!-- <textarea type="textarea" rows="4" cols="50" class="form-control" placeholder="Enter Your Notes..."></textarea> -->
#if($errors->has('reportdate'))
<div class="error text-danger mt-5px">{{ $errors->first('reportdate') }}</div>
#endif
<input type="hidden" name="manager_id" value="{{ Auth::user()->id }}">
<input type="hidden" name="member_id" value="{{ $id }}">
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-orange" value="Submit">
</div>
</form>
controller file
public function uploadMemberReports( Request $request ){
# Validation Rules
$rules = [
'reportdate'=>'required',
'reportfile' => 'required|mimes:pdf',
];
$messages = [
'reportdate.required' =>'Date is required.',
'reportfile.required' => 'File is required.',
'reportfile.mimes' => 'Only PDF files are allowed.',
];
$validator = Validator::make( $request->all(), $rules, $messages);
if ( $validator->fails() ) {
# if validations fails redirect back with errors
return redirect()->back()->withErrors($validator)->withInput();
} else {
# next action
}
}
When I try with valid PDF file it returning with error like following
The reportfile failed to upload.
Edit:
The request array response
Array
(
[_token] => yaX0ohRjl6tR298Zd9WeSLcgxcoVQ9nPx3K5gO4S
[reportdate] => 2021-01-12
[manager_id] => 2
[member_id] => 4
[reportfile] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => labreports_12.pdf
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/octet-stream
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1
[hashName:protected] =>
[pathName:SplFileInfo:private] =>
[fileName:SplFileInfo:private] =>
)
)
This can have several causes. But Frontend looks good at first glance. It could be a server problem. For example FileSize, MimeType. You can output the request variable in the first step to rule out the possibility that the PDF has not arrived.
Edit (see at your comments): 'required|mimes:application/pdf'
Simply add "enctype" into your form
<form method="POST" enctype="multipart/form-data" action="your_action">
Basic Usage Of MIME Rule
'reportfile' => 'mimes:png'
Just try with full extensions
'reportfile' => 'mimes:application/pdf'
OR
"reportfile" => 'mimetypes:application/pdf'
Full listing of MIME types and extensions https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
Following above #maik Lowrey, I sucessfully used :
'file' => 'required|mimes:pdf|max:2048'
works very well ! ! ! (Answered here cause i don't have lvl to comment his answer)

Laravel 5.4 trying to save a form but redirecting always to the create controller

I have a very strange issue.
I worked fine but now when i try to post the datas with the submit button the form redirect my always to the create view without going the storeClub method.
here my form :
{!! Form::open(array('route' => 'store.club', 'method' => 'POST' , 'files' => true )) !!}
<div class="row">
<div class="form-group col-md-4">
<label>Nom * </label>
{!! Form::text('lb_nom', null, ['class' => 'form-control', 'placeholder' => 'Nom']) !!}
</div>
<div class="form-group col-md-5">
<label>Prénom *</label>
{!! Form::text('lb_prenom', null, ['class' => 'form-control', 'placeholder' => 'Prénom']) !!}
</div>
</div>
<div class="box-footer" data-step="5" data-intro='Une fois tout les éléments renseignés vous pouvez cliquer sur Enregistrer et votre licence sera directement envoyée dans le Panier.'>
{!! Form::submit('Enregistrer', ['class' => 'btn btn-info btn-lg center-block']) !!}
</div>
{!! Form::close() !!}
Here my routing :
Route::get('/create/club' , 'StructureController#createClub')->name('create.club');
Route::post('/save/club' , 'StructureController#storeClub')->name('store.club');
Here my create view :
public function createClub(Request $request){
//do something
return view('structure/createClub' , compact('type_structure' ,'structure_pere'));
}
here my store method that i can't call when i submit :
public function storeClub(Request $request){
//do something
}
what my doing wrong ? i have a lot of forms in the application who works fine like that
Don't you use any validation for your request ?
I think you have validation errors, but you don't display them, I got this problem.
Use in your view (before your form for example) :
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
If your request have errors, it will redirect to the previous page.
use #csrf After form tag:
{!! Form::open(array('route' => 'store.club', 'method' => 'POST' , 'files' => true )) !!}
#csrf

Route [admin/update] not defined in Laravel

I am having trouble updating my data in Laravel and this is giving me headache. Every time i clicked on the update button error occurs.
Below is the error:
"Route [admin/update] not defined. (View: C:\xampp\htdocs\Tailor\core\resources\views\expensesCat\edit.blade.php)"
here is the edit.blade
<div class="portlet light bordered">
<h3 class="page-title">Expenses Categories</h3>
{!! Form::model($expenses_category, ['method' => 'POST', 'route' => ['admin/update', $expenses_category->id]]) !!}
<div class="panel panel-default">
<div class="panel-heading">
Edit
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 form-group">
<input name="order_create_by" type="hidden" value="{{ Auth::user()->id }}">
{!! Form::label('name', 'Name*', ['class' => 'control-label']) !!}
{!! Form::text('name', old('name'), ['class' => 'form-control', 'placeholder' => '']) !!}
<p class="help-block"></p>
#if($errors->has('name'))
<p class="help-block">
{{ $errors->first('name') }}
</p>
#endif
</div>
</div>
</div>
</div>
The web.php
Route::post('/update/{id}', 'ExpensesCategoriesController#updateCat');
The ExpensesCategoriesController
public function updateCat(UpdateExpensesCategoriesRequest $request, $id)
{
$expenses_category = ExpensesCategory::findOrFail($id);
$expenses_category->update($request->all());
return redirect('admin/expenses_categories');
}
Use url in your form -
{!! Form::model($expenses_category, ['method' => 'POST', 'url' =>'admin/update/'.$expenses_category->id]) !!}
You're trying to use the route by its name, but you didn't name it. So, change it to:
'route' => ['admin.update', ....
And name the route:
Route::post('/update/{id}', 'ExpensesCategoriesController#updateCat')->name('admin.update');
Or:
Route::post('/update/{id}', ['as' => 'admin.update', 'uses' => 'ExpensesCategoriesController#updateCat']);

foreach loop in a form to save values

I try to save two values from text field but actually with this view when i click on save i get only the first ID of the foreach.
the two text field are : resultat_domicile and resultat_visiteur i would like to get the values foreach objects
someone know how to achieve this ? thanks a lot in advance
here my view :
#foreach($rencontres as $rencontre)
{!! Form::open(array('route' => array('add.resultat', $rencontre->id)))!!}
<div class="box-body">
<div class="row">
<div class="col-md-5">
<h2 class="pull-right"><div class="fa fa-shield pull-right"></div>{{$rencontre->equipe_domicile->lb_equipe}}</h2>
</div>
<div class="col-md-2">
<div class="row">
<div class="col-xs-5">
{!! Form::text('resultat_domicile', null , ['class' => 'form-control', 'placeholder' => 'Ex: 20 points']) !!}
</div>
<div class="col-xs-2 text-center">
<span class="h2">-</span>
</div>
<div class="col-xs-5">
{!! Form::text('resultat_visiteur', null , ['class' => 'form-control', 'placeholder' => 'Ex: 20 points']) !!}
</div>
</div>
</div>
<div class="col-md-5">
<h2 class="pull-left"><div class="fa fa-shield pull-left"></div>{{$rencontre->equipe_visiteur->lb_equipe}}</h2>
</div>
</div>
<div class="box-footer">
<div class="col-xs-12 text-center">
{!! Form::submit('Save', ['class' => 'btn btn-info btn-lg center-block']) !!}
{!! Form::close() !!}
</div>
</div>
#endforeach
here my controller :
public function addResultat(Request $request , $id){
$rencontre = Rencontre::findOrFail($id);
$rencontre->resultat_domicile = $request->input('resultat_domicile');
$rencontre->resultat_visiteur = $request->input('resultat_visiteur');
dd($rencontre);
}
Are you sure you're not only retrieving the LAST of the 'recontres'? When you create those form elements with the names 'resultat_domicile' and 'resultat_visiteur', you are overwriting which field gets sent to the request.
If you put a [] after the name, then they will be an array of values when they get sent to the request.
For example, this would be:
{!! Form::text('resultat_domiciles[]', ...

Adding Route and Perameter in Form action in html in laravel 5

I am unable to get this, My Route is as under;
Route::patch('/info/{iplive}', ['as' => 'info', 'uses' => 'InfoController#index']);
and Here is My Form,
{!! Form::open(['url'=>'info/', 'id'=> 'lookup', 'class' => 'user_form','method'=>'PATCH']) !!}
<div class="row center-block">
<div class="col-lg-6 center-block">
<div class="input-group">
<input type="text" id="lookup" name="lookup" value="" class="form-control" placeholder="Search for Address">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Lookup</button>
</span>
</div>
</div>
</div>
<!--</form> -->
{!! Form::close() !!}
I am in efforts to use a url like, link but unable to get it done :(
your route expects a paramater to be filled in,
try something like this
{!! Form::open(['method' => 'PATCH', , 'id'=> 'lookup', 'class' => 'user_form', 'route' => [ 'info' , $your_iplive_variable]]) !!}
where $your_iplive_variable should be the paramater you wish to fill in.

Resources