Blade Templating Input Type Value - html

In Html forms i can do this as like :
<input type="hidden" name="token" value="{{ $token }}">
I would like to do this in full blade templating like this :
{{ Form::hidden('token', $token, array('class' => 'form-control')) }}
But the second option doesn't pass the $token value. Where do i go wrong ?
-- UPDATE --
I have found the solution for this :
It is quite easy question, but had to be careful while naming the inputs in blade templating.
This is my first form, which is working nice :
<form action="{{ action('RemindersController#postReset') }}" method="POST">
<input type="hidden" name="token" value="{{ $token }}">
Email<input type="email" name="email">
Password<input type="password" name="password">
Password<input type="password" name="password_confirmation">
<input type="submit" value="Reset Password">
</form>
**In blade templating form it was like **
{{ Form::open(array('action' => 'RemindersController#postReset')) }}
{{ Form::hidden('token', $token , array('class' => 'form-control')) }}
{{ Form::email('email', null, array('class'=>'form-control input-sm','placeholder'=>'Mail address')) }}
{{ Form::password('pass', array('class'=>'form-control input-sm','placeholder'=>'New Password')) }}
{{ Form::password('pass_conf', array('class'=>'form-control input-sm','placeholder'=>'Confirm Password')) }}
{{ Form::submit('Submit', array('class'=>'btn btn-info btn-block')) }}
{{ Form::close() }}
--SOLUTION--
So the error is :
In 1st form,
Password<input type="password" name="password">
Password<input type="password" name="password_confirmation">
In 2nd form,
{{ Form::password('pass', array('class'=>'form-control
input-sm','placeholder'=>'New Password')) }}
{{Form::password('pass_conf', array('class'=>'form-control
input-sm','placeholder'=>'Confirm Password')) }}
input names are password and password_confirmation, and in the second form they are named pass and pass_conf.
I changed pass to password and pass_conf to password_confirmation and it's now working fine.
Thank you for your feedbacks and patience.

It's just a guess but you can try to put also static value:
{{ Form::hidden('token', 'some token here', array('class' => 'form-control')) }}
and now check if it is in page source.
If it's not it means that you probably redirect to form using withInput and then Larravel fills the form with the same data as previous even if you set value manually to any HTML form element.
In this case you could make redirection using:
->withInput(Input::except('token'));
If it's the case you can read more about it in this topic: Laravel redirection using withInput doesn't allow to change inputs values
EDIT
You should try to change:
return Redirect::back()->with('error', Lang::get($response));
into:
return Redirect::back()->with(array ('error', 'token'), array(Lang::get($response), Input::get('token'));

Related

How can we use html <input type= ""> for Django model form?

using this for django model form, we cant create beautiful front end.
<form method="POST" action="">
{% csrf_token %}
<div class="esor">
Name: {{form.Name}}<br>
Class: {{form.Class}}<br>
Publisher: {{form.Publisher}}<br>
</div>
<input type="submit" value="submit">
</form>
Isn't there any ways so we can use html code like:
<form method="GET" action="#">
<input type="text" name="name" placeholder="Name of book">
</form>
instead of {{form.Name}} or {{form.as_p}}
Yes, you can get a beautiful interface that way, just pass the name you use in the form.py
Example:
forms.py
class NameForm(forms.ModelForm):
class Meta:
model = MyModel
fields = [
"whatever"
]
Html Template
<form method="POST">{% csrf_token %}
<input type="text" name="whatever" placeholder="Write whatever">
</form>
There are two approach I normally use:
Install widget tweaks, so you can customize your input easily:
{% load widget_tweaks %}
{{ form.Name|add_class:"css_class"|attr:"placeholder:Name of book" }}
Or 2, in your forms.py, set all the attributes you want for the field:
Name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Name of book'}))
You can do this by giving the name of your input element same as the name of your form field. And you can get name of your form field by displaying {{ form }} in html and then inspecting the form field in your browser and then the name attribute of that field will be name of the form field.

Laravel 5.4 not registrating users to database,

I'm simply trying to make my register form submit to my database, but my problem is you enter the info to the form press submit and it just reloads the page and does not send anything to the database.
originally it would submit what i entered but when you went to login it would just reload the page and not redirect, so i changed some stuff one thing lead to another now its not doing anything and i cannot pin-point the problems.
RegisterController.php
namespace App\Http\Controllers\Auth;
use App\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = ('index');
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest:admin');
}
routes/web.php
<?php
Auth::routes();
Route::get('/index', 'HomeController#index');
Route::prefix('/admin')->group(function() {
Route::get('/login', 'Auth\AdminLoginController#showLoginForm')->name('admin.login');
Route::post('/login', 'Auth\AdminLoginController#login')->name('admin.login.submit');
Route::get('/', 'AdminController#index')->name('admin.dashboard');
Route::get('/logout', 'Auth\AdminLoginController#logout')->name('admin.logout');
});
Route::prefix('/admin')->group(function() {
Route::get('register', 'Auth\AdminRegisterController#showRegistrationForm');
Route::post('register', 'Auth\AdminRegisterController#register')->name('admin.register');
});
Route::get('about', function () {
return view('about');
});
Route::get('advertise', function () {
return view('advertise');
});
Route::get('index', function () {
return view('index'); })->middleware('auth');
register.blade.php
#extends('header')
<div id="form-su" style="background-color: #d8b6ff" >
<h3 class="signuph"> Register </h3>
</div>
<form method="POST" action="{{ route('admin.register') }}">
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
<div class="div-su {{ $errors->has('name') ? ' has-error' : '' }}">
<input class="signup-menu2{{ $errors->has('name') ? ' has-error' : '' }}" " type="text" name="name" value="{{ old('name') }}" placeholder=" Name.." required >
<input class="signup-menu2{{ $errors->has('email') ? ' has-error' : '' }}" " type="email" name="email" value="{{ old('email') }}" placeholder=" Email.." required >
</br>
<input class="signup-menu2{{ $errors->has('password') ? ' has-error' : '' }}" type="password" name="password" placeholder=" Password.." required >
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
</br>
<label> confirm password
<input class="signup-menu2 " type="password" name="password_confirmation" placeholder=" Confirm Password.." required >
</label>
#endif
</br>
<button class="signup-btn" type="submit" name="submit" value="submit"> Sign Up </button>
</form> </br>
<p class="forgotten-password" href=""> Forgotten password?</p> </br>
<p class="log-in-here" a href="">Log in here </p> </br>
</div>
</div>
</form>
</body>
</html>
also for some reason the confirm password input does not show on the form in the browser, but if you submit the form it pops up... whys that?
i know that you may need to see other pages so don't hesitate to ask for them
i appreciate any help guys,

Form not submitted in laravel?

I want to submit the form to usercontroller file and call function invitebyemail.
When I click on submit it gives me error like "Whoops, looks like something went wrong."
Below are the structure of my Files and code.
Route File:
Route::POST('event/inviteforevent', 'UserController#invitebyemail');
Blade.php file
<form action="{{ route('inviteforevent') }}" method="POST">
<input type="email" class="form-control full-width" id="add-members-event-email" name="email" placeholder="Email address">
<input type="hidden" class="form-control full-width" name="eventid" value="{{ $event->id }}" >
<input type="submit" name="invite" value="submit" />
</form>
UserController File:
public function invitebyemail(Request $request){
$event = Event::find($request->eventid);
$timelineId = $event->timeline_id;
$username = Auth::user()->username;
$timelines = Timeline::find($timelineId);
return redirect()->back()->with('message', 'Invitation send to user by email.');
}
The route() helper function expects a route name.
Route::post('event/inviteforevent', 'UserController#invitebyemail')->name('inviteforevent');
Edit
Run php artisan serve and access your server at http://localhost:8000. You have not set the web root and laravel url rewrite won't work properly along with the url and route methods generating wrong links.
Apart from the Route change that was suggested, you may need to add the
{{ csrf_field() }}
after
<form action="{{ route('inviteforevent') }}" method="POST">
Read more here: https://laravel.com/docs/5.4/csrf#csrf-introduction
change
Route::POST('event/inviteforevent', 'UserController#invitebyemail');
to
Route::POST('event/inviteforevent', 'UserController#invitebyemail')->name("inviteforevent");
Reference : https://laravel.com/docs/5.4/routing#named-routes
Please replace this code in route:-
Route::post('event/inviteforevent', 'UserController#invitebyemail')->name('inviteforevent');
OR
Route::post('event/inviteforevent', ['as' => 'inviteforevent', uses' => 'UserController#invitebyemail']);
You have to add {{ csrf_field() }} which was suggested but one question? Where is your table names and id in your form opening? How that form have to know which table you want to add data in?
You have to add $events and $events->id into your form

how to change form field container div class?

I using symfony2.5. I change form field container div class.
generated form fields:
<div>
<label for="ddd" class="required">lname</label>
<input type="text" id="acme_demobundle_default_lname" name="acme_demobundle_default[lname]" required="required">
</div>
<div>
<label for="dddd" class="required">fname</label>
<input type="text" id="acme_demobundle_default_fname" name="acme_demobundle_default[fname]" required="required">
</div>
I add class to there are input`s container div.
any idea thanks
You can set attributes using the form builder inside your controller
$builder->add('lname', 'text', array('attr' => array('class'=>'something')))
If you have this in your form:
$builder->add('name')
In your template you should use the functions:
{{ form_label(form.name) }} {# access the label #}
{{ form_widget(form.name) }} {# access the input field #}
You can wrap that up with your div and style it however you want:
<div class="your-class">
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
</div>
You can check more on How to Customize Form Rendering

How to get the value of an input element directly in the twig template

I have this form in my twig template:
<form id="search-box" action="/search" method="get">
<input id="search-query" type="text" placeholder="Search..." name="search" role="textbox">
<a id="search-btn" href="....."></a>
</form>
My goal is to use the value of the input id="search-query" to create the href for the a id="search-btn"
For this, I have to access the value of the input id="search-query".
I have tryed:
{{ form.search-query.value }}
and
{{ search-box.search-query.value }}
But none of those works. I am getting the error that form or search does not exists.
Any idea?
In general, you can do this with jQuery:
var given_val = '';
var wanted_href = '';
given_val = $('input#search-query').val();
wanted_href = 'http://www.example.com/' + given_val;
$('a#search-btn').attr('href',wanted_href);
you should use {{ form.search-query.all.value }}