I am using Symfony 3 to create a web-application. I am using FOSUserBundle. I want user to register theirself, so I wrote a custom template and RegistrationFormType. My problem is, that the fiel "email" is not filled correctly in the controller. Every submit I get following error: https://ibb.co/cYkp8y
I am using the default controller from FOSUserBundle.
My template:
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'form-group'}}) }}
<fieldset>
<div class="form-group">
{{ form_widget(form.forename, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Vorname'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.surname, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Nachname'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.email, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Email'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.club, {'attr': {'class': 'form-control col-md-7 col-xs-12'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Passwort'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Passwort wiederholen'}}) }}
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Anmelden">
</fieldset>
{% do form.username.setRendered %}
{{ form_end(form) }}
My RegistrationType:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class)
->add('plainPassword', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\RepeatedType'), array(
'type' => LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'),
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
->add('forename', TextType::class, array(
'label' => 'Vorname',
))
->add('surname', TextType::class, array(
'label' => 'Nachname'
))
->add('club', EntityType::class, array(
'class' => 'AppBundle\Entity\Club',
'multiple' => false,
'expanded' => false,
'label' => 'Vereinszugehörigkeit',
'required' => false,
))
;
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
}
Does anybody know, why the controller can't handle the email?!
Related
I have a form in which I want to have checkboxes to add or remove elements from a collection: a User which have Responsability[].
I want to show some of the existing Responsability in the form but not all of them. I use an attribute called automatic to determine if I want to display them or not.
How can I edit my form to do such a thing?
UserType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class, [
'label' => 'Nom d\'utilisateurice'
])
->add('responsibilities', EntityType::class, [
// looks for choices from this entity
'class' => Responsibility::class,
// uses the Responsibility.label property as the visible option string
'choice_label' => 'label',
'label' => 'Rôles',
'multiple' => true,
'expanded' => true,
'choice_attr' => function($responsibility)
{
return [
'data-responsibility-description' => $responsibility->getDescription(),
];
},
])
->add('submit',SubmitType::class, [
'label' => 'Changer les informations',
'attr' => [
'class' => 'btn btn-outline-primary float-right'
]
]);
}
edit.html.twig:
{{ form_start(edit_form, {'attr': {'id': 'form-edit-user'}}) }}
<div class="form-group">
{{ form_label(edit_form.username) }}
{{ form_widget(edit_form.username) }}
</div>
<div class="form-group">
{{ form_label(edit_form.responsibilities) }}
{% for responsibility in edit_form.responsibilities %}
<div class="form-group">
{{ form_widget(responsibility) }}
{{ form_label(responsibility) }}
<span class="text-muted responsibility-description">
{{ responsibility.vars.attr['data-responsibility-description'] }}
</span>
</div>
{% endfor %}
</div>
{{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
You can use query_builder form option as documented here.
Something like this should work:
'query_builder' => function (EntityRepository $repository) {
return $repository
->createQueryBuilder('o')
->where('o.automatic = FALSE');
}
Or like this if you prefer having a parameter:
'query_builder' => function (EntityRepository $repository) {
return $repository
->createQueryBuilder('o')
->where('o.automatic = :automatic')
->setParameter('automatic', false);
}
using symfony 2.5 and Php 5.3.13.
i just want to add a field file type with option multiple => true.
It works ! but when the user click a second time on the button [Upload] it's deleting the first one already uploaded..
How can i do something like that : https://jsfiddle.net/gxfwvtqe/
In a symfony way, AdvertType.php :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('content', 'textarea')
->add('category', 'choice', array(
'choices' => array(
'incident' => 'Incident',
'general' => 'General',
),
'multiple' => false,
'expanded' => true,
'required' => true,))
->add('documents','file', array(
'required' => false,
'data_class'=> null,
'multiple' => true))
->add('save', 'submit')
;
}
form.html.twig :
<div class="well">
{{ form_start(form, {'attr': {'class': ''}}) }}
{{ form_errors(form) }}
{{ form_row(form.documents) }}
{{ form_widget(form.save, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
I am in need to displaying an error when two passwords fields are mismatching. I am trying to achieve this through setting invalid_message in my repeated password field, But when I try to call the error as {{form_errors(form.password)}} the error does not appear. However if I use {{form_errors(form)}} the the password mismatch error appears. I need to make the error field specific and would greatly value your input on this :)
I tried many online searches but none helped. Following is my implementation,
the twig
{{ form_start(form, {'attr': {'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate'}}) }}
<!--just placed the error here for debug purposes-->
{{ form_errors(form) }}
<div class="form-group {% if form.email.vars.errors|length > 0 %}has-error{% endif %} {% if form.email.vars.required == 'true' %}required{% endif %}">
{{ form_label(form.email) }}
<div class="col-sm-8">
{{ form_widget(form.email) }}
<span class="help-block">{{ form_errors(form.email) }}</span>
</div>
</div>
<div class="form-group {% if form.password.vars.errors|length > 0 %}has-error{% endif %} {% if form.password.vars.required == 'true' %}required{% endif %}">
{{ form_label(form.password.first) }}
<div class="col-sm-8">
{{ form_widget(form.password.first) }}
</div>
</div>
<div class="form-group required">
{{ form_label(form.password.second) }}
<div class="col-sm-8">
{{ form_widget(form.password.second) }}
</div>
</div>
<div class="form-group">
<div class="center-block btn-sign-in">
{{ form_widget(form.submit) }}
</div>
</div>
<span class="form-footer-msg">Already have an account? Sign In</span>
{{ form_end(form) }}
the form
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('email', 'email', array('label'=>'Email',
'attr'=>array('class'=>'form-control'),
'required'=>true,
'trim' => true,
'data'=>'user1#test.com',
'label_attr'=>array('class'=>'col-sm-3 control-label')));
$builder->add( 'password', 'repeated', array( 'type' => 'password',
//here is the password mismatch error message
'invalid_message' => ErrorMessages::PASSWORDS_DONOT_MATCH,
'options' => array('attr' => array('class' => 'password-field form-control')),
'error_bubbling' => true,
'required' => true,
'trim' => true,
'first_options' => array('label' => 'Password',
'error_bubbling' => true,
'label_attr'=>array('class'=>'col-sm-3 control-label')),
'second_options' => array('label' => 'Confirm password',
'error_bubbling' => true,
'label_attr'=>array('class'=>'col-sm-3 control-label'))));
$builder->add('submit', 'submit', array('label'=>'Create Account',
'attr'=>array('class'=>'btn btn-primary')))
->setMethod('POST')
->getForm();
}
public function getName() {
return 'signup';
}
the controller
public function indexAction() {
$signUp = new User();
$form = $this->createForm(new SignUpForm(), $signUp,
array('action'=>$this->generateUrl('accounts_signup')));
$request = $this->get('request');
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
if ($form->isValid()) {
$request = $this->get("request");
//do something
}else{
//do something else
}
}
return $this->render('AccountsBundle:Signup:index.html.twig',
array('form'=>$form->createView()));
}
Your input on this matter is greatly appreciated sirs :) thank you very much :)
The error bubbling is not needed. Here is a working example:
$builder->add('password', 'repeated', array(
'type' => 'password',
'label' => 'Zayso Password',
'required' => true,
'attr' => array('size' => 20),
'invalid_message' => 'The password fields must match.',
'constraints' => new NotBlankConstraint($constraintOptions),
'first_options' => array('label' => 'Zayso Password'),
'second_options' => array('label' => 'Zayso Password(confirm)'),
'first_name' => 'pass1',
'second_name' => 'pass2',
));
# twig
{{ form_row(form.password.pass1) }}
{{ form_row(form.password.pass2) }}
I am try to upload a file with laravel but when submit the form it gives the below error
Exception
Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
here is my blade:
{{ Form::open(array('route' => 'drivers.store', 'files' => true, 'class' => 'form-horizontal')) }}
<form role="form">
<div class="form-group first-field">
{{ Form::label('first_name', 'First Name:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4 ">
{{ Form::text('first_name', $value = null, array('placeholder' => 'ex-Jon', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('first_name') }} </span>
<div class="form-group">
{{ Form::label('last_name', 'Last Name:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('last_name', $value = null, array('placeholder' => 'ex-Doe', 'required' => 'required', 'class' => 'form-control', 'class' => 'form-control')) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('last_name') }} </span>
<div class="form-group">
{{ Form::label('email', 'Email:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('email', $value = null, array('placeholder' => 'ex-test#example.com', 'rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('email') }} </span>
<div class="form-group">
{{ Form::label('contact_number', 'Phone:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('contact_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('contact_number') }} </span>
<div class="form-group">
{{ Form::label('sin', 'SIN:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('sin', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('sin') }} </span>
<div class="form-group">
{{ Form::label('license_number', 'License Number:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('license_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('license_number') }} </span>
<div class="form-group">
{{ Form::label('license_file', 'License File:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::file('license_file') }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('license_file') }} </span>
<div class="form-group">
{{ Form::label('street_address', 'Street Address:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::text('street_address', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-symbol">* </span>
</div>
<span class='error-text'> {{ $errors->first('street_address') }} </span>
<div class="form-group">
{{ Form::label('password', 'Password:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::password('password',array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
<span class="required-text">Between 6 and 12 Characters</span>
</div>
<span class='error-text'> {{ $errors->first('password') }} </span>
<div class="form-group">
{{ Form::label('password_confirmation', 'Confirm Password:', array('class' => 'col-sm-3 control-label')) }}
<div class="col-xs-4">
{{ Form::password('password_confirmation', array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
</div>
</div>
<div class="form-group">
{{ Form::submit('Add Driver', array('class' => 'btn btn-primary center-block sh-request-button sign-up')) }}
</div>
</form>
{{ Form::close() }}
//controller:
public function store()
{
$input = \Input::all();
////echo "</pre>";print_r($input); $file= \Input::file('license_file.name');
$validator = $this->_modelDriver->validator($input);
if ($validator->fails()) {
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
}
else {
echo "test success";exit;
}
}
//validator:
public function validator(array $input, $isUpdate=false)
{
if(!$isUpdate) {
$rules = array(
'first_name'=>'required|alpha|min:2',
'last_name'=>'required|alpha|min:2',
'email'=>'required|email|unique:drivers',
'password'=>'required|between:6,12|confirmed',
'password_confirmation'=>'required|between:6,12'
);
} else {
$rules = array(
'firstname'=>'required|alpha|min:2',
'lastname'=>'required|alpha|min:2',
'email'=>'required|email',
);
}
return Validator::make($input, $rules);
}
I am new to laravel. So if someone can tell what am I doing wrong and how to fix this.
Thanks
This is happening because you are trying to return with the file input.
You should write this
$input = \Input::except('license_file');
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
instead of only
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
How can I adapt my form to implement bootstrap ?
My function for create form is:
private function createCreateForm(User $entity)
{
$form = $this->createFormBuilder($entity, array(
'action' => $this->generateUrl('user_create'),
'method' => 'POST',
))
->add('username')
->add('email')
->add('password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
))
->add('submit', 'submit', array('label' => 'Create'))
->getForm();
return $form;
}
My views is
{{ form_start(form) }}
{{ form_row(form.username, { 'attr': {'class': 'form-control', 'placeholder': 'Imię oraz nazwisko'} }) }}
{{ form_row(form.password, { 'attr': {'class': 'form-control', 'placeholder': 'Password'} }) }}
It is working for username but not for the password, some sugestion ? How can i create our own form ?
My bootstrap:
<form action="#" class="form-horizontal">
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">NAME</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="NAME
</div>
</div>
<div class="form-actions fluid">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn blue">Add</button>
<button type="button" class="btn default">Anuluj</button>
</div>
</div>
</form>
Use Mopa Bootstrapbundle https://github.com/phiamo/MopaBootstrapBundle
In your controller:
$this->createForm(
MyFormType(),
$entity,
array('attr' => array('class' => 'form-horizontal'))
);