Getting a route not defined error in Laravel Blade - html

I am a Laravel/web newbie, and am working with a Laravel application inherited from someone else. In the blades, I see two ways in which forms are written.
<form action="{{ route('getdata') }}"
{!! Form::open(array('url'=>'getdata','
I think in the first case, the form starts in HTML format and route alone is defined in laravel format whereas in the second case, even form is defined in Laravel format. Which is preferred and what are the differences? I tried replacing the second format with the first and got a Route [/getdata] not defined error. Laravel Version is 6.

In the first example:
<form action="{{ route('getdata') }}"
You are writing plain html, except for the {{ route('getdata') }} part, that you can read similar to <?php route('getData') ?>. Basically, just the action (the url) of the form is a PHP call to a function that will echo the url in that position, while the rest is plain html (an hardcoded text).
In the second example, {!! Form::open(array('url'=>'getdata',' you are using a Facade to access a class that will generate an html output (similar to the code from the first example ) and you are passing to the method Open() an url, that will be placed inside the generated html in the action field.
The problem that you have using the second method, is that your are not passing your route, but a string. Change it like this:
{!! Form::open(array('url'=>route('getdata'),'
To fix the error of the undefined route, you should just call name()function at the end of your route:
Route::('your_route_url','controller#method')->name('getdata');

You didn't define any route named getdata acctually.
Change your code route to url :
<form action="{{ url('getdata') }}

Related

With use PathVariable CSS not loading and how to fill form to update in thymeleaf

I am trying to do same small service and sometimes I am sending requests to my Rest Api.
I am getting two problems:
How to fill field in form in html using thymeleaf? I have form like this and would like to fill fields with current values (th:value="${}" - not working for me):
<form th:action="#{/offences/edit}" method="post" th:object="${createOffenceForm}"> <input th:field="*{name}" th:value="${currentOffence.name}"/> <input th:field="*{penaltyPoints}" th:value="${currentOffence.penaltyPoints}"/> <input th:field="*{amountOfFine}" th:value="${currentOffence.amountOfFine}"/> <button type="submit">UPDATE</button> </form>
The problem is with loading css styles to html when I redirect to the site with path variable. For example i created html with two buttons. First one is hardcoded:
<a th:href="#{/offences/test}" class="link" style="text-decoration: none"><button class="buttonOK" type="submit">EDIT</button></a>
after redirect my site looks like this (everything works, it should be like that):
`
and here is after second button - redirect with path variable:
<a th:href="#{'/offences/edit/' + ${offence.id}}" class="link" style="text-decoration: none"><button class="buttonOK" type="submit">EDIT</button></a>
and view after load:
For the issue with your form data not being filled, this is because th:field and th:value are not meant to be used at the same time. So th:field ends up overwriting you value.
In your case I would suggest either manualy setting name (and id) or replacing th:object with the a filled version of the bean you want to return and only using th:field
As for the second issue it looks like a faulty fragment return on a post call to me but need to atleast have the controller functions and more complete html to say anything about that. And in general it's advisable to have 1 question per problem and not bundle things.
Ok so i found soultion.
To fill fields in form I only had to add to ModelMap form with fields.
Something like this and thymeleaf autofilled fields in html.
modelMap.addAttribute("createOffenceForm", new CreateOffenceForm(offenceDTO.getName(), offenceDTO.getPenaltyPoints(), offenceDTO.getAmountOfFine()));
Second solution is simple too. When changed mapping in Controller from #GetMapping("/path/{id}") to #GetMapping("/path-{id}") - everything works. There is no other problems with styles...

How can I set choice values in twig

When I put this code I have an error.
Impossible to access an attribute ("value") on a string variable ("Etudiant").
CODE:
{{ form_widget(registrationForm.typePerso,
{'choices': {'Etudiant': 'Etudiant','Enseignant' : 'Enseignant'}})
}}
This is from symfony Github:
"you cannot, because choices impact the building of the form. They are not passed as is to the template (they main goal of the Form component is not to handle the rendering, but to handle the form binding)"
https://github.com/symfony/symfony/issues/18950

Decoding HTML tags in Laravel 5

I am using CKeditor for my Laravel 5.1 custom CMS. When I request content of stored pages in the database on my view, they appear with HTML tags like <p>Hello world</p>.
I have used html_entity_decode() with and without the charset specified to no avail.
It is also worth mentioning that I use Blade template engine at my view. Hence, my demo code looks like
$post->content = '<p>Hello world</p>'; // from the database from controller
{{html_entity_decode($post->content)}} //did not decode
I also tried using it in my controller instead and it did not change anything like this
$post->content = html_entity_decode($post->content); //before sending it to the view
I need your help to address this issue.
Instead of doing {{html_entity_decode($post->content)}},
try this:
{!! $post->content !!}
More info here https://laravel-news.com/2014/09/laravel-5-0-blade-changes/
you can do this in the controller
strip_tags($post->content);
or if you use in blade
{{ strip_tags( $post->content ) }}
see more info here strip_tags

Why do we put "?" in action = "?"

When I was learning Php with Mysql, I got a problem :
<form action="?" method="POST">
.
.
.
</form>
Why do we put "?" in the action attribute?
PS: this form was for inserting text into a database.
Actually, I have an Index.php file which is the controller, and 2 files (form.html.php and joke.html.php as templates). In the tutorial, I first clicked on a link "add joke" to include the form.html.php file and in this form there is <form action="?"> </form> and a submit <input>. When I click on submit, the controller index tests and executes the inserted SQL query.
Thanks.
Personally don't ever do that.... Use action="action.php" or use action="" post to the current URL.
Not sure what you are trying to accomplish with ? in the action attribute.
"We" don't and "You" shouldn't do so either.
If you want to POST the data to the current URL, leave it empty, i.e. action="".
If you want to POST to another URL, put that URL in there, e.g. action="save.php".

Linking images to routes in Laravel

I'm working on laravel php framework , I've a simple question ,
While using blade template engine , I normally use html helper functions
to render forms , links or whatever so .
Now when i try to put a link to specific route , i usually to like that :
{{ HTML::link_to_route('author','TheAuthor')) }}
First parameter takes the route , and the second one takes the string that will appear ,
To make it simpler that code will produce :
TheAuthor
Now I want to replace TheAuthor with an image ,
What can i do ?
Or you can use {{route}}
Ex:
<img src="'.$image.'" />
I know this is not the answer you want to hear - but you cannot pass any image via link_to_route.
The problem is the output from the HTML class is escaped automatically. So if you try to pass this:
{{ HTML::link_to_route('author','<img src="'.URL::base().'assets/images/image.jpg" alt="icon" />')) }}
it comes out like this:
<img src="http://laravel3.dev/assets/images/image.jpg" alt="icon" />
which will just be text on the screen - no image. Instead you need to use URI::to_route('author') and generate the link yourself. So make a helper a like this (not tested):
function link_to_route_image($route, $image)
{
$m = '<a href="'.URL::to_route($route).'">'
. '<img>'.$image.'</img>'
. '</a>';
return $m;
}
It may become tedious if you are doing this all over the place, but in isolated spots the best way to handle this would actually be to wrap your entire link_to_* method call in HTML::decode.
{{ HTML::decode(HTML::link_to_route('author','<img src="{URL::base()}assets/images/image.jpg" alt="icon" />')) }}
There's nothing you can do from the inside of the function (such as wrapping the "title" portion in decode) because the cleaning is happening within the function call, but if you decode the entire link it will render the html properly.