No, I finally decided not to use a subform (see below).
Originally I wanted to use a subform because 'termsAccepted' was not a
property of the class Enterprise. 'termsAccepted' and the class
Enterprise were both properties of the Registration class. Now both
Enterprise and Registration have the properties username, contactName,
etc. I bind the form to the Registration class and later save the
values to Enterprise.
Twig now looks like this:
<form action="#" method="post">
{{ form_errors(form) }}<br>
{{ form_errors(form.username) }}<br>
<label for="username">{% trans "username" from "labels" %}</
label>:<br>
{{ form_field(form.username) }}<br>
{{ form_errors(form.contactName) }}<br>
<label for="contactName">{% trans "contact.name" from "labels" %}</
label>:<br>
{{ form_field(form.contactName) }}<br>
{{ form_errors(form.contactEmail) }}<br>
<label for="contactEmail">{% trans "contact.email" from "labels" %}
</label>:<br>
{{ form_field(form.contactEmail) }}<br>
{{ form_errors(form.password) }}<br>
{{ form_errors(form.password.password) }}<br>
<label for="password.password">{% trans "password" from "labels" %}
</label>:<br>
{{ form_field(form.password.password) }}<br>
<label for="password.passwordAgain">{% trans "password.again" from
"labels" %}</label>:<br>
{{ form_field(form.password.passwordAgain) }}<br>
{{ form_errors(form.termsAccepted) }}<br>
{{ form_field(form.termsAccepted) }} <label for="termsAccepted">{%
trans "terms.accepted" from "labels" %}.</label><br>
{{ form_hidden(form) }}
<input type="submit" value="{% trans "submit" from "labels" %}" />
</form>
The controller looks like this:
public function signupAction()
{
$registration = new Registration();
$manager = $this->get('doctrine.orm.entity_manager');
$form = RegistrationForm::create($this->get('form.context'),
'registrationForm');
$form->bind($this->get('request'), $registration);
if ($form->isValid())
{
$factory = $this->get('security.encoder_factory');
$translator = $this->get('translator');
$mailer = $this->get('mail_manager');
$enterprise = $registration->process($manager, $factory,
$mailer, $translator);
return $this->render('::confirmPublic.twig.html',
array('message' => $translator->trans('registered', array(),
'page_messages')));
}
return $this->render('AuthBundle:Auth:signup.twig.html',
array('form' => $form));
}
The form looks like this:
class RegistrationForm extends Form
{
protected function configure()
{
$this->add(new TextField('username'));
$this->add(new TextField('contactName'));
$this->add(new TextField('contactEmail'));
$this->add(new PasswordField('password'));
$this->add(new RepeatedField(new PasswordField('password'),
array('first_key' => 'password', 'second_key' => 'passwordAgain')));
$this->add(new CheckboxField('termsAccepted'));
}
}
Registration class looks likes this:
class Registration
{
...
public function process(EntityManager $manager, EncoderFactory
$factory, MailManager $mailer, Translator $translator)
{
$enterprise = new Enterprise();
$enterprise->setUsername($this->username);
$enterprise->setContactName($this->contactName);
$enterprise->setContactEmail($this->contactEmail);
$now = new \DateTime("now");
$enterprise->setCreatedAt($now);
$enterprise->setUpdatedAt($now);
$enterprise->setLastLogin($now);
$enterprise->setEnabled(FALSE);
$enterprise->setAccountNonExpired(TRUE);
$enterprise->setAccountNonLocked(TRUE);
$enterprise->setCredentialsNonExpired(TRUE);
$enterprise->setSalt(rand(0, 10000));
$confirmationToken = \md5($this->username . $enterprise-
>getSalt());
$enterprise->setConfirmationToken($confirmationToken);
$encoder = $factory->getEncoder($enterprise);
$encrPassword = $encoder->encodePassword($this->password,
$enterprise->getSalt());
$enterprise->setPassword($encrPassword);
$enterpriseRole = $manager->getRepository('iMARKET\AuthBundle
\Entity\Role')->findOneByRole('ROLE_ENTERPRISE');
$enterprise->getAllRoles()->add($enterpriseRole);
$manager->persist($enterprise);
$manager->flush();
$mailer->sendTemplateEmail($this->contactEmail, $translator-
>trans('activate', array(), 'mail_subjects'),
'AuthBundle:Auth:activationEmail.twig.html', array('contactName' =>
$this->contactName, 'confirmationToken' => $confirmationToken));
return $enterprise;
}
...
}
On ápr. 5, 08:20, François CONSTANT <[email protected]>
wrote:
> Hi,
>
> I've got the exact same issue. Andreas, have you found the solution?
>
> PS: I'm using PR9.
>
> Cheers,
>
> On Feb 16, 9:34 pm, Andras <[email protected]> wrote:
>
> > Hi,
>
> > A bit shorter version of my question: I have aformwith asubform
> > called enterprise.
>
> > How can I print the field validationerrorswithtwig? E.g.
> > {{ form_errors(form.enterprise.username) }} does not work for the
> > field username.
>
> > Andras
>
> > On febr. 15, 11:42, Andras <[email protected]> wrote:
>
> > > Hi,
>
> > > I'm trying to print the error messages for the fields of thesubform
> > > called enterprise intwig:
>
> > > If I do it like this no messages appear (except for
> > > form_errors(form.termsAccepted)):
>
> > > {{ form_errors(form) }}<br>
> > > {{ form_errors(form.enterprise.username) }}<br>
> > > {{ form_label(form.enterprise.username) }}:<br>
> > > {{ form_field(form.enterprise.username) }}<br>
> > > {{ form_errors(form.enterprise.contact_name) }}<br>
> > > {{ form_label(form.enterprise.contact_name) }}:<br>
> > > {{ form_field(form.enterprise.contact_name) }}<br>
> > > {{ form_errors(form.enterprise.contact_email) }}<br>
> > > {{ form_label(form.enterprise.contact_email) }}:<br>
> > > {{ form_field(form.enterprise.contact_email) }}<br>
> > > {{ form_errors(form.enterprise.password.first) }}<br>
> > > {{ form_label(form.enterprise.password.first) }}:<br>
> > > {{ form_field(form.enterprise.password.first) }}<br>
> > > {{ form_errors(form.enterprise.password.second) }}<br>
> > > {{ form_label(form.enterprise.password.second) }}:<br>
> > > {{ form_field(form.enterprise.password.second) }}<br>
> > > {{ form_errors(form.termsAccepted) }}<br>
> > > {{ form_field(form.termsAccepted) }}
> > > {{ form_label(form.termsAccepted) }}<br>
> > > {{ form_hidden(form) }}
>
> > > If I write {{ form_errors(form.enterprise) }} to the top it shows all
> > > of thesubformerror messages at once at the top of theform. Is this
> > > a bug or am I doing something the wrong way?
>
> > > Andras
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en