Софтуерно Инженерство
Loading...
+ Нов въпрос
Martina_Shebova avatar Martina_Shebova 10 Точки

Грешка при опит за регистрация на играч

Здравейте, получавам следната грешка въпреки, че всичко по кода ми изглежда наред.... само паролата я взема от формата, username и fullname са NULL :(

An exception occurred while executing 'INSERT INTO players (username, fullName, password) VALUES (?, ?, ?)' with params [null, null, "$2y$13$UhruC9ZnQclfKZyNrSiFMOVJjNpp.fF\/uvTFrNPcTFlZDbHbWw5ae"]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'username' cannot be null.

Тагове:
0
PHP Web Development 11/12/2016 18:00:49
krasimir.i.petrov avatar krasimir.i.petrov 10 Точки

Само с кода на грешката няма как да ти кажем къде бъркаш, дай кода от контролера и формата.

0
Martina_Shebova avatar Martina_Shebova 10 Точки

Контролер:

<?php

namespace MarsBattleGroundBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use MarsBattleGroundBundle\Entity\Player;
use MarsBattleGroundBundle\Form\PlayerType;
use MarsBattleGroundBundle\Repository\PlayerRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class PlayerController extends Controller
{
    /**
     * @Route("/register", name="user_register")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function registerAction(Request $request)
    {
        // 1) build the form
        $user = new Player();

        $form = $this->createForm(PlayerType::class, $user);

        // 2) handle the submit (will only happen on POST)
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            // 3) Encode the password (you could also do this via Doctrine listener)
            $password = $this->get('security.password_encoder')
                ->encodePassword($user, $user->getPassword());
            $user->setPassword($password);

            // 4) save the User!
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            // ... do any other work - like sending them an email, etc
            // maybe set a "flash" success message for the user

            return $this->redirectToRoute('security_login');
        }

        return $this->render(
            'user/register.html.twig',
            array('form' => $form->createView())
        );
    }



    /**
     * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
     * @Route("/profile", name="user_profile")
     */
    public function profileAction()
    {
        $user = $this->getUser();
        return $this->render("user/profile.html.twig", ['user'=>$user]);
    }
}

 

Форма:

{% extends 'base.html.twig' %}

{% block body_id 'register' %}

{% block main %}
    <div class="container body-content span=8 offset=2">
        <div class="well">
            <form class="form-horizontal" action="{{ path('user_register') }}" method="post">
                <fieldset>
                    <legend>Login</legend>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="user_username">Username</label>
                        <div class="col-sm-4 ">
                            <input class="form-control" id="user_username" placeholder="Username" name="user[username]" required type="text">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="user_fullName">Full Name</label>
                        <div class="col-sm-4 ">
                            <input type="text" class="form-control" id="user_fullName" placeholder="Full Name" name="user[fullName]" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="user_password_first">Password</label>
                        <div class="col-sm-4">
                            <input type="password" class="form-control" id="user_password_first" placeholder="Password" name="user[password][first]" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="user_password_second">Confirm Password</label>
                        <div class="col-sm-4">
                            <input type="password" class="form-control" id="user_password_second" placeholder="Password" name="user[password][second]" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-4">
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </div>

                    {{  form_row(form._token) }}
                </fieldset>
            </form>
        </div>
    </div>
{% endblock %}
0
krasimir.i.petrov avatar krasimir.i.petrov 10 Точки

А би ли постнала Entity на плеъра, формата и контролера изглеждат валидни.

0
Martina_Shebova avatar Martina_Shebova 10 Точки

Добавих:

public function registerAction(Request $request)
{
    // 1) build the form
    $user = new Player();
    $form = $this->createForm(PlayerType::class, $user);

    // 2) handle the submit (will only happen on POST)
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        // 3) Encode the password (you could also do this via Doctrine listener)
        $password = $this->get('security.password_encoder')
            ->encodePassword($user, $user->getPassword());
        $user->setPassword($password);
$getRequest = $request->request->get('user');

$userName = $getRequest['username'];
$fullName = $getRequest['fullName'];

$user->setUsername($userName);
$user->setFullName($fullName);
    $em = $this->getDoctrine()->getManager();
    $em->persist($user);
    $em->flush();

    return $this->redirectToRoute('security_login');
}

И тръгна :)

0