I am working on laravel 5.3 and i want to get authenticated user email as my field value my field code is
<div class="col-md-8">
<input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : '{{Auth::user()->email}}' }}" required />
</div>
Here above i tried as {{Auth::user()->id}}' }} but its give syntax error if
syntax error, unexpected '}'
If i try with single bracket it prints as it is
You have syntax error,
<input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : Auth::user()->email }}" required />
Copy paste this code,
you tried to give {{}} inside {{}} to fetch auth details.
Give it a try, it should work.
instead of
<input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : Auth::user()->email }}" required />
Try,
#php($email=Auth::user()->email)
#if(isset($student->email))
$email=$student->email
#endif
<input type="text" class="form-control" name="email" value="{{ $email }}" required />
Related
I´m traying to create form to create event.
i have one resourceRoute in my laravel 7. But when i send my form, return this message:
The POST method is not supported for this route
but how i said i have a resource route, and i call to method create.
My routes
Route::resource('calendario', 'CalendarioController');
My form:
div class="col-xs-12 p-5">
<form action="{{ Request::is('calendario/*/edit') ? route('calendario.update', $event->id) : route('calendario.create') }}" method="post">
#csrf
#if(Route::currentRouteName() == 'calendario.edit')
#method('PUT')
#endif
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" name="nombre" id="nombre" value="{{ isset($event) ? $event->nombre : old('nombre') }}" aria-describedby="emailHelp" placeholder="Nombre del cliente">
</div>
<div class="form-group">
<label for="fecha-inicio">Fecha Inicio</label>
<input type="text" value="{{ isset($event) ? $event->fecha_inicio : old('fecha_inicio') }}" class="form-control" name="fecha_inicio" id="fecha-inicio">
</div>
<div class="form-group">
<label for="fecha-inicio">Fecha Fin</label>
<input type="text" value="{{ isset($event) ? $event->fecha_fin : old('fecha_fin') }}" class="form-control" name="fecha_fin" id="fecha-fin">
</div>
<input type="submit" class="btn btn-info" value="{{ Request::is('calendario/*/edit') ? 'Guardar' : 'Crear Cita' }}">
</form>
</div>
</div>
</div>
thsi form it´s used also for edit event and in edit i can ok... I don´t understand that i´m doing wrong
Thanks you for help
This is 100% an issue with the Route::resource('calendario', 'CalendarioController')
The update method accepts put as I can remember.
As you can see here: put/patch
So you can change your form method to method="put" or your have to define your routes like this: Route::post('path/{id}', 'CalendarioController#update')
I am trying to disabled an input field if it has value. Somehow, it looks like this:
<input type="text" name="sex" value="{{ old('sex', $user['sex']) }}" placeholder="">
I have tried adding like:
<input type="text" name="sex" value="{{ old('sex', $user['sex']) }}"
disabled= {{ $user['sex'] == null ? disabled :'' }} >
But it is not working. I am using blade.php by the way.
Try this
<input type="text" name="sex" value="{{ old('sex', $user['sex']) }}" {{ $user['sex'] ? '' : 'disabled' }} >
By disabling the field it breaks the validation rules. I would rather prefer using readonly attribute.
<input class="form-control" name="name" type="text" {{ $role->name ? 'readonly' : '' }} value="{{ old('name', $role->name) }}" required="true" aria-required="true"/>
I have created a create form create.blade.php and a edit form edit.blade.php - both form fields are identical.
It quite annoying when I add new fields in the create.blade.php, I also need to add fields in edit.blade.php.
How can I merge into 1 blade file it so it work for both way for creating and editing (view via db).
For example in create.blade.php I have this:
<label>Name</label>
<input type="text" name="name" value="{{ old('name') }}">
#if ($errors->has('name'))
<label class="error">{{ $errors->first('name') }}</label>
#endif
In edit.blade.php
<label>Name</label>
<input type="text" name="name" value="{{ old('name', $category->name) }}">
#if ($errors->has('name'))
<label class="error">{{ $errors->first('name') }}</label>
#endif
Just check if model exists:
{{ old('name', empty($category) ? '' : $category->name) }}
Also, you may want to check if it's create or update form and change form url accordingly:
{{ route(empty($category) ? 'article.create' : 'article.update' ) }}
<input type="text" name="name" value="{{ $category ? $category->name : old('name') }}">
you can use a partial (fancy name for a smaller template) for your form, and pass that partial the data you require.
Using the example of a booking system:
In your create blade file:
#include('bookings._forms', ['booking' => new \App\Booking])
and the edit file ($booking being retrieved on the controller):
#include('bookings._forms', ['booking' => $booking])
And then your form partial can look like this:
<label for="name" class="label">Name</label>
<input type="text"
class="input"
name="name" id="name"
placeholder="Guest name"
value="{{ old('name') ?? $booking->name }}">
The good thing with this method is that now you can have ONLY your form elements on the partial and only have to update 1 file when you add fields, AND the actual tag stays on your create and edit template, so no worries about that either.
Hope that's clear and it helps.
My form submission controller
// Code to get input from user and validation is done
// if validation fails
return Redirect::to(URL::route('showlogin'))->withInput();
showlogin method
$view->email = Input::flashOnly('email');
$view->password = Input::flashOnly('password');
return $view;
view page
<input type="text" value="{{ $email }}" name="email">
<input type="password" value="{{ $password }}" name="password">
The page does not fill the box with previous input when the validation fails. Have I missed something here?
Use Laravel form helper to create form inputs to remember old input
<input type="text" value="{{ $email }}" name="email">
<input type="password" value="{{ $password }}" name="password">
will be
{{Form::email('email', Input::old('email') ) }}
{{Form::text('password', Input::old('password')) }}
I know that this question is perfectly answered in FOSUserBundle documentation, but yet I can't solve my problem.
I am trying to make an own login page using FOSUserBundle.
I have a main.html.twig that is EXACTLY the same as the login.html.twig included in the FOSUserBundle source.
The only difference is that main.html.twig is in my own bundle structure and login.html.twig is in FOSUserBundle folder structure.
I reach both /login and /main. I resolve them and the render starts.
But when accessing /main, I get the following error:
Twig_Error_Runtime: Variable "csrf_token" does not exist in "AcmeStoreBundle:Main:main.html.twig" at line 5
The code is known by the FOSUserBundle users, but anyway I paste it here:
<form action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<label for="username">{{ 'security.login.username'|trans({}, 'FOSUserBundle') }}</label>
<input type="text" id="username" name="_username" value="" />
<label for="password">{{ 'security.login.password'|trans({}, 'FOSUserBundle') }}</label>
<input type="password" id="password" name="_password" />
<input type="checkbox" id="remember_me" name="_remember_me" value="on" />
<label for="remember_me">{{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}</label>
<input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}" />
The variable "csrf_token" is somehow not recognized from outside FOSUserBundle. Or something else that I am not getting.
Any clue over there?
Since you are not using FormBuilder, in your action that is responsible for rendering main.html.twig you should generate this token and pass it to the View.
$csrf = $this->get('form.csrf_provider'); //Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider by default
$token = $csrf->generateCsrfToken($intention); //Intention should be empty string, if you did not define it in parameters
You should pass $token as csrf_token variable to your View
Check also my answer to similar question