How to write form markup in Laravel4 - html

Can you help me to write this form tag in blade sintax:
<form name="room" id="1" method="post" class="narrow_width">
Thank you!

See http://laravel.com/docs/html for the Form syntax and methods.
You can wrap these in Blade tags.
{{ Form::open() }}
{{ Form::text('username') }}
Etc... In your case
{{ Form::open(array('name' => 'room', 'method' => 'post', 'class' => 'narrow_width', id => '1')) }}

Related

HTML tag displaining plainly instead of being processed [duplicate]

This question already has answers here:
HTML in Symfony2 form labels instead of plain text
(9 answers)
Closed 1 year ago.
I'm using the following simple controller function to create a form:
$json = json_decode(file_get_contents('../data/sample.json'));
public function index(Request $request): Response
{
$form = $this->createFormBuilder();
$form->add('city', ChoiceType::class, [
'choices' => $json[0]->options,
'label' => $json[0]->label,
]);
$form->add('submit', SubmitType::class, [
'attr' => [
'class' => 'btn btn-primary'
],
]);
$form->add('next', SubmitType::class, [
'attr' => [
'class' => 'btn btn-warning',
'disabled' => true
]
]);
$form = $form->getForm();
.
.
.
return $this->render('quiz/index.html.twig', [
'form' => $form->createView(),
]);
}
The data is read from the following same json file :
[
{
"options" : ["Lisbon", "London", "Los Angels"],
"label" : "Please select your <u>city</u> of residence"
}
]
Each of the labels has some HTML tag(s) in them but all of those tags are not being processed. So the example above displays in my browser exactly as seen in the code.
In my twig template, I have tried each one of the following but individually:
{{ form(form) }}
{{ form(form) | raw }}
{{ form(form) | convert_encoding('UTF-8', 'iso-2022-jp') }}
{{ form(form) | convert_encoding('UTF-8', 'HTML-ENTITIES') }}
{% autoescape false %}
{{ form(form) | convert_encoding('UTF-8', 'HTML-ENTITIES') }}
{% endautoescape %}
{{ form(form) | striptags('<u><input><p><select><option><br><b><button>') }}
{{ form(form) | striptags('<u><input><p><select><option><br><b><button>') | raw }}
Despite all those suggestions I found online, my form labels still display unprocessed HTML tags.
An potential added complication is that I print the entire form as one whole using {{ form(form) }}. However because the form is dynamically assemble from JSON data, it's not really possible to print the form in segments because the JSON data can be added to or removed from at any time.
Well the answer turned out to be rather simple.
In order to include HTML tags in the label, label_html => true needs to be set on the formgroup, according to this post.

Adding form action in html in laravel

I am unable to pass url in views html form action tag.
<form method="post" action="??what to write here??" accept-charset="UTF-8">
I want to set it's action to WelcomeController#log_in function in WelcomeController file in controllers.
Here are my routes:
Route::get('/','WelcomeController#home');
Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController#log_in'));
Route::get('home', 'HomeController#index');
After submitting it keeps the same url
http://localhost:8000/
And the main error line
Whoops, looks like something went wrong.
After that there is 1/1 TokenMismatchException in compiled.php line 2440:
You can use the action() helper to generate an URL to your route:
<form method="post" action="{{ action('WelcomeController#log_in') }}" accept-charset="UTF-8">
Note that the Laravel 5 default installation already comes with views and controllers for the whole authentication process. Just go to /home on a fresh install and you should get redirected to a login page.
Also make sure to read the Authentication section in the docs
The error you're getting now (TokenMismatchException) is because Laravel has CSRF protection out of the box
To make use of it (and make the error go away) add a hidden field to your form:
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
Alternatively you can also disable CSRF protection by removing 'App\Http\Middleware\VerifyCsrfToken' from the $middleware array in app/Http/Kernel.php
Laravel 5.8
Step 1: Go to the path routes/api.php add:
Route::post('welcome/login', 'WelcomeController#login')->name('welcome.login');
Step2: Go to the path file view
<form method="POST" action="{{ route('welcome.login') }}">
</form>
Result html
<form method="POST" action="http://localhost/api/welcome/login">
<form>
if you want to call controller from form action that time used following code:
<form action="{{ action('SchoolController#getSchool') }}" >
Here SchoolController is a controller name and getSchool is a method name, you must use get or post before method name which should be same as in form tag.
1) In Laravel 5 , form helper is removed .You need to first install laravel collective .
Refer link: https://laravelcollective.com/docs/5.1/html
{!! Form::open(array('route' => 'log_in')) !!}
OR
{!! Form::open(array('route' => '/')) !!}
2) For laravel 4, form helper is already there
{{ Form::open(array('url' => '/')) }}
Use action="{{ action('WelcomeController#log_in') }}"
however TokenMismatchException means that you are missing a CSRF token in your form.
You can add this by using <input name="_token" type="hidden" value="{{ csrf_token() }}">
For Laravel 2020. Ok, an example:
<form class="modal-content animate" action="{{ url('login_kun') }}" method="post">
#csrf // !!! attention - this string is a must
....
</form>
And then in web.php:
Route::post("/login_kun", "LoginController#login");
And one more in new created LoginController:
public function login(Request $request){
dd($request->all());
}
and you are done my friend.
In Laravel 8:
Step 1: In the blade file:
<form action="{{ route('authLogin') }}" method="post">
#csrf
....
</form>
Step 2: And then in web.php:
use App\Http\Controllers\UsersController;
Route::post('login-user', [UsersController::class, 'login'])->name('authLogin');
Step 3: And in the UsersController:
public function login(Request $request){
dd($request->all());
}
Happy to share. Thanks to ask this question.
** For more information, please see https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes
{{ Form::open(array('action' => "WelcomeController#log_in")) }}
...
{{ Form::close() }}
You need to set a name to your Routes.
Like this:
Route::get('/','WelcomeController#home')->name('welcome.home');
Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController#log_in'))->name('welcome.log_in');
Route::get('home', 'HomeController#index')->name('home.index');
I just put name on Routes that need this. In my case, to call from tag form at blade template.
Like this:
<form action="{{ route('home.index') }}" >
Or, You can do this:
<form action="/" >
Form Post Action :
<form method="post" action="{{url('login')}}" accept-charset="UTF-8">
Change your Route :
In Routes -> Web.php
Route::post('login','WelcomeController#log_in');
The following should work.
{{ Form::open( array('url' => action('WelcomeController#log_in'), 'files'=>true,'method'=>'post') ) }}
...
{{ Form::close() }}
I wanted to store a post in my application, so I created a controller of posts (PostsController) with the resources included:
php artisan make:controller PostsController --resource
The controller was created with all the methods needed to do a CRUD app, then I added the following code to the web.php in the routes folder :
Route::resource('posts', 'PostsController');
I solved the form action problem by doing this:
I checked my routing list by doing php artisan route:list
I searched for the route name of the store method in the result table in the terminal and I found it under the name of posts.store
I added this to the action attribute of my form: action="{{route('posts.store')}}" instead of action="??what to write here??"
Your form is also missing '{{csrf_field()}}'

Define HTML validation attributes with Laravel Blade syntax

I would like to combine the Laravel model binding properties with the easy jQuery Validation plugin, if that is possible.
I can't figure out how to add the 'required' attribute to the blade syntax.
Here is normal form syntax with the "required" attribute:
<input type="text" name="title" required>
Laravel Blade syntax
{{ Form:: text('title', null, array('class' => 'form-control')) }}
Any help would be appreciated.
required is a normal HTML input attribute. The only difference is that it doesn't have different values -- it's either present or it's not, which makes it the so-called boolean attribute.
Anyway, this should do the trick:
{{ Form:: text('title', null, array('class' => 'form-control', 'required' => '')) }}
See more here.

Bootstrap: pull left and right in panel footer breaks footer

I have a a Bootstrap panel with two buttons in the footer:
<div class="panel-footer">
{{ Form::open( array('route' => array('dogs.edit', $dog->id), 'method' => 'get', "class" => 'col-lg-6 ')) }}
{{ Form::submit("Edit", array('class' => 'btn btn-default')) }}
{{ Form::close() }}
{{ Form::open( array('route' => array('dogs.destroy', $dog->id), 'method' => 'delete', "class" => 'col-lg-6 ')) }}
{{ Form::submit("Delete", array('class' => 'btn btn-default')) }}
{{ Form::close() }}
</div>
The forms for each of the buttons have the class pull-left and pull-right so that they are each floated to either side of the panel footer.
The float is working, but the buttons are now outside of the footer:
I tried using the grid system instead of the pull helpers but I ended up with the same result.
I've also searched around for the solution (this has to be pretty common I would think) and haven't found an explanation that doesn't require overriding Bootstrap with custom css.
Is it possible to fix this with just Bootstrap or would I need to throw my own css in to fix it?
Just add a div with clearfix after the buttons
<div class="clearfix"></div>
stalin's answer is correct, but you can use another approach that is more elegant
From Bootstrap Documentation (#clearfix)
Easily clear floats by adding .clearfix to the parent element.
Just add clearfix to the parent div:
<div class="panel-footer clearfix">
{{ Form::open( array('route' => array('dogs.edit', $dog->id), 'method' => 'get', "class" => 'col-lg-6 ')) }}
{{ Form::submit("Edit", array('class' => 'btn btn-default')) }}
{{ Form::close() }}
{{ Form::open( array('route' => array('dogs.destroy', $dog->id), 'method' => 'delete', "class" => 'col-lg-6 ')) }}
{{ Form::submit("Delete", array('class' => 'btn btn-default')) }}
{{ Form::close() }}
</div>
just add the class text-right on the panel-footer div
I think its strange that .panel-footer isn't included in the clearfix css library already the way .panel-body and even .modal-footer are. Instead of having to remember to throw a clearfix class on ever footer (if you have a lot it can be a pain) its better to just add the clearfix css to .panel-footer in your master CSS or edit the library directly.
.panel-footer:before, .panel-footer:after {
display: table;
content: " ";
}
.panel-footer:after {
clear: both;
}

following a tutorial notfoundhttpexception

i am pretty new to framework and laravel in general, im struggling to learn laravel, and i have found this book.
https://leanpub.com/learninglaravel
I hope is a good start. (usually i coded in php and html)
I have a problem following the basic tutorial (:/ not a good start lol)
Routes.php
Route::post('contact', function()
{
$data = Input::all();
$rules = array(
'subject' => 'required',
'message' => 'required'
);
$validator = Validator::make($data, $rules);
if($validator->fails()) {
return Redirect::to('contact')->withErrors($validator)->withInput();
}
return 'Your message has been sent';
});
Route::get('/', function()
{
return View::make('home');
});
Route::get('about', function()
{
return View::make('about');
});
Contact.blade.php
#extends('layout')
#section('content')
<h1>Contact Us.</h1>
<p>Please contact us by sending a message using the form below:</p>
{{ HTML::ul($errors->all(), array('class'=>'errors'))}}
{{ Form::open(array('url' => 'contact')) }}
{{ Form::label('Subject') }}
{{ Form::text('subject','Enter your subject') }}
<br />
{{ Form::label('Message') }}
{{ Form::textarea('message','Enter your message') }}
<br />
{{ Form::submit() }}
{{ Form::close() }}
#stop
THe other pages works well
You need another route to display the contact form page
Route::get('contact', function() {
return View::make('contact');
});