Symfony - Form is not submit - forms

I work with Symfony to build and send a contact form by mail.
I use swiftMailer.
The problem is that my form is not submitted.
So, my swiftmailer can't work because the isSumbitted will stay at false and $mailer->send(message) will not be activate
My controller :
public function index (Request $request, Swift_Mailer $mailer)
{
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$contactData = $form->getData();
$name = $contactData['name'];
$content = $contactData['message'];
$email = $contactData['email'];
$entreprise = $contactData['entreprise'];
$zip = $contactData['zip'];
$message = (new Swift_Message('Vous avez un nouveau message sur le site AMC Industrie.fr'))
->setFrom('test#example.com')
->setTo('test#gmail.com')
->setSubject('Demande d\'information')
->setCharset('utf-8')
->setContentType('text/html')
->setBody(
"
<p> Vous avez reçu un message de <strong>$name</strong>, de l'entreprise <strong>$entreprise</strong>
<br>(code postal : $zip ) <br>
Son email est : <strong>$email</strong>. </p>
<p> Son message :</p>
<p>$content
</p>
"
)
;
$mailer->send($message);
return $this->redirectToRoute('map_contact');
}
return $this->render('contact/map_contact.html.twig', [
'form' => $form->createView()
]);
}
My Form :
{
$builder
->add('name',TextType::class,
['label'=>false,
'constraints' => new NotBlank,
'attr' => [
'class'=> 'form-control',
'placeholder' => 'Nom et prénom',
]
])
->add('email',EmailType::class,
['label'=>false,
'constraints' => new NotBlank,
'attr' => [
'class' => 'form-control',
'placeholder' => 'adresse e-mail'
]
])
->add('entreprise',TextType::class,
['label'=>false,
'constraints' => new NotBlank,
'attr' => [
'class'=> 'form-control',
'placeholder' => 'Nom de votre entreprise'
]
])
->add('zip',NumberType::class,
['label'=>false,
'constraints' => new NotBlank,
'attr' => [
'class'=> 'form-control',
'placeholder' => 'Code postal'
]
])
->add('message',TextareaType::class,
['label'=>false,
'constraints' => new NotBlank,
'attr' => [
'class'=> 'form-control',
'placeholder' => 'Décrivez-nous votre projet'
]
])
->add('save', SubmitType::class,
['label' => 'Envoyer',
'attr'=> [
//'class' => 'waves-effect waves-light btn-large btn btn-primary mt-1 mb-5 lift',
'id' => 'form-submit'
]])
;
}
My template :
<form>
{{ form_start(form) }}
<div class="row">
<div class="col-12 col-md-6">
<div class="form-group mb-5">
{{form_label(form.name, 'Nom et prénom')}}
{{form_widget(form.name)}}
</div>
</div>
<div class="col-12 col-md-6">
<div class="form-group mb-5">
{{form_label(form.email, 'Email') }}
{{form_widget(form.email)}}
</div>
<div class="col-12 col-md-6">
<div class="form-group mb-5">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-md-6">
<div class="form-group mb-5">
{{form_label(form.entreprise, 'Entreprise')}}
{{form_widget(form.entreprise)}}
</div>
</div>
<div class="col-12 col-md-6">
<div class="form-group mb-5">
{{form_label(form.zip, 'Code Postal')}}
{{form_widget(form.zip)}}
</div>
<div class="col-12 col-md-6">
<div class="form-group mb-5">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="form-group">
{{form_label(form.message, 'Comment pouvons-nous vous aider ?')}}
{{form_widget(form.message)}}
</div>
</div>
</div> <!-- / .row -->
<div class="row justify-content-center">
<div class="col-auto py-5">
<!-- Submit -->
<div>
{{form_widget(form.save)}}
</div>
</div>
</div>
{{ form_end(form) }}
</form>
My debug tool :
Form in get parameter OK. I have all my input with informations.
Debug tool - get parameters:
BUT
there is not sumbitted data. form is not submit
debug tool - form is not submit:
I don't understand where is my problem.

Ok, it's resolved !
The problem : I already have a html balise for my form, in my template.
When I delete , it's ok !

Related

Symony: Render a form for each element

I'm trying to render an edit form in a modal for each element on the same page I create them, but it seems not possible to render a same form multiple times.
This is my controller:
/**
* #Route("/outcome", name="outcome")
*/
public function index(OutcomeRepository $repo, Request $request, EntityManagerInterface $manager): Response
{
$outcomes = $repo->findAll();
$outcome = new Outcome();
$form = $this->createForm(OutcomeType::class, $outcome);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$manager->persist($outcome);
$manager->flush();
$this->addFlash(
'success',
"Income <strong>{$outcome->getTitle()}</strong> has been added."
);
return $this->redirectToRoute('outcome');
}
return $this->render('outcome/outcome.html.twig', [
'outcomes' => $outcomes,
'form' => $form->createView(),
]);
}
This is the OutcomeType:
class OutcomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => "Title",
'attr' => [
'placeholder' => "Write a title"
]
])
->add('amount', MoneyType::class, [
'label' => "Amount",
'attr' => [
'placeholder' => "Write an amount"
]
])
->add('save', SubmitType::class, [
'label' => 'Send',
'attr' => [
'class' => 'btn btn-primary'
]
]);
}
And this is the twig part:
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#newOutcomeModal">
New outcome
</button>
<div class="modal fade" id="newOutcomeModal" tabindex="-1" aria-labelledby="newOutcomeModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newOutcomeModal">Add a new outcome</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{{form_start(form)}}
{{form_widget(form)}}
{{form_end(form)}}
</div>
</div>
</div>
</div>
{% for outcome in outcomes %}
<div class="card m-4">
<div class="card-body">
<h5 class="card-title">{{outcome.title}}</h5>
<p class="card-text">{{outcome.amount}}</p>
<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#editOutcomeModal{{outcome.id}}">Edit</button>
</div>
</div>
<div class="modal fade" id="editOutcomeModal{{outcome.id}}" tabindex="-1" aria-labelledby="editOutcomeModal{{outcome.id}}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editOutcomeModal{{outcome.id}}">Edit {{outcome.title}} outcome</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{{form_start(form)}}
{{form_widget(form)}}
{{form_end(form)}}
</div>
</div>
</div>
</div>
{% endfor %}
I get the error: "An exception has been thrown during the rendering of a template ("Field "outcome" has already been rendered, save the result of previous render call to a variable and output that instead.")."
I don't know how to solve this. I tried to turn the fields of OutcomeType in a collectionType but it didn't work.
Can someone help me please ?

This result is a forward only result set, calling rewind() after moving forward is not supported

i want to iterate à table to view tables data 2 time (in listbox and dropdown boutton), when i use one foreach for 1 inout it's ok, but when i use 2 foreach for 2 inputs i hade the bellow error i searched, i found that i must use $resultSet->buffer(); whn i replace it it my table the same eror stay:
File:
C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-db\src\Adapter\Driver\Pdo\Result.php:182
Message:
This result is a forward only result set, calling rewind() after moving forward is not supported
Stack trace:
#0 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-db\src\ResultSet\AbstractResultSet.php(241): Zend\Db\Adapter\Driver\Pdo\Result->rewind()
#1 C:\xampp\htdocs\gestion-commercial-03\module\Documentachat\view\documentachat\documentachat\index.phtml(138): Zend\Db\ResultSet\AbstractResultSet->rewind()
#2 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-view\src\Renderer\PhpRenderer.php(506): include('C:\\xampp\\htdocs...')
#3 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-view\src\View.php(207): Zend\View\Renderer\PhpRenderer->render(NULL)
#4 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-view\src\View.php(236): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#5 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-view\src\View.php(200): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel))
#6 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-mvc\src\View\Http\DefaultRenderingStrategy.php(105): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#7 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))
#8 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-eventmanager\src\EventManager.php(490): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#9 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-eventmanager\src\EventManager.php(248): Zend\EventManager\EventManager->triggerListeners('render', Object(Zend\Mvc\MvcEvent))
#10 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-mvc\src\Application.php(384): Zend\EventManager\EventManager->triggerEvent(Object(Zend\Mvc\MvcEvent))
#11 C:\xampp\htdocs\gestion-commercial-03\vendor\zendframework\zend-mvc\src\Application.php(356): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent))
#12 C:\xampp\htdocs\gestion-commercial-03\public\index.php(29): Zend\Mvc\Application->run()
#13 {main}
Table :
<?php
///=======================DocumentachatTable factory=========================
namespace Documentachat\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\TableGateway\TableGateway;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
class DocumentachatTable
{
protected $tableGateway;
private $lastinserted;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll($paginated = false)
{
if ($paginated) {
// create a new Select object for the table album
$select = new Select('achat__document');
// create a new result set based on the Album entity
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Documentachat());
// create a new pagination adapter object
$paginatorAdapter = new DbSelect(
// our configured select object
$select,
// the adapter to run it against
$this->tableGateway->getAdapter(),
// the result set to hydrate
$resultSetPrototype->buffer();
$resultSetPrototype
);
$select->where(array('achat__document.deleted' => 1));
$paginator = new Paginator($paginatorAdapter);
return $paginator;
}
$select = $this->tableGateway->getSql()->select();
$select->where(array('achat__document.deleted' => 1));
$results = $this->tableGateway->selectWith($select);
return $results;
}
public function lastInserted()
{
return $this->lastinserted;
}
public function getDocument($id)
{
$id = (int)$id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function getLastCount($idType)
{
$idType = (int)$idType;
$select = $this->tableGateway->getSql()->select();
$select->where->equalTo('id_type', $idType);
$select->order('count desc');
$rowset = $this->tableGateway->selectWith($select);
$row = $rowset->current();
if (!$row) {
return 0;
}
return $row;
}
public function saveDocument(Documentachat $document)
{
$data = array(
'reference' => $document->reference,
'id_type' => $document->idType,
'id_fournisseur' => $document->idFournisseur,
'address' => $document->address,
'deleted' => 1,
'date_doc' => $document->dateDoc
);
$id = (int)$document->id;
if ($id == 0) {
$data['count'] = $this->getLastCount($document->idType)->count + 1;
$this->tableGateway->insert($data);
$this->lastinserted = $this->tableGateway->getLastInsertValue();
} else {
if ($this->getDocument($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Documentachat id does not exist');
}
}
}
public function deleteDocument($id)
{
$data = array(
'deleted' => 0,
);
$this->tableGateway->update($data, array('id' => $id));
}
public function searchDocument($query)
{
$select = $this->tableGateway->getSql()->select();
$select->where(array('achat__document.deleted' => 1));
$select->where->like('achat__document.reference', '%' . $query . '%');
$results = $this->tableGateway->selectWith($select);
return $results;
}
View:
<?php
$title = 'Documentachat';
$this->headTitle($title);
?>
<div class="container-fluid container-fullw bg-white ng-scope">
<!-- ======partie haut-->
<div class="panel border-dark panel-white bg-white margin-top-20" style="box-shadow: 0 3px 6px rgb(0 0 0 / 16%), 0 3px 6px rgb(0 0 0 / 23%);">
<div class="panel-body Shadows-2 ">
<!-- =========== inputs filtre-->
<div class="row">
<div class="col-md-2 form-group">
<label> Date début </label>
<input type="date" id="dateMinI" class="form-control" name="dateMinI">
</div>
<div class="col-md-2 form-group">
<label> Date fin </label>
<input type="date" id="dateMaxI" class="form-control" name="dateMaxI" >
</div>
<div class="col-md-2 form-group">
<label> N° Document </label>
<input type="text" id="Reference" class="form-control" name="Reference" value autocomplete="off">
</div>
<div class="col-md-2 form-group">
<label> Fournisseur </label>
<input type="text" id="fournisseur" class="form-control" name="fournisseur" >
</div>
<div class="col-md-2 form-group">
<label> Document Type </label>
<select id="doctype" class="form-control" name="doctype">
<option value="">---</option>
<?php foreach ($this->docTypes as $item) : ?>
<option value=<?php echo $item->id; ?> > <?php echo $item->label; ?> </option>
<?php endforeach;
?>
</select>
</div>
<div class="col-md-2">
<div class=" padding-20">
<button type="button" id="sendid" class="btn btn-primary btn-block">
<i class="fa fa-filter" aria-hidden="true"></i>
OK
</button>
</div>
</div>
</div>
<!-- =========== inpouts filtre-->
<div class="row padding-20 bg-light-grey">
<button type="button" data-toggle="modal" data-target="#transformerModal" id="transformer"
class="btn btn-sm btn-default">
<i class="fa fa-arrow-right" aria-hidden="true"></i>
Transformer
</button>
<div class="btn-group pull-right">
<button type="button " class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
Ajouter <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li class="disabled"><a href="#">
<i class=" btn-sm fa fa-file-text-o " aria-hidden="true"></i>
DEVIS</a></li>
<li>
<a target="_blank" href="/document/add/5">
<i class=" btn-light-purple fa fa-file-text-o btn-sm " aria-hidden="true"></i>
BON DE COMMANDE</a>
</li>
<li>
<a target="_blank" href="/document/add/19">
<i class="btn btn-green fa fa-file-text-o btn-sm" aria-hidden="true"></i>
BON DE LIVRAISON</a>
</li>
<li>
<a target="_blank" href="/document/add/20">
<i class=" btn-light-orange fa fa-file-text-o btn-sm " aria-hidden="true"></i>
BON DE RETOUR</a>
</li>
<li class="disabled"><a href="#">
<i class="btn btn-light-azure btn-sm fa fa-file-text-o" aria-hidden="true"></i>
FACTURE INTERNE</a></li>
<li class="disabled"><a href="#">
<i class="btn btn-dark-azure btn-sm fa fa-file-text-o" aria-hidden="true"></i>
FACTURE A COMPTABILISER</a></li>
<li class="disabled"><a href="#">
<i class=" btn-sm fa fa-file-text-o " aria-hidden="true"></i>
FACTURE COMPTABILISEE </a></li>
<li class="disabled"><a href="#">
<i class=" btn-dark-orange btn-sm fa fa-file-text-o " aria-hidden="true"></i>
FACTURE DE RETOUR</a></li>
<li>
<a target="_blank" href="/document/add/33">
<i class=" btn-dark-yellow fa fa-file-text-o btn-sm " aria-hidden="true"></i>
AVOIR FINANCIER</a>
</li>
</ul>
</div>
</div>
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Ajouter
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<?php
foreach ($this->docTypes as $item) { ?>
<li><?= $item->label?></li>
<?php } ?>
<?php ?>
</ul>
</div>
</div>
</div>
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Documentachat\Controller\Documentachat' => 'Documentachat\Controller\DocumentachatController',
'Documentachat\Controller\Parametreachat' => 'Documentachat\Controller\ParametreachatController',
'Documentachat\Controller\DocArticle' => 'Documentachat\Controller\DocArticleController',
),
),
'router' => array(
'routes' => array(
'documentachat' => array(
'type' => 'segment',
'options' => array(
'route' => '/documentachat[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Documentachat\Controller\Documentachat',
'action' => 'index',
),
),
),
'parametreachat' => array(
'type' => 'segment',
'options' => array(
'route' => '/parametreachat[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Documentachat\Controller\Parametreachat',
'action' => 'index',
),
),
),
'docarticle' => array(
'type' => 'segment',
'options' => array(
'route' => '/docarticle[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Documentachat\Controller\DocArticle',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'documentachat' => __DIR__ . '/../view',
),
),
);

Add div around input field in ZF2 form

I am creating a form with Zend Framework 2. I'm using Form class to define form elements like this:
class PropertyForm extends Form
{
public function __construct($name=null)
{
...
$this->add(array(
'name' => 'zip',
'type' => 'Text',
'required' => false,
'options' => array(
'label' => 'Zip Code',
),
'attributes' => array(
'id' => 'zip',
'class' => 'form-control',
),
));
...
}
}
And in view I display this form element with the following code:
<div class="form-group"><?php echo $this->formRow($form->get('zip')); ?></div>
This generates the following HTML output:
<div class="form-group">
<label for="zip">Zip Code</label>
<input type="text" name="zip" id="zip" class="form-control" value="">
</div>
What I want to achieve is to have <div class="my-class"> around the input field as well. So the desired HTML output would be:
<div class="form-group">
<label for="zip">Zip Code</label>
<div class="my-class">
<input type="text" name="zip" id="zip" class="form-control" value="">
</div>
</div>
How can I achieve that?
You could get label and the element itself with two different calls:
For label:
<?php echo $this->formLabel($yourForm->get('your_element')); ?>
For the elements:
<?php echo $this->formSelect($yourForm->get('your_element_select')); ?>
<?php echo $this->formText($yourForm->get('your_element_text')); ?>
<?php echo $this->formRadio($yourForm->get('your_element_radio')); ?>
So, for you example, it would be something like:
<div class="form-group">
<?php echo $this->formLabel($form->get('zip')); ?>
<div class="my-class">
<?php echo $this->formText($form->get('zip')); ?>
</div>
</div>

Anchor error on form - Symfony3/Materialize

Using Symfony 3.2 && PhpStorm.2016.3.2 on Ubuntu 16.04
I have a problem that I partially resolved on the phone number field in this twig form.
I use materialize as the framework css for this project and I use the form of this framework.
I would like to be able to anchor the view directly on the form after an error has been spotted. So far it works great with the phone number section but anyone else doesn't work. I tried to tweak my code as much as I can but still I cannot anchor my view on other fields that get an error. It is pretty much frustrating.
here is the form twig in the view
<form class="col m12" method="POST">
{{ form_start(form) }}
<div class="row" id="radio-button-block">
<div class="col m12">
{{ form_row(form.baptismChoice) }}
</div>
</div>
<div class="row">
<div class="input-field col s6 validate" id="last_name" type="text">
{% if form.vars.errors|length %}
{{ form_row(
form.lastName,
form.lastName.vars|merge({'attr': {'autofocus': null}})
)
}}
{% else %}
{{ form_row(form.lastName) }}
{% endif %}
</div>
<div class="input-field col s6 validate" id="first_name" type="text">
{% if form.vars.errors|length %}
{{ form_row(
form.firstName,
form.firstName.vars|merge({'attr': {'autofocus': null}})
)
}}
{% else %}
{{ form_row(form.firstName) }}
{% endif %}
</div>
</div>
<div class="row">
<div class="input-field col s12 validate" id="email" type="email">
{{ form_row(form.authorEmail) }}
</div>
</div>
<div class="row">
<div class="input-field col s12 validate" id="icon_telephone" type="tel">
{{ form_errors(form) }}
{% if form.vars.errors|length %}
{{ form_row(
form.authorPhone,
form.authorPhone.vars|merge({'attr': {'autofocus': null}})
)
}}
{% else %}
{{ form_row(form.authorPhone) }}
{% endif %}
</div>
</div>
<div class="row">
<div class="input-field col s12 validate" id="city" type="text">
{{ form_row(form.city) }}
</div>
</div>
<div class="row" id="comment-block">
<div class="input-field col s12">
<div id="textarea">
{{ form_row(form.comment) }}
</div>
</div>
</div>
<div class="row">
<div class="input-field col s12 center-align">
{{ form_row(form.submit) }}
</div>
</div>
{{ form_end(form) }}
</form>
and here the Form builder in FomrType.php
->add('firstName', TextType::class, array(
'label' => 'Prénom',
'required' => true,
'attr' => array(
'class' => 'validate',
'id' => 'first_name',
),
'constraints' => array(new Regex(
array(
'pattern' => "#^[a-zéèàêâùïüë' -]{2,}$#i",
'message' => 'Oops ! Ce champ n\'est pas bon.'
)
)),
))
->add('lastName', TextType::class, array('constraints' => new Regex("#^[a-zéèàêâùïüë' -]{2,}$#i"),
'label' => 'Nom',
'required' => true,
'attr' => array(
'class' => 'validate',
'id' => 'last_name',
),
))
->add('authorEmail', EmailType::class, array('label' => 'Adresse mail',
'required' => true,
'attr' => array(
'class' => 'validate',
'if' => 'email'
),
))
->add('authorPhone', TextType::class, array(
'label' => 'Numéro de téléphone',
'required' => true,
'error_bubbling' => true,
'attr' => array(
'class' => 'validate',
'id' => 'icon_telephone'
),
'constraints' => array(new Regex(
array(
'pattern' => '#^0[1-9]([-. ]?[0-9]{2}){4}$#',
'message' => 'Oops ! Ce champ n\'est pas bon.'
)
)),
))
does anyone have an idea?
You're checking if the whole form has the errors set {% if form.vars.errors|length %}, but you should check if the specific field has an error {% if form.field_name.vars.errors|length %}.
In your present code, when an error occurs on any one of the fields, then autofocus is set on all of your fields and browser sets and scrolls to the first one.

Zend Form Decorators issue

*I speak English not well. So i'm going to post the code now.*
Form code:
protected $elementDecorators = array('ViewHelper','Errors','Description','Label',
array('HtmlTag',array('tag' => 'div','class' => '_wrapperElement')
));
public function init(){
$this->addElement('text','mytext',array(
'class' => '_inputText',
'label' => 'Mytext',
'required' => true,
'decorators' => $this->elementDecorators
));
$this->setDecorators(array('FormElements',array('HtmlTag',array('tag' => 'div','class' => '_formWrapper')),'Form'));
}
Output:
<form method="post" action="" enctype="application/x-www-form-urlencoded">
<div class="_formWrapper">
<div class="_wrapperElement">
<label class="required" for="mytext">Mytext</label>
<input type="text" class="_inputText" value="" id="mytext" name="mytext">
</div>
</div>
</form>
Now i want a div wraps Label and Input element like this:
<form method="post" action="" enctype="application/x-www-form-urlencoded">
<div class="_formWrapper">
<div class="_wrapperElement">
<div class="_wrapperLabel">
<label class="required" for="mytext">Mytext</label>
</div>
<div class="_wrapperInput">
<input type="text" class="_inputText" value="" id="mytext" name="mytext">
</div>
</div>
</div>
</form>
How to do that?
I tried many times but i can't do it.
Thanks!
protected $elementDecorators = array('ViewHelper','Errors','Description', array('Label', array('tag' => 'div', 'class' => '_wrapperLabel')
),
array('HtmlTag',array('tag' => 'div','class' => '_wrapperInput')
));
i found the solution that render decorators to ViewScript.