Symfony2 - Wrong controller action called - symfony

I am doing a lot on the page and I think I am getting a conflict somewhere. Basically, my page initially shows an input and a blank div. When an input is provided and submitted, the page refreshed with the div full of data. The user can then select some of this data and finally submit again.
This is my view
{% block main %}
<div class="col-md-4">
<section class="panel panel-default">
<header class="panel-heading">
<h3 class="panel-title">Terminal</h3>
</header>
<div class="panel-body">
<form action="{{ path('NickAlertBundle_terminalSearch') }}" method="post" enctype="multipart/form-data" class="terminalForm" id="terminalForm">
<div class="row">
<div class="col-md-12">
<input type="text" class="addMargin" id="terminal_command" name="terminal_command" placeholder=">">
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-4">
<input type="submit" class="btn btn-default" id="terminal_submit" value="Submit">
</div>
</div>
</form>
</div>
</section>
</div>
<div class="col-md-8" id="terminal-window">
<table class="terminalAvailability">
{% if data is defined %}
<form action="{{ path('NickAlertBundle_terminalCreate') }}" method="post" enctype="multipart/form-data" class="terminalForm">
{% for info in data %}
<tr>
<td class="flightNumber">{{ info.flightNumber }}</td>
<td class="details">{{ info.from ~ info.to }}</td>
{% for seat, availability in info.seats %}
<td class="seatClass">
<label for="{{ seat }}">
<span>{{ seat ~ availability }}</span>
</label>
<input type="checkbox" id="{{ seat }}" name="seats[{{ info.flightNumber }}][]" style="display: none;" value="{{ seat }}" />
</td>
{% endfor %}
<td class="otherInfo">{{ info.other }}</td>
</tr>
{% endfor %}
<div class="row">
<div class="col-md-8 col-md-offset-4">
<input type="submit" class="btn btn-default" value="Submit">
</div>
</div>
</form>
{% endif %}
</table>
</div>
<div class="modal"></div>
{% endblock %}
The first div is the input and the second div is the div the data will be displayed, selected, and resubmitted.
I then have my controller actions
public function terminalAction()
{
return $this->render('NickAlertBundle:Page:terminal.html.twig');
}
public function terminalSearchAction(Request $request)
{
try {
$terminal_command = strtoupper($request->get('terminal_command'));
$error = array();
if (!$terminal_command) {
$error[] = "Please enter the Command";
}
if (count($error)) {
echo "There were errors adding the alert.\n\n";
foreach ($error as $item) {
echo $item . "\n";
}
die();
}
$uapiService = $this->container->get('alert_bundle.api_service');
$commandData = $apiService->terminalService($terminal_command);
return $this->render('NickAlertBundle:Page:terminal.html.twig', array(
'data' => $commandData,
));
}catch (Exception $e) {
}
}
public function terminalCreateAction(Request $request)
{
try {
foreach ($request->request->get('seats') as $row) {
foreach ($row as $seat) {
var_dump($seat);
}
}
return $this->render('NickAlertBundle:Page:terminal.html.twig');
}catch (Exception $e) {
}
}
And finally my routes
NickAlertBundle_terminal:
pattern: /terminal
defaults: { _controller: NickAlertBundle:Alert:terminal }
methods: [GET]
NickAlertBundle_terminalSearch:
pattern: /terminal
defaults: { _controller: NickAlertBundle:Alert:terminalSearch }
methods: [POST]
NickAlertBundle_terminalCreate:
pattern: /terminal
defaults: { _controller: NickAlertBundle:Alert:terminalCreate }
methods: [POST]
So the page is initially displayed fine. The user then enters some input, submits it, and the response data is then displayed in the div. So this means the first two routes work perfectly. With the data in the div, the user can select some data, and then submit it. However, when this data is submitted, they are displayed with
There were errors adding the alert. Please enter the Command
This error is for the second action, and should not have anything to do with the third action. The second form has its path set to NickAlertBundle_terminalCreate so why would it cross wires with the other action?
Thanks

NickAlertBundle_terminalCreate will always solve to NickAlertBundle_terminalSearch, because the RouteMatcher will always match the pattern /terminal + method POST to the first route with these rules.
Why not give NickAlertBundle_terminalSearch a pattern like /terminal/search?

Related

Removing form_start from twig views Symfony3.4

In order to customize the views of the new user page, I need to remove the elements {{ form_start(form) }} and {{ form_end(form) }} from my twig template new.html.twig. It lets the page unorganized. Unfortunately, when I delete it, the insert new user don't display.
here is my twig code new.html.twig :
<div class="content">
{{ form_start(form) }}
<form class="form-horizontal group-border-dashed" action="#" style="border-radius: 0px;">
<div class="form-group">
<label class="col-sm-3 control-label">
Username
</label>
<div class="col-sm-6">
{{ form_widget(form.username, { 'attr': {'class': 'form-control','placeholder':'Username'}} ) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">
EMAIL
</label>
<div class="col-sm-6">
{{ form_widget(form.email, { 'attr': {'class': 'form-control','placeholder':'EMAIL '}} ) }}
</div>
</div>
<--! and so on -->
{{ form_widget(form) }}
<input type="submit" value="Create" />
{{ form_end(form) }}
<ul>
<li>
Back to the list
</li>
</ul>
</form>
</div>
Is there a way to remove the form_start and form_end from my twig
have you consider using the options of form_start?
like this :
form_start(form, 'attr':{ 'class':'form-horizontal group-border-dashed', 'style': 'border-radius: 0px;'})
you'll be able to remove the <form> but keep a concitent templating
note that the style should'nt be in your html page but in your css file. If you're using bootstrap you have access to this :
<span class="rounded-0"></span>
PS : if you fully template your form, you won't have access to the individual errors of each form that are autogenerated by symfony. read this page for more info.
post edit
I have edited your question to see it more clearly and new questions arised.
why are you using the form_widget(form) if you create all the element by yourself ?
you forgot the form_rest(form)that create the hidden csrf token (essential for security) and finnaly if you want to keep your <ul> inside the form why closing the form with form_end(form) before ?

Symfony simple login

i to do login in my webpage about symfony documentation example and i have a little problem. It's work but work a second time, after i clear cache. In first time i catch error with your session time out or you disabled cookie.
it's base.html.twig
{% if is_granted('ROLE_USER') %}
<div class="content">
<div class="starter-template">
{% block body %}{% endblock %}
</div>
</div>
{% else %}
<div class="container">
<div class="row">
<div class="col-md-offset-5 col-md-3">
<div class="form-login">
<h4>Welcome back.</h4>
<form action="{{ path('login') }}" method="post">
<input type="text" name="_username" id="userName" class="form-control input-sm chat-input" placeholder="username" />
</br>
<input type="password" name="_password" id="userPassword" class="form-control input-sm chat-input" placeholder="password" />
</br>
<div class="wrapper">
<span class="group-btn">
<button type="submit" href="#" class="btn btn-primary btn-md">login <i class="fa fa-sign-in"></i></button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
{% endif %}
and login action:
/**
* #Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
I try return redirect in this action but nothing helps me.
If login to do in /login page it's work okey. But I want use with which construction how in base.html
Try work on dev mode.
1) Go on web directory
2) Open app.php file
3) Change the code inside app.php to
$kernel = new AppKernel('dev', true);
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
Remove from controller
/**
* #Route("/login", name="login")
*/
Finally try move route in routing file.
For example in routing.yml:
login:
path: /login
defaults: { _controller: YourNameBundle:NameYourController:login }

how to override ProfileFormType to edit users? symfony2

I'm working with symfony2.I'm using FOSUserBundle, so i want to override FormType of edit user to add name to builder. so how i should do it?
what i've did :
ProfileFormType :
<?php
namespace UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
class ProfileFormType extends BaseType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
// add your custom field
$builder->add('name')
->add('roles', 'collection', array(
'type' => 'choice',
'options' => array(
'choices' => array(
'ROLE_ADMIN' => 'Admin'))))
->add('image', new ImageType())
;
}
public function getName() {
return 'user_edit_profile';
}
}
services.yml
user.edit.form.type:
class: UserBundle\Form\Type\ProfileFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: user_edit_profile }
edit.html.twig
{% extends "UserBundle::layout.html.twig" %}
{% block body %}
<center> <h1> Modification de profile </h1> </center>
<aside class="col-sm-3">
<div class="panel panel-default">
<div class="panel-heading">Modification</div>
<div class="panel-body">
Veuillez remplir les champs
</div>
</div>
</aside>
<!--timeline-->
<section class="timeline col-sm-9">
<!--post Timeline-->
<div class="thumbnail thumbnail-post">
<!--caption-->
<div class="caption">
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="form-horizontal">
<div class="form-group">
{{ form_errors(form.name) }}
<div class="col-sm-9">
Name {{ form_widget(form.name, { 'attr': {'class': 'form-control', 'placeholder': 'form.name'|trans } }) }}
</div>
</div>
<div class="form-group">
{{ form_errors(form.email) }}
<div class="col-sm-9">
Email {{ form_widget(form.email, { 'attr': {'class': 'form-control', 'placeholder': 'form.email'|trans } }) }}
</div>
</div>
<div class="form-group">
{{ form_errors(form.username) }}
<div class="col-sm-9">
Pseudo {{ form_widget(form.username, { 'attr': {'class': 'form-control', 'placeholder': 'form.username'|trans } }) }}
</div>
</div>
<div class="form-group">
{{ form_errors(form.current_password) }}
<div class="col-sm-9">
Mot de passe actuelle {{ form_widget(form.current_password, { 'attr': {'class': 'form-control', 'placeholder': 'form.current_password'|trans } }) }}
</div>
</div>
</br>
{{ form_rest(form) }}
<div class="form-group">
<div class="col-md-4 col-sm-4 col-xs-12 col-md-offset-3">
<input class="btn btn-default submit" type="submit" value="{{ 'registration.submit'|trans }}">
</div>
</div>
</form>
</div> <!--#caption-->
<!--#post timeline-->
</div>
<!--#timeline-->
</section>
{% endblock %}
{% block js %}
<script>
$(document).ready(function () {
$('#roles').hide();
});
</script>
{% endblock %}
i'm getting this error :
Neither the property "name" nor one of the methods "name()",
"getname()"/"isname()" or "__call()" exist and have public access in
class "Symfony\Component\Form\FormView" in
FOSUserBundle:Profile:edit.html.twig at line 28.
How can i resolve that.
profile:
form:
type: fos_user_profile
It needs that at config.yml of fos
Update your service file:
user.edit.form.type:
class: UserBundle\Form\Type\ProfileFormType
arguments: [%fos_user.model.user.class%]
decorates: fos_user.profile.form.type
tags:
- { name: form.type, alias: user_edit_profile }
fos_user.profile.form.type is mean our name of service which will be replace our service that name user.edit.form.type
Add use Symfony\Component\Form\FormBuilderInterface;
You got error:
Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in FOSUserBundle:Profile:edit.html.twig at line 28.
due to fos_user_user table has not name field. If you want to add field whitch not locate in table you'll should add field those fields in entity generate getters and setters and finaly create new migration and migrate up.

Symfony2 getLastUsername() function not working

I'm creating my first Symfony2 login form and want to use the getLastUsername() function to set the value of the username textbox but when calling the function it simply returns an empty string.
This is my SecurityController.php file:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class SecurityController extends Controller
{
/**
* #Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'security/login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
/**
* #Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
}
}
?>
and my Twig template for the form:
{# app/Resources/views/security/login.html.twig #}
{% extends 'base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('css/login.css') }}" rel="stylesheet" />
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('js/login.js') }}"></script>
{% endblock %}
{% block body %}
<div id="container">
{% if error %}
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<div id="loginbox">
<form id="loginform" action="{{ path('login_check') }}" method="post">
<p>Enter username and password to continue.</p>
<div class="input-group input-sm">
<span class="input-group-addon"><i class="fa fa-user"></i></span><input class="form-control" type="text" name="_username" id="username" placeholder="Username" value="{{ last_username }}" />
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span><input class="form-control" type="password" name="_password" id="password" placeholder="Password" />
</div>
<div class="form-actions clearfix">
<div class="pull-left">
Create new account
</div>
<div class="pull-right">
Lost password?
</div>
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}" />
<input type="submit" class="btn btn-block btn-primary btn-default" value="Login" />
</div>
</form>
</div>
</div>
{% endblock %}
Everything works fine. Logged in and out many times but the las username isn't showing. Thought Symfony might be using cookies to save the last used username but it isn't creating any cookies, only the PHP session is saved in a cookie. Is it a problem in the cookie configurations or may be something else?
Thanks,

how to create a customized form

I'm working in a project and I would like to create form and not using form_widget. I want to create my own form.
Here is my controller:
public function newAction()
{
$entity = new Etudiant();
$form = $this->createCreateForm($entity);
return $this->render('MyAppRevBundle:Etudiant:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
And my html page is:
{% extends '::base.html.twig' %}
{% block body -%}
<form action="{{ path('etudiant') }}" {{form_enctype(form)}} method="post" >
<label for="username" class="col-md-2 control_label">Login :</label>
<input type="text" id="username" name="_username" value="Thaer"required="required" />
<br>
<input type="submit"/>
{{ form_rest(form) }}
</form>
<ul class="record_actions">
<li>
<a href="{{ path('etudiant') }}">
return
</a>
</li>
</ul>
{% endblock %}
so i solved
so it should be
{% extends '::base.html.twig' %}
{% block body -%}
<form action="{{ path('etudiant') }}" {{form_enctype(form)}} method="post" >
<table>
<tr>
<td>Libelle: </td>
<td>{{ form_widget(form.libelle)}}
</tr>
</table>
< input type="submit" />
{{ form_rest(form) }}
</form>
<ul class="record_actions">
<li>
<a href="{{ path('etudiant') }}">
return
</a>
</li>
{% endblock %}

Resources