Laravel Validation of a HTML array within the request - laravel

I'm using {!! Form::model( $data) !!} to get my edit working dynamically, also I have normal inputs like {!! Form::text('name', null) !!} but, within my controller, before I send my $data to my view, I use with like the following $data = User::with('Address')->find($id_user);
When I submit, I send my form to my controller update public function update(MyRequest $request) that merge both rules (address and user) to create the validation.
The problem:
The Address input to work with Form::model, I had to make it as an array like this {!! Form::text('Address[zipcode]', null) !!} and now, my validation doesn't work.
My request looks like this:
Array ( [_method] => PATCH [_token] => OlF0IuFxwTIucFavzUB9Sk1IGE5NJlaaarbBxSE [name] => Michel [Address] => Array ( [zipcode] => 80000400))
I was thinking if there is a way to indicate via rule that it should be looked inside the sub-array, something like Address[zipcode] but it didn't work

Got that working right after I posted the question....
$rules = ['Address.zipcode' => 'required']
Instead of quotes like before, using DOT will make you access the sub-array

Related

Laravel validation of spezific field/value

i have a form field with two options:
{!! Form::select('pet',array('1'=>'Dog', '2'=>'Cat') ,null,['class'=>'form-text']) !!}
is there any way to validate this values via $this->validate($request, [...]) or do i have to do this via if statement?
thanks for any sugestions.
You cant prevent people edit the data
You have to validate the data once you have the request in you server
Could try like this:
$this->validate(request(), [
'pet' => 'required|in:1,2'
]);
so only if pet value is 1 or 2 will pass.
not tested but should work

Laravel - HasMany CRUD - Update

I need some help, please,
I have two tables: carriers and sell
Carrier CRUD is working. Sell CRUD is also working, except Update method.
In order to update, i need carrierId and sell id, right?
This is my CarrierSellController:
And this is my carriers/sell/edit.blade.php
The model binding is working:
Form action has /edit at the end of the url. It's oke?
If i hit submit button, i get this error:
Try this one
{!! Form::model($sell,['url'=>url('carriers/'.$sell->carrier->id.'/sell/'.$sell->id),'method'=>'patch']) !!}
and in the Sell model add
public function carrier()
{
return $this->belongsTo(Carrier::class);
}
make sure the method is PUT
add this to your form
{{ method_field('put') }}
Why don't you if you want to be absolutely sure just try declaring the route yourself like:
Route::put('carriers/{carrierId}/sell/{id}', [ 'as' => 'carriers.sell.update', 'uses' => 'CarrierSellController#update' ]);
And then in your view have this:
{!! Form::model($sell, ['method' => 'PUT', 'route' => ['carriers.sell.update', 'carrierId' => $sell->carrier->id, 'id' => $sell->id]) !!}

Get the model ID when updating a resource

I have a form that will submit a Patch request to the controller's update method.
But the update method requires to have $id, as you can see below whenever I try that I get a
No query results for model [Item]. Since the update method did not receive the $id of the model
public function update($id)
{
$item = Item::findOrFail($id);
$update = Input::all();
// some codes to save changes
return Redirect::route('items.index');
}
Another thing is that whenever I submit the form, url turns into something like this:
mw.dev/items/%7Bitems%7D
Edit
routes.php
Route::resource('items','ItemsController');
ItemController
public function edit($id)
{
$item = Item::findOrFail($id);
return View::make('items.edit')->with('item',$item);
}
I have included the code on my edit.blade.php
{{Form::open(array('route' => 'items.update', 'method'=>'patch'))}}
{{Form::text('barcode', $item->barcode, array('placeholder' => 'barcode'))}}
{{Form::text('imei',$item->imei, array('placeholder' => 'imei'))}}
{{Form::text('item_name', $item->item_name, array('placeholder' => 'item name'))}}
{{Form::submit('edit')}}
{{Form::close()}}
You have to pass the model to your view and need to pass the id parameter when generating the form. Assume that you have a User model and it's available in the view. So you may generate the form action using something like this:
// Using Form::open method with route
Form::open(array('route' => array('route.name', $user->id)))
// Using Form::model method with route
Form::model($user, array('route' => array('route.name', $user->id), 'method' => 'patch'))
// Using Form::open method with action
Form::open(array('action' => array('Controller#update', $user->id), 'method' => 'patch'))
// Using Form::open method with url
Form::open(array('url' => 'something/update/' . $user->id, 'method' => 'patch'))
Check more on Opening A Form.
The URL
mw.dev/items/%7Bitems%7D
is most likely the url-encoded form of
mw.dev/items/{items}
I suppose there is a problem in the form submission or in the <form>'s action paremeter or in the Route::* declaration in routes.php.
This could also explain why you don't get any $id upon submission.

How to pass validation errors to view without redirect in Laravel

How can i pass validation errors directly to a View without a redirect in Laravel?
I don't want to do that:
return Redirect::back()->withErrors($validator)->withInput();
but rather:
Input::flash(); // for repopulating fields
Validator::flash(); // this doesn't exist
return View::make('fragments/login_ajax');
Or another more concise direct generation of a view without a redirect. Optimal would be something like that, but that doesn't work:
return View::make('fragments/login_ajax')->withErrors($validator)->withInput();
The reason is, that this is the answer to an ajax based login. Redirecting to another method only used to display the same view deems unneccessary.
Cheers
Though agreeing it is a bad idea to do this, you can use View::share() to share the errors to all the views.
\View::share('errors', $errors);
Use below solution.
Controller:
$validator = Validator::make($request->all(),
[
'class_id' => 'required|numeric|not_in:0',
'batch_id' => 'required|numeric|not_in:0',
'student_id' => 'required|numeric|not_in:0'
]);
$this->viewData['errors'] = $validator->errors();
View File:
{!! Form::label('class_id','Student Class') !!}
{!! Form::select('class_id', array(), 0, ['class' => 'form-control', 'id' =>
'class_id']) !!}{!! $errors->first('class_id', '<p class="help-block">:message</p>')!!}
Real answer:
this is not an issue that is worth spending time on. It is an architecture problem.
Possible Solution to your situation (not recommended):
Make auth a new route and have it return json of errors/messages. Then have JS deal with the errors / messages / logic.

form model binding and file upload

I got an answer previously to my question about how to create edit/update details form in laravel, and the answer was utilizing Form::model(modelName). Nevertheless, the page which is updating is the same page handling the insertion. I have the following form opening:
{{ Form::open(array('action' => 'SomeController#basic','files' => true)) }}
how can I change this to use the form::model instead? I also have an upload of file in same page (profile pic).
Laravel 4 or 5 Form <-> Model binding
Maybe is a "bit" late for the original poster, but for those who search for a solution, this can be used as a later reference.
The L4 solution is:
{{ Form::open($object, array('method' => 'PATCH', 'action' => array('SomeController#update', $object->id),'files' => true)) }}
For Laravel 5, you would use:
{!! Form::model($object, ['method' => 'PATCH', 'action' => ['ObjectsController#update', $object->id], 'files' => true]) !!}
Home this helps!
Form model binding can be done by declaring form open as
{{ Form::model($user, array('action' => 'SomeController#post', 'files'=> true)) }}
You pass the model to the view for example here $user is the model.
$user = User::find(1);
return View::make('editUser')->with(compact('user'));
For file upload, if the $user is isset you could display the image. Along with the upload button. When update is clicked you can check
if(Input::has('file'))
then move the file and upload the path in the database. May be there is a different solution for this.
Is this what you were looking for?

Resources