I am experimenting with the implementation of ecommerce "DevAndClick". I cannot retrieve the delivery and billing addresses, and the session address[livraison] displays a null value. I am blocked. Has anyone ever met this problem that can help me?
Here is code of the form of the Twig view:
<form action="{{path('validation')}}" method="POST">
<h4>Adresse de livraison</h4>
{% for adresse in utilisateur.adresses %}
<label class="radio">
<input type="radio" name="livraison" value="{{adresse.id}}" {% if loop.index0 == 0 %} checked="checked" {% endif %} >
{{adresse.adresse}} , {{adresse.cp}} {{adresse.ville}}-{{adresse.pays}}<i class="icon-trash"></i> <br />
{{adresse.prenom}} {{adresse.nom}}
</label>
{% endfor %}
<br /><br />
<h4>Adresse de facturation</h4>
{% for adresse in utilisateur.adresses %}
<label class="radio">
<input type="radio" name="facturation" value="{{adresse.id}}" {% if loop.index0 == 0 %} checked="checked" {% endif %} >
{{adresse.adresse}} , {{adresse.cp}} {{adresse.ville}}-{{adresse.pays}}<i class="icon-trash"></i> <br />
{{adresse.prenom}} {{adresse.nom}}
</label>
{% endfor %}
<button class="btn btn-primary">Valider mes adresse </button>
</form>
Here are the two methods of my controller:
public function setLivraisonOnSession(Request $request)
{
$session = $request->getSession();
if(!$session->has('adresse')) $session->set('adresse',array());
$adresse = $session->get('adresse');
if($request->request->get('livraison') != null && $request->request->get('facturation') != null )
{
$adresse['livraison'] = $request->request->get('livrasion');
$adresse['facturation'] = $request->request->get('facturation');
}
else{
return $this->redirect($this->generateUrl('validation'));
}
$session->set('adresse',$adresse);
return $this->redirect($this->generateUrl('validation'));
}
public function validationAction(Request $request)
{
if($request->isMethod('POST'))
$this->setLivraisonOnSession($request);
$em = $this->getDoctrine()->getManager();
$session = $request->getSession();
$adresse = $session->get('adresse');
//var_dump($adresse['livraison']);
// die();
$produits = $em->getRepository('EcommerceBundle:Produits')->findArray(array_keys($session->get('panier')));
$livraison = $em->getRepository('EcommerceBundle:UtilisateursAdresses')->find($adresse['livraison']);
$facturation = $em->getRepository('EcommerceBundle:UtilisateursAdresses')->find($adresse['facturation']);
return $this->render('EcommerceBundle:Default/panier/layout:validation.html.twig',array(
'produits' => $produits,
'livraison'=> $livraison,
'facturation'=>$facturation,
'panier'=>$session->get('panier')
));
}
Confirm that you have initialized your variable $adresse as an array before using it, like below:
$adresse = array();
Related
In code migrated from Symfony 2.7 to 4.0, validation no longer happens on my form, allowing bad data to pass through and cause a Doctrine constraint violation
I'm new to Symfony and was asked to migrate a 2.7 application to 4.0. I did this in steps (2.7->2.8->3.x->4.0) and addressed issues as they came up, but one thing that broke along the way is automatic form validation. In the original version, if I attempted to create a new user and left the fields blank, it would correctly flag those and pop up " must not be empty" messages in the UI. Now, it lets those past until it attempts to write to the database, at which point Doctrine barfs because the database not null constraints are violated.
I've tried to figure out what I'm doing wrong, but I don't have a firm grasp on how the form creation process and syntax has changed. All of the example documentation on validation in forms assumes the createFormBuilder() approach, and all my existing code uses createForm(). What am I missing?
Here's part of the user object associated with the form showing the #Assert statements that I expect to trigger validation warnings:
/**
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="Domain\CoreBundle\Repository\UserRepository")
* #ORM\HasLifecycleCallbacks()
* #UniqueEntity(fields="email", message="This email address is already in usage")
* #UniqueEntity(fields="username", message="This username is already in usage")
*/
class User extends BaseUser implements JsonSerializable
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #Assert\NotBlank(message="Email should not be empty")
* #Assert\Email(strict=true)
* #Assert\Length(max=150, maxMessage="Email should be less than {{ limit }} characters")
*/
protected $email;
/**
* #Assert\NotBlank(message="Username should not be empty")
* #Assert\Regex(
* pattern = "/^\d*[a-zA-Z][ a-zA-Z0-9\!\#\#\$\%\^\&\-\_\=\+\~\?\.]*$/i",
* message = "Username should include at least one letter"
* )
*/
protected $username;
/**
* #var string
*
* #Assert\NotBlank(message="First name should not be empty")
* #ORM\Column(name="first_name", type="string", length=255)
*/
protected $firstName;
/**
* #var string
*
* #Assert\NotBlank(message="Last name should not be empty")
* #ORM\Column(name="last_name", type="string", length=255)
*/
protected $lastName;
(rest of code omitted for conciseness)
And here's the addNew action from the controller (AdministratorController extends UserController):
/**
* Add new administrator
*
* #param Request $request
*
* #return Response
*/
public function addNewAction(Request $request)
{
$company = $this->getCurrentCompany();
$form = $this->createForm(AddAdministratorType::class, null,
array('current_user'=> $this->user, 'restricted_admin'=>$this->getRestrictedAdmin(), 'company'=>$company));
if ($request->getMethod() == Request::METHOD_POST) {
$form->handleRequest($request);
// check if the user already exists
$userManager = $this->get('fos_user.user_manager');
$user = $form->getData();
$oldUser = $userManager->findUserByUsername($user['email']);
if ($oldUser)
{
$alreadyExists = false;
if ($user["isSuperAdmin"] &&$oldUser->isGrantedSuperAdmin())
$alreadyExists = true;
if ($user["isCompanyAdmin"] && $oldUser->isGranted(UserRepository::ROLE_COMPANY_ADMIN, $company))
$alreadyExists = true;
if (!$user["isCompanyAdmin"] && !$user["isSuperAdmin"] && $oldUser->isGranted(UserRepository::ROLE_ADMIN,$company))
$alreadyExists = true;
if ($alreadyExists)
$form->get('email')->addError(new FormError('This email address is already in use'));
}
if ($form->isValid()) {
$user = $form->getData();
if ($oldUser) // if the user already exists, we just need to add the role
{
if (!$this->getUser()->isGrantedSuperAdmin() &&
!in_array($company->getId(), array_map(function($x){return $x->getId();}, $oldUser->getCompaniesWithRole())))
{
// the user isn't currently in this company and the user adding the role
// isn't a super admin, so we have to create a shadow user entry to hide
// the real user info from other in the company until the user logs into
// the company
$oldShadow=$this->em->getRepository(ShadowUser::class)->findOneBy(array("user" => $oldUser, "company"=>$company));
if (!$oldShadow)
{
$shadow = new ShadowUser();
$shadow->setUser($oldUser);
$shadow->setFirstName($user["firstName"]);
$shadow->setLastName($user["lastName"]);
$shadow->setCompany($company);
$shadow->setIsVydioUsed($user["isVydioUsed"]);
$shadow->setVydioRoomLink($user["vydioRoomLink"]);
$shadow->setCreatedDate(new \DateTime());
$this->em->persist($shadow);
}
}
if ($user["isSuperAdmin"])
{
$oldUser->addMyRole(UserRepository::ROLE_SUPER_ADMIN, $company);
$this->get('pp_mailer')->onAddNewRole($oldUser,UserRepository::ROLE_SUPER_ADMIN, $company );
}
if ($user["isCompanyAdmin"])
{
$oldUser->addMyRole(UserRepository::ROLE_COMPANY_ADMIN, $company);
$this->get('pp_mailer')->onAddNewRole($oldUser,UserRepository::ROLE_COMPANY_ADMIN, $company );
}
if (!$user["isSuperAdmin"] && !$user["isCompanyAdmin"])
{
$oldUser->addMyRole(UserRepository::ROLE_ADMIN, $company);
$this->get('pp_mailer')->onAddNewRole($oldUser,UserRepository::ROLE_ADMIN, $company );
}
$programRepo = $this->em->getRepository(ProgramUser::class);
foreach($user["programs"] as $program)
{
$oldRelation = $programRepo->findOneBy(array("user"=> $oldUser, "program"=>$program));
if (!$oldRelation)
{
$relation = new ProgramUser();
$relation->setUser($oldUser);
$relation->setProgram($program);
$relation->setCompany($company);
$this->em->merge($relation);
}
}
$this->em->persist($oldUser);
$this->em->flush();
}
else
{
$newUser = new User();
$newUser->setPassword($this->get('domain_core_service')->generatePassword());
$newUser->setDefaultCompany($company);
$newUser->setFirstName($user["firstName"]);
$newUser->setLastName($user["lastName"]);
$newUser->setEmail($user["email"]);
$newUser->setUsername($user["email"]);
$newUser->setEnabled($user["enabled"]);
$newUser = $this->em->getRepository('DomainCoreBundle:User')->addUserInSystem($userManager, $newUser);
$token = $this->get('domain_core_service')->generateToken();
$newUser->setConfirmationToken($token);
if ($user["isSuperAdmin"])
{
$newUser->addMyRole(UserRepository::ROLE_SUPER_ADMIN, $company);
$this->get('pp_mailer')->onAddNewUser($newUser,UserRepository::ROLE_SUPER_ADMIN, $company );
}
if ($user["isCompanyAdmin"])
{
$newUser->addMyRole(UserRepository::ROLE_COMPANY_ADMIN, $company);
$this->get('pp_mailer')->onAddNewUser($newUser,UserRepository::ROLE_COMPANY_ADMIN, $company );
}
if (!$user["isSuperAdmin"] && !$user["isCompanyAdmin"])
{
$newUser->addMyRole(UserRepository::ROLE_ADMIN, $company);
$this->get('pp_mailer')->onAddNewUser($newUser,UserRepository::ROLE_ADMIN, $company );
}
foreach($user["programs"] as $program)
{
$relation = new ProgramUser();
$relation->setUser($newUser);
$relation->setProgram($program);
$relation->setCompany($company);
$this->em->merge($relation);
}
$this->em->persist($newUser);
$this->em->flush();
}
return $this->redirect($this->generateUrl('domain_admin_show_all_administrators_page'));
}
}
return $this->render(
'DomainAdminBundle:Administrators:add-new.html.twig',
array(
'form' => $form->createView(),
'page_title' => 'Add New Administrator',
'currentSidebar' => $this->currentSideBar,
'currentSidebarItem' => $this->currentSidebarItem,
)
);
}
And the twig file for the form:
{% extends 'DomainAdminBundle::base-admin-layout.html.twig' %}
{% import '::widgets/form_errors.html.twig' as form_custom_errors %}
{% import '::widgets/label.html.twig' as form_custom_labels %}
{% block title %} My Application| {{ page_title }} {% endblock %}
{% block javascripts %}
{{ parent() }}
<script type="text/javascript" src="{{ asset('assets/scripts/admin-add-new.js') }}"></script>
{% endblock %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" type="text/css" href="{{ asset('assets/styles/admin-add-new.css') }}">
{% endblock %}
{% block admin_main_content %}
<div class="content-block administrator-controller" ng-controller="AdministratorController">
<div class="content-title-bar">
<div class="pull-left">
<h2>{{ page_title }}</h2>
</div>
</div>
<div class="content-block" ng-controller="AdminController">
{{ form_start(form, {"attr": { "action":"{{ path('domain_admin_add_new_administrator_page') }}", 'enctype': 'multipart/form-data', "method":"POST", "novalidate":"novalidate", "autocomplete":"off", "class":"form-horizontal add-user", "ng-submit":"disableAddButton()" }}) }}
<div class="base-box info-block">
<div class="control-group">
<div class="controls">
{{ form_widget(form.enabled) }}
{{ form_label(form.enabled, 'Active') }}
</div>
</div>
{% if app.user.isGrantedSuperAdmin() %}
<div class="control-group">
<div class="controls">
{% set companyAdminValue = form.isCompanyAdmin.vars.checked ? 'true' : 'false' %}
{{ form_widget(form.isCompanyAdmin, { 'attr':{ 'ng-model':'adminForm.isCompanyAdmin', 'ng-init': 'adminForm.isCompanyAdmin=' ~ companyAdminValue } }) }}
{{ form_label(form.isCompanyAdmin, 'Company Admin') }}
{% set superAdminValue = form.isSuperAdmin.vars.checked ? 'true' : 'false' %}
{{ form_widget(form.isSuperAdmin, { 'attr':{ 'ng-model':'adminForm.isSuperAdmin', 'ng-init': 'adminForm.isSuperAdmin=' ~ superAdminValue } }) }}
{{ form_label(form.isSuperAdmin, 'Super Admin') }}
</div>
</div>
{% endif %}
<div class="control-group" ng-init="initMultiSelect(true)">
{{ form_custom_labels.widget(form.programs) }}
<div class="controls">
{{ form_widget(form.programs) }}
{{ form_custom_errors.widget(form.programs) }}
</div>
</div>
<div class="control-group">
{{ form_custom_labels.widget(form.firstName) }}
<div class="controls">
{{ form_widget(form.firstName) }}
{{ form_custom_errors.widget(form.firstName) }}
</div>
</div>
<div class="control-group">
{{ form_custom_labels.widget(form.lastName) }}
<div class="controls">
{{ form_widget(form.lastName) }}
{{ form_custom_errors.widget(form.lastName) }}
</div>
</div>
<div class="control-group">
{{ form_custom_labels.widget(form.email) }}
<div class="controls">
{{ form_widget(form.email) }}
{{ form_custom_errors.widget(form.email) }}
</div>
</div>
<div class="control-group">
{{ form_custom_labels.widget(form.timezone) }}
<div class="controls">
{{ form_widget(form.timezone) }}
{{ form_custom_errors.widget(form.timezone) }}
</div>
</div>
</div>
<div class="text-right">
<button id="add-admin-submit" type="submit" class="btn btn-do" ng-disabled="isDisabled">Add new administrator</button>
Cancel
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
</div>
{% endblock %}
If I leave all fields blank and click "Add New Administrator" it doesn't flag them as blank, instead passing them onto Doctrine. The expected behavior is that it flags them at the UI and doesn't attempt to write them to the database.
I'm sure I've created multiple crimes against Symfony as I've ramped up the learning curve, so go easy. Right now I'm just trying to address this narrow issue; refactoring to more elegantly fit Symfony 4 will have to wait for another day.
Thanks!
Looks like you want to validate the User class against the data from your request.
Have you set data_class option in your form type class?
It's required if you want to use validation rules from another class (as you marked your properties with some #Assert* annotations).
https://symfony.com/doc/current/forms.html#creating-form-classes
Another way to do validation is to choose validation rules right in your FormType.
I have this form in twig, and I want to get the selected option and the value of the input field from a simple html form:
(PS: Don't say 'You'd better generate a form using the controller!' I know that, but I have a reason why I want to create a form in twig: because that allows me to create as many forms as I want using a for loop.)
I tried to pass arguments in the action path, but that didn't work.
<form action="{{ path('changeProf') }}" method="post" id="form">
<section class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="position: relative; left: 35%;top: 6vmin">
<label style="display:inline-table;">
<span> <input type="text" value="{{ user.id }}" disabled="true" id="iduser"style="max-width: 18vmin;"/></span>
</label>
</section>
<section class="col-lg-9 col-md-9 col-sm-9 col-xs-9"style="position: relative; left: 35%;top: 6vmin">
<label style="display:inline-table;">
<span>{% set k=1 %}
<select id="profil">
{% for prof in profArr %}
<option value="{{ prof }}"> {{ prof }} </option>
{% endfor %}
</select>
</span>
</label>
</section>
<input type="submit" class="btn btn-info btn-xs" style="position: relative;top:18vmin;left:-28%">
</form>
This is the action that handles the form :
/**
* #Route("/profile/chProf",name="changeProf")
* Method ("POST")
* #Template()
*/
public function changeProfAction(Request $request)
{
$session = new Session();
$session->start();
$search = $session->get('user');
$gestAcces = $session->get('acces');
$gestEtat = $session->get('etatUser');
$gestCont = $session->get('contenu');
$repMsg = $session->get('repMsg');
$gestRec = $session->get('Reclam');
$gestMess = $session->get('gestMess');
$gestMp = $session->get('gestMp');
if ($search == Null) {
return $this->redirectToRoute('empty', array('search' => $search,
'contenu' => $gestCont,
'gestAcces' => $gestAcces,
'gestEtat' => $gestEtat,
'repMsg' => $repMsg,
'gestRec' => $gestRec,
'gestMess' => $gestMess,
'gestMp' => $gestMp,
));
}
$em = $this
->getDoctrine()
->getManager();
$reposit = $em->getRepository("CNAMCMSBundle:userprof");
$rep = $em->getRepository("CNAMCMSBundle:profil");
$userprof=new userprof();
$libprof=$request->request->get('profil');
$iduser=$request->request->get('iduser');
$idprof=$rep->findOneBy(array('libelle'=>$libprof));
$userprof->setIdUser($iduser);
$userprof->setIdProfil($idprof);
$em->persist($userprof);
$em->flush();
return $this->render('CNAMCMSBundle:Default:userProf.html.twig', array(
'search'=>$search,
'contenu'=>$gestCont,
'gestAcces'=>$gestAcces,
'gestEtat'=>$gestEtat,
'repMsg'=>$repMsg,
'gestRec'=>$gestRec,
'gestMess'=>$gestMess,
'gestMp'=>$gestMp,
));
}
I think I found out what caused the error you receive.
$request->request->get('profil'); returns NULL.
This means, the form did not send a profil entry.
Look where is the profil in the form:
<input type="text" value="{{ user.id }}" disabled="true" id="iduser"style="max-width: 18vmin;"/>
There is no name attribute! Which is actually what is sent with the request. The name attribute, not the id. The id is used only for local styles and javascripts.
The solution:
<input type="text" value="{{ user.id }}" disabled="true" id="iduser" name="iduser" style="max-width: 18vmin;"/>
Do the same for profil
Hope this helps.
After filtering for a particular criteria, display total of the sales transaction amounts on top of the page. This should be total of all the pages if pages are more than one.
Can someone guide me how to do this
We did it in the following manner and it is working like a charm
Step 1 : Added two methods in orderAdmin
public function getTemplate($name)
{
if($name == 'list') {
return 'AppBundle:OrderAdmin:list.html.twig';
}
return parent::getTemplate($name); stub
}
public function getSumOf($field)
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select('SUM( ' . $query->getRootAlias() . '.' . $field . ') as total');
$query->setFirstResult(null);
$query->setMaxResults(null);
$result = $query->execute(array(), \Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);
return $result;
}
Step 2: Created a template file list.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
{% block list_header %}
<div class="pull-right" style="margin-right: 10px;">
<label for="{{ admin.uniqid }}_sum_of_orders" class="control-label">Grand Total: </label>
<label class="control-label"> S$ {{ admin.getSumOf('orderAmount') }} </label>
</div>
{% endblock %}
I have the following controller:
<?php
namespace Dev\TaskBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use Dev\TaskBundle\Entity\User;
class UserController extends Controller
{
/**
* Redirects to directory where you can input new password
*
* #Route("/{id}", name="user_update")
*/
public function userUpdatePanel($id)
{
$user = $this->getDoctrine()->getRepository('DevTaskBundle:User')->find($id);
return $this->render('DevTaskBundle:User:userUpdate.html.twig', array('user' => $user));
}
/**
* Updates password of the user
*
* #Route("/updatePassword", name="updatePass_action")
*/
public function passUpdateAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$id = $request->get('id');
$user = new User();
$user = $em->getRepository('DevTaskBundle:User')->find($id);
$password = $request->get('passInput');
$user->setPassword($password);
$em->flush();
$users = $this->getDoctrine()->getRepository('DevTaskBundle:User')->findAll();
return $this->render('DevTaskBundle:User:accounts.html.twig', array('users' => $users));
}
}
Here is form from userUpdate.html.twig
<form action="{{ path('updatePass_action') }}" method="POST">
<div>
Username: {{ user.username }}
</div>
<input type="hidden" name="id" value="{{user.id}}">
<div class="form-group">
<label for="passInput">Password:</label>
<input type="text" class="form-control" id="passInput" name="passInput" value="{{ user.password }}">
</div>
<button type="submit" class="btn btn-default">Change</button>
</form>
and when I proceed to change the password I receive such error
Impossible to access an attribute ("username") on a null variable in DevTaskBundle:User:userUpdate.html.twig at line 23
After changing the password I want to render accounts.html.twig so why there is the error about userUpdate? What is wrong here?
Part of accounts.html.twig with twig scripts:
<table class="table table-bordered">
<thead>
<tr>
<th>Login</th>
<th>Hasło</th>
<th>Narzędzia</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>
{{ user.username }}
</td>
<td>
{{ user.password }}
</td>
<td>
<a href="{{ path('user_update', {'id': user.id}) }}" >
<span class="glyphicon glyphicon-pencil linkDeco" ></span>
</a>
{% if user.status == 1 %}
<a href="{{ path('deleteAction', {'name': 'User' ,'direction': 'account_panel' , 'id': user.id}) }}" >
<span class="glyphicon glyphicon-trash linkDeco"></span>
</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
EDIT
problem must be related to userUpdate.html.twig because when I deleted
<div>
Username: {{ user.username }}
</div>
I have the error
Impossible to access an attribute ("id") on a null variable in DevTaskBundle:User:userUpdate.html.twig at line 24
Any idea why is that?
Doctrine find method return array of objects, use findOne instead of find when you expecting only one result.
$user = $this->getDoctrine()->getRepository('DevTaskBundle:User')->findOneById($id);
In the show page i whant add order quantity in my session.
This is my action
/**
* #Route("/add/{id}", name="add_action")
* #param Request $request
* #param $id
* #return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function addAction(Request $request, $id)
{
$session = $request->getSession();
$basket = $session->get('basket');
$key = array_search($id, $basket);
if(!$key)
{
$basket[$key] = $request->query->get('item');
$session->set('basket', $basket[$key]);
}
return $this->redirectToRoute('basket_action');
}
In this action i have error Warning: array_search() expects parameter 2 to be array, null given. What can be done to make it work?
P.S.
part of my template
<form method="get" action="{{ path('add_action', {'id' : product.id}) }}">
<select class="form-control" name="item">
{% for i in 1..10 %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
<button class="btn btn-success" >Add to basket</button>
</form>
change
$basket = $session->get('basket');
to
$basket = $session->get('basket', array());
Looks like you are not storing basket as an array on session correctly, I would need to see your code where stores it. You can add a check if is an array before using array_search to prevent this error