I just learn how to use fosuserbundle, I just added "name" column to entity and database field, and now, the registration form is shows like this :
Email :[_______________________________]
Username :[_______________________________]
Password :[_______________________________]
Verification:[_______________________________]
Name :[_______________________________]<-name
my question is how to make it orders like this since I added 'name, field later.
Email :[_______________________________]
Name :[_______________________________]<-order name here
Username :[_______________________________]
Password :[_______________________________]
Verification:[_______________________________]
should I make it ordered from database table or just from Form factory ?
thanks,
Copy vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/views/Registration/register_content.html.twig into app/Resources/FOSUserBundle/views/Registration/register_content.html.twig and make changes like:
vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/views/Registration/register_content.html.twig
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}
<div>
<input type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
</form>
app/Resources/FOSUserBundle/views/Registration/register_content.html.twig
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_widget(form.email) }}
{{ form_widget(form.name) }}
{{ form_widget(form.username) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.second) }}
{{ form_end(form) }}
<input type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
</form>
One way to do it as #repincln's answer. But other way around is to update the form builder, as you have already extended to add your field. I think it would be much easier/cleaner if you just re-arrange your form while building it. Assuming you have override the form as per FOSUserBundle Documentation
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('name')
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
;
}
Use which one you like.
Happy Coding...
Related
I'm a beginner with PHP and Symfony.
I'm trying to show each values of ring (which is entity) from database and trying to add checkbox at the end of the each ring.
My twig template only shows the checkbox in the first ring.
How do I show all the rings?
This is my controller:
public function testPage(Request $request) {
$ring = $this->ringRepository->customFindAll();
$jewel = $this->ringJewelRepository->customFindAll();
$custom = $this->customRingRepository->customFindAll();
$form = $this->createFormBuilder($ring)
->add('active', CheckboxType::class, [
'label'=>'Selection',
'required'=>false
])
->getForm();
$form->handleRequest($request);
return [
'rings'=>$ring,
'jewels'=>$jewel,
'custom' => $custom,
'form'=>$form->createView()
];
}
This is my Twig template:
{% for ring in rings %}
<div>
<div class="text-center">
<p>Ring Name: {{ ring.ring_name }}</p>
<p>Ring Type: {{ ring.ring_type }}</p>
{{ form_start(form) }}
{{ form_widget(form.active) }}
{{ form_end(form) }}
</div>
<div class="text-center">
Update Ring
</div>
</div>
{% endfor %}
Considering your use-case I think you might be looking for an EntityType, with the 'multiple' option set to true. The syntax would be as below. Make sure to import your Ring class as well.
->add('active', EntityType::class, [
'class' => Ring::class,
'multiple' => true,
'label' => 'Selection',
'required' => false,
'query_builder' => static fn (RingRepository $ringRepository) => $ringRepository->customFindAll(),
]);
You can read more about the EntityType in the symfony docs.
I have this buildForm method in FormType file:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', TextareaType::class, array(
'label' => 'Коментар',
'attr' => array(
'class' => 'form-control input_box'
)
))
->add('submit', ButtonType::class, array(
'label' => 'SEND',
'attr' => array(
'id' => 'saveButton'
)
));
}
Then i render form in twig file:
<div class="post_comment">
<h3>Add comment</h3>
{{ form_start(commentForm) }}
<!--div class="col-md-6">
<h4>Name</h4>
<input type="text" class="form-control input_box" id="fullname" placeholder="">
</div>
<div class="col-md-6">
<h4>Email</h4>
<input type="text" class="form-control input_box" id="email" placeholder="">
</div-->
<div class="col-md-12">
<h4>{{ form_label(commentForm.content) }}</h4>
{{ form_widget(commentForm.content) }}
{{ form_widget(commentForm.submit) }}
</div>
{{ form_end(commentForm) }}
</div>
But button does not have id with saveButton, instead:
<button type="button" id="app_bundle_comment_form_type_submit" name="app_bundle_comment_form_type[submit]">SEND</button>
When i set id in twig file, like this, it's work fine:
{{ form_widget(commentForm.submit, {'id': 'saveButton' }) }}
The ID of the button will be the first parameter of the add() function (in your case, "submit"). That's why you get "app_bundle_comment_form_type_submit" on your id.
To delete the rest of the id, there's a function in the FormType file called getBlockPrefix(). Just set the return value to "" and you're done.
I am trying to override the default areas of the fosuserbundle registration form. I added all the required fields I wanted through database and customize with the bootstrap cdn. But when I try to customize the defaults fields of the registration form, I cannot find it in both register_content.html.twig and register.html.twig to edit them.
register_content.html.twig
{% trans_default_domain 'FOSUserBundle' %}
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register'}}) }}
{{ form_widget(form) }}
<div>
<input class="btn btn-success" type="submit" id="_submit" name="_submit" value="{{ 'registration.submit'|trans }}" />
</div>
{{ form_end(form) }}
register.html.twig
{% extends "#FOSUser/layout.html.twig" %}
{% block fos_user_content %}
{% include "#FOSUser/Registration/register_content.html.twig" %}
{% endblock fos_user_content %}
I would like to customize all the following textareas.
Email
Username
Password
Repeat password
Where can I find the above fields?
Thanks in advance.
You can find its at vendor/friendsofsymfony/user-bundle/Form/Type/RegistrationFormType.php if you want to change its directy
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
;
}
But it better to override the forme type as below in config.yml (create a new formType)
fos_user:
# ...
registration:
form:
type: AppBundle\Form\RegistrationType
If you want to override existing FormType then follow that doc: http://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html
If you want to choose specific fields in html, you can select fields like that:
{{ form_widget(form.email) }}
{{ form_widget(form.username) }}
or
{{ form_widget(form.email, { 'attr': {'class': 'foo'} }) }}
I tried to create 2 form groups but I can see only the first group.
Is this the correct way to make a form group?
My Form Type Builder:
$builder->add(
$builder->create('step1', 'form', array('virtual' => true))
->add('url')
->add('description')
->add('created_at'),
$builder->create('step2', 'form', array('virtual' => true))
->add('user')
->add('tags', 'entity', array(
'class' => 'LanCrmBundle:LinkTag',
'property' => 'title',
'multiple' => true
))
);
My view :
{% extends '::base.html.twig' %}
{% block body %}
<form method="post" {{ form_enctype(form) }} class="form-horizontal" novalidate>
{{ form_widget(form) }}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-success"/>
</div>
</div>
</form>
{% endblock %}
if you want to create form steps with symfony 2, you can use the next bundle:
https://packagist.org/packages/craue/formflow-bundle
I used it and works fine.
You can see an example in:
http://www.craue.de/sf2playground/en/CraueFormFlow/
this below code is my profile managment form for users and i want to fill input tags with $profile variable .
{{ $profile }} can echo all fields of user table. but i can not fill input tags with this variable.
Result: {"id":"1","username":"mahdi","name":"Mahdi","family":"Pishguy","email":"name#gmail.com"}
My Form:
{{ Form::open(array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
<div class="form-group rtl">
<div>Your Username: <b> {{ $profile->username }} </b></div><br />
{{ Form::label('name' , 'name: ') }}
{{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}
{{ Form::label('family' , 'family: ') }}
{{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}
<p>
{{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
</p>
</div>
{{ Form::close() }}
Thats easy, see the Form Model Binding in the docs
You code should be something like this:
{{ Form::model($profile, array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
<div class="form-group rtl">
<div>Your Username: <b> {{ $profile->username }} </b></div><br />
{{ Form::label('name' , 'name: ') }}
{{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}
{{ Form::label('family' , 'family: ') }}
{{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}
<p>
{{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
</p>
</div>
{{ Form::close() }}
NOTE: You can remove the Input::old() stuf because this is handled by the Form::model()