Reputation: 93
I have a problem with Symfony. I create my form and then add a submit button, but my form is always not valid. An when I click on the button it's seems that nothing happens... I try all the methods to see if there is error but there is not... I've already tried this solution to see if I have errors : Symfony2: my form returns false from isValid() but empty array for getErrors() from unique constraint condition. This is my php code :
public function addAction(Request $request) {
$user = new User();
// On ajoute les champs de l'entité que l'on veut à notre formulaire
$form = $this->get('form.factory')->createBuilder('form', $user)
//->setAction($this->generateUrl('testRegister'))
->add('email', 'text')
->add('name', 'text')
->add('firstname', 'text')
->add('login', 'text')
->add('password', 'password')
->add('save', 'submit')
->getForm()
;
// On fait le lien Requête <-> Formulaire
// À partir de maintenant, la variable $user contient les valeurs entrées dans le formulaire par le visiteur
$form->handleRequest($request);
if (!$form->isValid()) {
print_r('Form is not valid...');
print_r((string) $form->getErrors());
}
// On vérifie que les valeurs entrées sont correctes
if ($form->isValid()) {
print_r('It works !');
// On enregistre notre objet dans la base de donnée
$uri = "http://192.168.128.13:8081/DumontPerat/sharesite/person/";
//On encode les données
$data = array(
'name' => $user->name,
'firstname' => $user->firstname,
'login' => $user->login,
'password' => $user->password,
'email' => $user->email,
'isAdmin' => $user->isAdmin,
'status' => $user->status
);
$response = \Httpful\Request::post($uri)
->sendsJson()
->body($data)
->send();
// On redirige vers la page suivante
return $this->render('ShareSiteGeneralBundle:Default:registerEnd.html.twig');
}
// On passe la méthode createView() du formulaire à la vue
// afin qu'elle
// puisse afficher le formulaire toute seule
return $this->render('ShareSiteGeneralBundle:Default:register.html.twig', array(
'form' => $form->createView(),
));
}
And this is my view (twig) :
{%extends '::base.html.twig'%}
{%block title%}Registration{%endblock%}
{%block body%}
<h3>Registration</h3>
<div class="well">
{{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }}
{# Les erreurs générales du formulaire. #}
{{ form_errors(form) }}
<div class="form-group">
{# Génération du label. #}
{{ form_label(form.name, "Lastname :", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
{{ form_errors(form.name) }}
<div class="col-sm-4">
{# Génération de l'input. #}
{{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.firstname, "Firstname :", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
{{ form_errors(form.firstname) }}
<div class="col-sm-4">
{{ form_widget(form.firstname, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.email, "Email :", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
{{ form_errors(form.email) }}
<div class="col-sm-4">
{{ form_widget(form.email, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.login, "Login :", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
{{ form_errors(form.login) }}
<div class="col-sm-4">
{{ form_widget(form.login, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.password, "Password :", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
{{ form_errors(form.password) }}
<div class="col-sm-4">
{{ form_widget(form.password, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="form-group">
{{ form_widget(form.save, {'attr': {'class': 'btn btn-primary'}}) }}
</div>
</div>
{#<pre>{{ dump(form) }}</pre>#}
{{ form_end(form) }}
</div>
{% include 'ShareSiteGeneralBundle:Default:footer.html.twig' %}
{%endblock%}
Thanks a lot !
Upvotes: 1
Views: 2999
Reputation: 1922
Try to use submit() method instead of handleRequest
, so this way your form will be submitted.
Notice, if your Symfony version below 3.0 you can still pass $request
to the submit()
, but try to avoid this, by using, for example, json_decode($request->getContent(), true)
or something else because passing $request
to submit is deprecated.
And also, having business logic in the controller is very bad practice. Try to put your logic to the model instead (for example, you have to use services for this).
P.S. I don't see where you set action
and method
for your form.
Upvotes: 1
Reputation: 479
Change
print_r((string) $form->getErrors())
to
print_r($form->getErrors(true))
to see the contents of the error-array including errors from child elements.
UPDATE
Also you should check whether the form has been submitted or not before querying any results depending on that.
$form->handleRequest($request);
if ($form->isSubmitted())
{
if ($form->isValid()) {
// [...]
}
}
Upvotes: 0