php app/console doctrine:generate:form AppBundle:Usuario --no-interaction
el cual editamos asi:
class UsuarioType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', EmailType::class);
$builder->add('password', RepeatedType::class, array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => PasswordType::class
));
}/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Usuario'
));
}
}
en este caso vamos a usar el email como "nombre de usuario" para el registro
notese que el campo password aunque es unico, se mostrar en el formulario como duplicado
para que se pueda ingresar 2 veces y asi confirmar el password.
creamos ahora el controller:
class RegistrationController extends Controller
{
/**
* @Route("/register", name="register")
*/
public function registerAction(Request $request)
{
// Create a new blank user and process the form
$user = new Usuario();
$form = $this->createForm('AppBundle\Form\UsuarioType', $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$this->get('logger')->debug('todo OK');
// Encode the new users password
$encoder = $this->get('security.password_encoder');
$password = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($password);
$user->setUsername($user->getEmail());
// Save
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('login');
}
return $this->render('auth/register.html.twig', [
'form' => $form->createView(),
]);
}
}
finalmente creamos la vista:
vista simple:
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" value="Register" />
{{ form_end(form) }}
vista customizada:
{{ form_start(form, {'action': path('register'), 'method': 'POST',}) }}
<div class="row">
<div class="large-12 columns">
{{ form_widget(form.username) }}
{{ form_errors(form.username) }}
</div>
</div>
<div class="row">
<div class="large-12 columns">
{{ form_widget(form.email) }}
{{ form_errors(form.email) }}
</div>
</div>
<div class="row">
<div class="large-12 columns">
{{ form_widget(form.password.password) }}
{{ form_errors(form.password.password) }}
</div>
</div>
<div class="row">
<div class="large-12 columns">
{{ form_widget(form.password.confirm) }}
{{ form_errors(form.password.confirm) }}
</div>
</div>
<div class="row">
<div class="large-12 large-centered columns">
<input type="submit" class="button expand" value="Register"/>
</div>
</div>
{{ form_end(form) }}
TIP : agregar check de Aceptacion de terminos y condiciones
simplemente agregar en el UsuarioType
$builder->add('termsAccepted', CheckboxType::class, array(
'mapped' => false,
'constraints' => new IsTrue(),
));
fuente
https://symfony.com/doc/3.4/doctrine/registration_form.html
No hay comentarios:
Publicar un comentario