Input Validation with parsley.js on Laravel Blade - validation

first i use laravel blade for my input from, like this
{{ Form::open('practicums/'.$practicums->id, 'PUT') }}
<table align="center">
<tr>
<td>{{ Form::label('name', 'Practicum Name') }}</td>
<td width="75px"></td>
<td>{{ Form::text('name', $practicums->name) }}</td>
</tr>
</table>
i want to use parsley.js to validate the input, i add
data-validate="parsley"
as
{{ Form::open('practicums/'.$practicums->id, 'PUT', array('data-validate' => 'parsley')) }}
but when i add parsley.js parameters on input form (like: data-type,data-required, ets) it's error. then i use 'old' input form like
<input type="text" id="name" name="name" data-required="true"/>
it works.
How to use the parsley.js parameters in Laravel Blade?
Can i still use input form from Laravel Blade with parsley.js, or i should use the old method?
Thanks before.

maybe you should share what's the error it appear ?
i m using the parsley with laravel 4 without any problem, this is my sample code
{{ Form::text('name',Input::old('name'),array('id'=>'name','data-required'=>'true','data-required-message'=>'Name Required','placeholder'=>'Please enter name')) }}

You should do like this
{{ Form::open('practicums/'.$practicums->id, 'PUT', array('data' => 'parsley-validate')) }}

This might help you.
{!! Form::open(['route' => 'posts.store','data-parsley-validate'=>'']) !!}
{{Form :: label('title','Title:')}}
{{Form:: text('title',null,array('class'=>'form-control','required'=>'','max length'=>'255'))}}
{{Form :: label('slug','Slug:')}}
{{Form::text('slug',null,["class"=>'form-control','required'=>'','min length'=>'5','max length'=>'255'])}}
{{Form::label('body','Post Body:')}}
{{Form::textarea('body',null,array('class'=>'form-control','required'=>''))}}
{{Form::submit('Create Post',array('class'=>'btn btn-success btn-block','style'=>'margin-top:20px;'))}}
{!! Form::close() !!}

Related

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST

I am creating an index form that displays some data. Everything is ready but when I make the delete button I get an error "The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST."
Route
Route::group(['middleware' => ['auth']], function() {
Route::resource('roles','RoleController');
Route::resource('users','UserController');
Route::resource('kamar_theresia','Kamar_TheresiaController');
});
Controller
public function destroy($id)
{
Kamar_Theresia::find($id)->delete();
return redirect()->route('kamar_theresia.index')
->with('success','Kamar Theresia deleted successfully');
}
View
#foreach ($kamar_theresia as $tere)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $tere->nama }}</td>
<td>{{ $tere->name }}</td>
<td>{{ $tere->ketersediaan }}</td>
<td>
#can('theresia-delete')
{!! Form::open(['method' => 'DELETE','route' => ['kamar_theresia.destroy', $tere->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
That's because the you're passing DELETE method as the method of your form, and it is wrong, the right thing to do is pass de POST method.
Check this example:
<form action="{{ route('kamar_theresia.destroy', $tere->id) }}" method="POST">
#csrf
#method('delete')
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
Your controller should be:
public function destroy(Kamar_Theresia $khamar_teresia)
{
$khamar_teresia->delete();
return redirect()->route('kamar_theresia.index')
->with('success','Kamar Theresia deleted successfully');
}
Make sure you do not have your form inside another form. I made this silly mistake and got the same error message.
Looks like you're almost there! I would use POST for the form similar to this:
{{ Form::open(['method' => 'POST', 'route' => ['kamar_theresia.destroy']) }}
{{ Form::hidden('id',$tere->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
and then in your controller
public function destroy(Request $request){
$id = $request->input('id');
Kamar_Theresia::find($id)->delete();
The rest of your code should be ok. Let me know if this doesn't work.
Use the {{ csrf_field() }} and {{ method_field('DELETE') }} into Form.
{{ csrf_field() }}
{{ method_field('DELETE') }}
Use this into Controller
public function destroy($id)
{
$delete = kamar_theresia::find($id);
$delete->delete();
return redirect('/')->with('deleted','Kamar Theresia deleted successfully');
}
if we are using Route::resource() then it will be automatically route with destroy function.
Forgot to put slash in action at start:
<form method="POST" action={{--here=> --}}"/save_edit_delete_order/{{$order_id}}">
#csrf
#method('delete')
......
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Yes, I am</button>
</div>
</div>
</form>
And in resource Controller:
public function destroy($id)
{
return 'kuku';
}
view
<form action="{{route('command.delete',[$command->id,$command->car_id])}}" method="post">
#csrf
{{method_field('delete')}}
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
web
Route::delete('/commands/{commandId}/{carId}/delete','CommandController#deleteUserCommands')->name('command.delete');

Changing laravel form to html form

Hi i created a form with laravel form helpers but i want to change it to a standard html form. The issue i am having is with the "PUT" function, when i try to edit my posts no data is displayed so i think my form properties are wrong.
Form header
<form method="post" action="{{route('posts.update',[$post->id])}}" enctype="multipart/form-data">
{{csrf_field()}}
{{method_field('put')}}
<input type=""text" name="name" class="name">
<input type=""text" name="body" class="body">
<button></button>
</form>
(--UPDATED--)
Laravel Form
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, ["class" => 'form-control input-lg']) }}
{ Form::label('body', 'Body:') }}
{{ Form::text('body', null, ["class" => 'form-control input-lg']) }}
{{ Form::submit('Save Changes', array('class' => 'btn btn-success btn-block')) }}
{{ Form::close() }}
I want the blog data to be displayed in the form for me to edit.. When i use form helpers it works fine
Any help will be much appreciated
Thanks
Ash
Ash,
I don't see the Laravel form code here to compare the two, but the easiest way to convert the Laravel form to straight HTML is to view source on the generated form and copy the generated HTML code back into the blade. Then you can replace populated data with variables.
(--UPDATED--)
Hence, put this back into your blade:
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, ["class" => 'form-control input-lg']) }}
{ Form::label('body', 'Body:') }}
{{ Form::text('body', null, ["class" => 'form-control input-lg']) }}
{{ Form::submit('Save Changes', array('class' => 'btn btn-success btn-block')) }}
{{ Form::close() }}
and then copy the HTML output for the "put" action. Or, if you're on the current version of Laravel, you can simply use the put method as follows:
#method('PUT')
(see https://laravel.com/docs/5.8/blade)
(--ORIGINAL--)
Also, you didn't mention what version of Laravel you're on. The syntax for inserting fields into the blade varies.
(--UPDATED--)
#kapitan, you're right. I "learned" that from using LaraShift telling me that all my {{ $field }} needed to be changed to {!! $field !!} to upgrade my app. The syntax has changed as follows.
In older versions of Laravel, variables inserted with {{ $field }} were UNescaped, and variables inserted with {{{ $field }}} were escaped.
In newer versions of Laravel, variables inserted with {{ $field }} are escaped, and variables inserted with {!! $field !!} are UNescaped. So the meaning of {{ $field }} has reversed from older versions to newer versions.

Trying to get property of non-object - laravel 5.4

I am using Laravel eloquent relationship, when i use
{{$subject->cat}}
i receive a json response like below
{"id":13,"name":"Fsc","created_at":"2017-10-23 00:00:00","updated_at":"2017-10-23 00:00:00"}
as i have 2nd object "name" here i tried
{{$subject->cat->name}}
but got error
Trying to get property of non-object
while i am using same approach for other table and copied same method here but getting error.
See my Blade file code, i am calling object inside foreach loop
#foreach ($subjects as $subject)
<tr>
<td width="8%">{{$subject->id}}</td>
<td width="22%">{{$subject->subject}} </td>
<td width="22%">{{$subject->cat->name}}</td>
<!-- <a class="btn btn-success btn-sm" href="{{route('subjects.show', $subject->id)}}"><i class="fa fa-eye"></i></a> | -->
<i class="fa fa-pencil"></i> |
{!! Form::open(['route' => ['subjects.destroy', $subject->id], 'method' => 'DELETE', 'class' => 'delete-form']) !!}
{{ Form::button('<i class="fa fa-times" aria-hidden="true"></i>', ['class' => 'btn btn-danger btn-sm pull-left', 'type' => 'submit']) }}
{!! Form::close() !!}
</td>
</tr>
#endforeach
First Use json_decode($json);
Takes a JSON encoded string and converts it into a PHP variable.
Then
Use {{$subject->cat}}
Just use json_decode like:
$var = json_decode($subject, true);
Then you can use:
{{$subject->cat->name}}
See more here!
Hope this helps you!
You need to decode your json and then use like this:
$result = json_decode ($subject->cat);
echo $result->name;
Use your variable name in place of $result
For insight here is pastebin demo
Try this
$cat = json_decode($subject->cat); // returns object
{{ $cat['name'] }}
or
$cat = json_decode($subject->cat, true); // returns array
{{ $cat['name'] }}

pass multiple id's from checkboxes to my controller with Laravel

I have a products table in my database. I'm getting the name of the products from my DB and print theire names in a bootstrap table on my blade. Currently there is a edit button on every row line. If I'm pressing the button I'm passing the ID of this product to my controller edit function. Now I want to add checkboxes for every product in this table and if I set for example two ticks ( so two different products ) I want to pass both ID's at the same time to my controller.
Looks like this:
{!! Form::open(['action' => 'ProductController#tags']) !!}
#foreach($products as $product)
<tr>
<td>
{!! Form::checkbox('check', $product->id) !!}
</td>
<td>
{{ $product->name }}
</td>
<td>
#foreach($product->tags as $tag)
{{$tag->name}},
#endforeach
</td>
<td>
{{ $product->created_at->format('d.m.y') }}
</td>
<td>
<a href="{{ action('ProductController#edit', ['id' => $product->id]) }}">
<button class="btn btn-default" style="float: right;">Edit</button>
</a>
</td>
</tr>
#endforeach
{!! Form::close() !!}
As you see, I've set the product id as a seccond parameter for every checkbox, but this haven't worked well of course. Can someone tell me a way how I can give every checkbox the ID of the product and pass their ID's to my controller at the same time, if they are marked with a tick ?
Thanks for taking the time :)
Have you tried accepting multiple checkbox values as array by adding '[]' to the checkbox name?
{!! Form::checkbox('check[]', $product->id) !!}
I believe that this Form facade was from Laravel 4, i never used it, but i think that this will generate an input, with checkbox type, a name of 'check' and the id as the value, right?
Did you tried to set the name to 'check[]'? This way all checked ids will be in one array 'check' in your request. In Laravel 5 at least, you can try to dump the request['check'] and this should return an array os checked ids

Laravel: Getting plain text while using Form class

I am a beginner of laravel. I have just installed "illuminate/html": "~5.0" with the help of link http://www.ekoim.com/blog/fix-class-form-html-not-found-laravel-5/
Now when I tried using of {{ Form::open() }} {{ Form::close() }} in view file. It is returning form in plain text format to browser.
<form method="POST" action="http://project.dev" accept-charset="UTF-8"><input name="_token" type="hidden" value="qNbi3DD1YGQncOv3PRBWl1l6BxVViWZnleVAmniS"></form>
I am attaching screenshot here.
Is there anything I forgot to do in order to this code to work?
Unlike Laravel 4, in Laravel 5 the content between {{ }} is now escaped by default. To output unescaped content you need to use {!! !!}. So in your case:
{!! Form::open() !!} {!! Form::close() !!}
Read more in the Laravel Templates Docs.

Resources