How to specify Symfony2 Bootstrap checkbox inline style? - symfony

The Symfony2 Boostrap template has a conditional switch on 'checkbox-inline'. How is this triggered?
{% if 'checkbox-inline' in parent_label_class %}
{{- form_label(form, null, { widget: parent() }) -}}

Since the conditional check is looking in parent_label_class, you can simply add to your form builder an option called label_attr, and there you can append your class.
Example:
$builder->add('checkbox', 'checkbox',
array(
'label_attr' => array(
'class' => 'checkbox-inline'
)
)
);
Which would give the following output:
<div class="checkbox">
<label class="checkbox-inline required">
<input type="checkbox" id="form_checkbox" name="form[checkbox]" required="required" value="1" />Checkbox
</label>
</div>

Related

Change Symfony Form Type element's form group CSS class

Briefly: I want to edit my form elements parent div class. In this code don't add or edit the css class to my target div (which is form-group) and the follow Form Type is an element of another Form Type.
I have an Form Type like the follow:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class, [
'empty_data' => '',
'label' => 'ID',
'attr' => [
'class' => 'col-sm-3'
],
]);
$builder->add('companyName', TextType::class, [
'empty_data' => '',
'label' => 'Şirket Adı',
'attr' => [
'class' => 'col-sm-3'
],
]);
}
So this form type renderin this HTML:
<div class="form-group field-member_earning_filter"><label class="control-label"> </label>
<div id="form_filters_member" css="col-sm-11">
<div class="form-group field-integer"><label class="control-label"
for="form_filters_member_id">ID</label><input type="number"
id="form_filters_member_id"
name="form_filters[member][id]"
class="form-control col-sm-3">
</div>
<div class="form-group field-text"><label class="control-label" for="form_filters_member_companyName">Ticari
Ünvan</label><input type="text" id="form_filters_member_companyName"
name="form_filters[member][companyName]" class="form-control col-sm-3"></div>
As you can see the CSS class which I want to add for each elements group are added to form-control element. But actually I want to do this:
<div class="form-group col-sm-3 field-integer"><label class="control-label"
for="form_filters_member_id">ID</label><input type="number"
id="form_filters_member_id"
name="form_filters[member][id]"
class="form-control">
I tried to refactor this challenge at view layer (on twig side) but actually doesn't have any real block to edit that "form group" section. Additionally that it's a form type in another form type for that reason the block don't affect my code. I mean this :
{% block form_row %}
<div class="form-group col-sm-3"> <--! but this form group actually my form type's form group -->
{{ form_label(field) }}
{{ form_errors(field) }}
{{ form_widget(field) }}
</div>
{% endblock %}
Do you have any idea?
I found a solution for my problem. It's little bit workaround or hacking means but solved my problem:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class, [
'empty_data' => '',
'label' => 'ID',
'block_prefix' => ' col-sm-3'
]);
$builder->add('companyName', TextType::class, [
'empty_data' => '',
'label' => 'Şirket Adı',
'block_prefix' => ' col-sm-3'
]);
}
This form-group's div is just affecting from block_ options. As you guess the block_prefix is a prefix of class but just adding a whitespace () then it's solve my problem :)

Display all rows in Symfony 3

I have a form which has EntityType:class below in Symfony 3 or SF3, as you can see I am calling the function from method findAllActiveBuyingCurrencies from BranchCurrencyRepository.
->add('currency', EntityType::class, [
'class' => BranchCurrency::class,
'choice_value' => 'rate',
'choice_label' => function($currency){
return $currency->getName() . ' (' . $currency->getCode() .') - '.$currency->getRate().'';
},
'placeholder' => 'Choose currency',
'query_builder' => function (BranchCurrencyRepository $er) {
return $er->findAllActiveBuyingCurrencies();
},
])
The functions does it's thing and displays all currencies inside the form (Option)
But, I wanted to display all currencies and my problem is, only 25 currencies are displayed.
Please see my code displaying all currencies
// branchCurrenciesRepository
public function findAllActiveBuyingCurrencies() {
return $this->createQueryBuilder('currency')
->where('currency.type = :type')
->setParameter('type', BranchCurrencyConstant::Buying);
}
View
<div class="col-3">
<label for="" class="required">
<strong> Currency <span class="highlight-red"></span></strong>
</label>
{{ form_widget(form.currency) }}
{% if not form.currency.vars.valid %}
<p class="mt10" style="color: #DC2B1B;">{{ form.currency.vars.errors[0].message }}</p>
{% endif %}
</div>

Hidden ACF front end form not saving

I'm using the front end form functionality of Advanced Custom Fields which works fine when on a standard page but when it's within a div that's hidden (either using Jquery to hide it or setting css to display none) the update button stops working and no data is saved. It doesn't show any errors in the console either.
How can I get around this? It's integral that the ACF form is hidden.
This is the code I'm using to display a particular set of fields:
<?php $activity_information = array(
'post_id' => $ID, // post id to get field groups from and save data to
'field_groups' => array(1781), // this will find the field groups for this post (post ID's of the acf post objects)
'form_attributes' => array( // attributes will be added to the form element
'class' => ''
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_field_open' => '<div>', // field wrapper open
'html_field_close' => '</div>', // field wrapper close
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => '', // default updated message. Can be false to show no message
); ?>
<?php acf_form( $activity_information ); ?>
CSS:
.reveal-area {
display: none;
}
This is what it's generating (with the 3 dots representing all the fields):
<section class="row reveal-area">
<div class="columns">
<form id="acf-form" action="" method="post">
<div id="acf-form-data" class="acf-hidden">
<input type="hidden" id="_acf_post_id" name="_acf_post_id" value="1773"> <input type="hidden" id="_acf_nonce" name="_acf_nonce" value="6798af667b"> <input type="hidden" id="_acf_validation" name="_acf_validation" value="1"> <input type="hidden" id="_acf_ajax" name="_acf_ajax" value="0"> <input type="hidden" id="_acf_changed" name="_acf_changed" value="0"> <input type="hidden" id="_acf_form" name="_acf_form" value="cllBUjlDU09QMUtyVXBKalJ3VmVsS3lFQXpPcHEwUm5KNmdHRzVrYXRnY0RCT0xrUndDMWhQTXg4UVJzZm1xTHlVQllDWkFrV2R4ZzNmT3FQdjhnNWJSMFdaTUNHYlNjNzZ5OEcvbzBkOHROcW5BYXlRYmdQZ1R0bW93UmJyUXV0MEZQVmZkWlZSbEVGV2hqS1E2dHJxSjFxR2xuU1gxZUwydGRrR254VjZuTTRHdFV0WXBmRGJTeS9KQ2VIbkkxR0Rxcit0ZWlDck9IL2pOYk16ODVqQ28reFBNR05QSWRUc3R2Z1E2L0tnbWZrMXlhRDRCNU5ZWFk0M0MyTkpqNkZpa254NXBudk1qZk5uNytRZDN0NmFpN0FOdkFXTWRDYXNUd2gzRXJxeXNqaHJUaU1rRTJTMENiZVhsNHV6b1ZWbGUxTHJwTm1YQTEzUlozcmNrWU5mdFdYSjF3WDNINEtJWGdVREsvNTNFRzg5dHVKVGxqTmFILyt1ci9tOGNDYTYyVUk1eDB2ZUZCdEg3R3JHQmhtSWZyczkvS3diOHIwY2FwOWNISTdmWlhsK241Tmdib2UyZjlNM1o0RnhCOW5QdjRiL2t5cm1HVTlmR3BCek1mYnAyUkFRRWlmNlJqcjdTS3g2dmdUOCtvRjBWL28rcFhmOWVrV2ViV3ZkUHN4NVZuOXBIMzR4RStUQUxMTksyVnN0YjNBVnFBU1crb3pWUzA3TUpSVkJJN2RjYmxnWnUzMk95RE42b2tqc1ROdnFVRFpmRFZSU3BvaFIxOWkvS042WHh6NGlIQlRBSlBRNXJVV1pHL0ZRL2Nid3RxbmRkcjNHREd2bHlkTmFnMnUrYjhRc0FpWjhhNkx1d3JoUU5yYi9kYnNxSWllSnU1a3E5bmhCNU9mSS8xK08rZ3VLN0tsYWZYV0N0ZElqeFJ2TkNBNEhsVC9ocHAwaklIMHZGTkFjKzBuZEhrZzU4cW9TRzNNL0k3Z2puWDAyRWZoOHNaeXF1UHlOUHR6aTA1L1B1QVVWMXd3WC9ybGlQOCsyVGZRdFdDaXpsbElZbll1UVJzQ3lpQjBoaEx0cmNYbG5hdTV3bGpNSVZab1ZhSUltWXRBWnhGMVplVjVFZXNBNzMzM0dSZmx1YWErUmE2WlpnYU11WUR2VS9kU1ZPRFlVNWZMYkZkNVZtZXZLMnBSN2t2K28xVzBQbDh0ZU94MUN4VEE0VDRBU0MxSGdHc2htZjNWR1phbUlGcUtnQ0h2d1Q0cDEwUUFjM1cwQkZ4RzltWUN2YjdwSTBhcGxtYkp4a05rRG4yK3JwSERFY1A5TFRFNGpJUzBmZXZ2ZkdZWHRzcUdNaXk2QTJSbFMxdnZmOURobE5xdGdBRWtieFZjZnRFbThrWFdWa2xWaVpFSnFQOWg0NDZNWDlrOXdjQzFXS0lsUWt4T1pRWENpd246Ok07hUIWm2U5p0Q9mApuvuM=">
</div>
...
<div class="acf-field acf-field-text acf-field--validate-email" style="display:none !important;" data-name="_validate_email" data-type="text" data-key="_validate_email">
<div class="acf-label">
<label for="acf-_validate_email">Validate Email</label>
</div>
<div class="acf-input">
<div class="acf-input-wrap">
<input type="text" id="acf-_validate_email" name="acf[_validate_email]">
</div>
</div>
</div>
</form>
</div>
<div class="acf-form-submit">
<input type="submit" class="acf-button button button-primary button-large" value="Update">
</div>
</section>
When you didn't make changes in the fields of the form, ACF ignores the submitting. Normally the form has a hidden field with id _acf_changed and value 0. When you are editing a field this value changed to 1.
So try to add JS script AFTER YOUR FORM:
<script type="text/javascript">
var is_edit=document.getElementById('_acf_changed');
is_edit.value='1';
</script>

Can we change the name field value in the formBuilder?

All of my question are in the subject :)
It's possible de customize the attribute "name" of a field in the formBuilder ?
If this is possible, can you describe the process for make this ?
Thank you for your reply in advance
You can change "name" attribute with Twig code:
{{ form_row(form.myfield, {'full_name':'myname'}) }}
You need to use "full_name" because "name" only changes the "title" attribute.
But I think that Symfony Forms uses the name to bind the form fields to the model, and if you change could have problems.
You can define field label by additional parameter.
$form = $this->createFormBuilder($task)
->add('task', 'text')
->add('dueDate', 'date', array('label' => 'Dday'))
->add('save', 'submit', array('label' => 'Create Task'))
->getForm(); //^
//|by using 3rd parameter 'options'
//|you can change 'name' of the field
EDIT: This is my formBuilder class for this
$builder
->add('type', 'choice', [
'choices' => [
'1',
'2',
'3'
],
'multiple' => true,
'expanded' => true,
])
;
I try to describe better my problem
actually, i have this html code
<div class="control-group control-filter">
<label>
<input type="checkbox" class="checkbox checkbox-orange">Foo
</label>
</div>
<div class="control-group control-filter">
<label>
<input type="checkbox" class="checkbox checkbox-orange">Foo
</label>
</div>
<div class="control-group control-filter">
<label>
<input type="checkbox" class="checkbox checkbox-orange">Foo
</label>
</div>
But now, i need to convert this code into the formBuilder, i use the choice type and i need to group by name all checkbox of this choice and make a custom class on all input element of these, can i do this ?

Symfony/Silex: Form submission does nothing (no errors, nothing returned)

I'm using the most recent version of Silex (without the .phar) with Doctrine DBAL installed, on this signup form page.
If I enter invalid details, it returns to that form as excepted. But if the details are valid, instead of redirecting to the /success/ page, it returns the same form again like nothing happened. The database has no entry received and Apache error log doesn't report any problems.
<?php
// ...
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Parser;
use Silex\Provider\FormServiceProvider;
use Symfony\Component\Validator\Constraints as Assert;
// ...
$app->match('/signup/', function(Request $request) use($app, $page) {
$data = array('name' => 'John','surname' => 'Smith','telephone' => '00011112222');
$form = $app['form.factory']->createBuilder('form', $data)
->add('name', 'text', array(
'constraints' => array(
new Assert\NotBlank(),
new Assert\MinLength(2),
),
'invalid_message' => 'First name is too short, It should have 2 characters or more',
))
->add('surname', 'text', array(
'constraints' => array(
new Assert\NotBlank(),
new Assert\MinLength(2),
),
'invalid_message' => 'Surname is too short, It should have 2 characters or more',
))
->add('telephone', 'text', array(
'constraints' => array(
new Assert\NotBlank(),
new Assert\Regex("/[\d\-\ ]+/"),
new Assert\MinLength(11),
),
'invalid_message' => 'Please enter a valid phone number. Must have 11 digits and may contain dashes (-) or spaces.',
))
->getForm();
if ('POST' == $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$app['db']->insert('signups', array(
'forename' => $data['name'],
'surname' => $data['surname'],
'telephone' => $data['telephone']
));
return $app->redirect('/success/');
}
}
$page['form'] = $form->createView();
return $app['twig']->render('signup.html.twig', $page);
}, 'POST|GET');
$app->match('/success/', function() use($app, $page) {
return $app['twig']->render('success.html.twig', $page);
}, 'POST|GET');
And the twig form
<form class="well" action="/signup/" method="post">
<fieldset>
<div class="control-group">
{% if (form_errors(form.name)) or (form_errors(form.surname)) or (form_errors(form.telephone)) %}
<div class="error-in-form">
<h5 style="color:#c00;">Please review the following errors:</h5>
<br />
<div>
<p class="help-msg"><span>First Name: </span></p>
<div class="error-msg">{{ form_errors(form.name) }}</div>
<div class="clearfix"></div>
</div>
<div>
<p class="help-msg"><span>Surname: </span></p>
<div class="error-msg">{{ form_errors(form.surname) }}</div>
<div class="clearfix"></div>
</div>
<div>
<p class="help-msg"><span>Telephone: </span></p>
<div class="error-msg">{{ form_errors(form.telephone) }}</div>
<div class="clearfix"></div>
</div>
</div>
{% endif %}
{{ form_label(form.name) }}
<div class="controls">
{{ form_widget(form.name, { 'attr': { 'class': 'input-medium' } } ) }}
{{ form_widget(form.surname, { 'attr': { 'class': 'input-medium' } } ) }}
</div>
</div>
<div class="control-group">
{{ form_label(form.telephone) }}
<div class="controls">
{{ form_widget(form.telephone, { 'attr': { 'class': 'input-fullwidth' } } ) }}
</div>
</div>
<p class="tnc">If you accepts the terms and conditions below, please proceed.</p>
<button id="big-red-button" type="submit" class="btn btn-danger btn-fullwidth">Submit ></button>
</fieldset>
</form>
Well, looks like I forgot to add {{ form_rest }} to the Twig form template.
Since I hadn't also included {{ form_errors(form) }} either, I couldn't see the error regarding the missing CSFP token, a hidden field that gets added to the form.

Resources