Display all rows in Symfony 3 - symfony

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>

Related

symfony Admin lte Bundle personalize datetype form

I'm using the kevinPast adminLte bundle on symfony 5. Every datetype got their own format(dd-MM-YYYY) and you can't update it as a normal way by writing 'format' => 'yyyy-MM-dd' inside the form for example. The problem occurs also for other datetype, like birthday.
I also try to use my own datepicker function to override the existing one, but it's a bundle, so it first not work, then it broke also the existing js.
You can see the different try i did:
->add('startTime', DateTimeType::class, [
'required' => true,
'error_bubbling' => false,
'date_widget' => 'single_text',
// #todo Can we make this format configurable?
'date_format' => 'yyyy-MM-dd', // must match data-date-format as defined in field.html.twig
'minutes' => range(0, 55, 5)
])
it will show the data like that
it use the block date_widget on twig, the single_text area
{% block date_widget %}
{% if widget == 'single_text' %}
<div class="input-group">
<div class="input-group-addon">
<i class="far fa-calendar-alt"></i>
</div>
{% if type is not defined or type != 'date' %}
{% if attr.class is defined %}
{% set class = attr.class ~ ' timepicker' %}
{% else %}
{% set class = ' timepicker' %}
{% endif %}
{% set attr = attr|merge({'class' : class, 'data-datepickerenable':'on'}) %}
{% endif %}
{{ block('form_widget_simple') }}
</div>
{% else %}
{% set date_pattern = '<div class="row">' ~ date_pattern ~ '</div>'|raw %}
{{ date_pattern|replace({
'{{ year }}' : '<div class="col-xs-4">{{ year }}</div>',
'{{ month }}' : '<div class="col-xs-4">{{ month }}</div>',
'{{ day }}' : '<div class="col-xs-4">{{ day }}</div>',
})|raw|replace({
'{{ year }}': form_widget(form.year),
'{{ month }}': form_widget(form.month),
'{{ day }}': form_widget(form.day),
})|raw }}
{% endif %}
{% endblock %}
I also try to override it by using the following code:
Js part:
$(document).ready(function() {
$('.datepicker').datepicker({
format: "DD/MM/YYYY h:mm"
});
});
form part
->add('startTime', DateTimeType::class, [
'required' => true,
'error_bubbling' => false,
'date_widget' => 'single_text',
//<input type="text" class="form-control" data-inputmask-alias="datetime" data-inputmask-inputformat="dd/mm/yyyy" data-mask="" im-insert="false">
// #todo Can we make this format configurable?
'date_format' => 'yyyy-MM-dd', // must match data-date-format as defined in field.html.twig
'minutes' => range(0, 55, 5),
'attr' => ['class' => 'datepicker'],
])
This one overide the actual datepicker, but first doesn't fix the problem(you still have the standard format DD-MM-YYY) then it also broke the js used by the bundle(some navigation button doesn't work etc)
If I found a fix, i will also write the answer here.
So after few work on it and if someone got the same problem, I found one solution, maybe not the best but it's work.
I checked the demo used with adminLte bundle on symfony (Kimai2)
So to be able to edit your calendar and format your date, you need to follow these step:
1 : create a dateTimePickerType and use it instead of default DatetimeType
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DateTimePickerType extends AbstractType
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['attr'] = array_merge($view->vars['attr'], [
'data-datetimepicker' => 'on',
'autocomplete' => 'off',
'placeholder' => strtoupper($options['format']),
'data-format' => $options['format_picker'],
'data-time-picker-increment' => $options['time_increment'],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'label' => '',
'widget' => 'single_text',
'html5' => false,
'format' => 'YYYY-MM-DD',
'format_picker' => 'YYYY-MM-DD',
'with_seconds' => false,
'time_increment' => 1,
]);
}
/**
* {#inheritdoc}
*/
public function getParent()
{
return DateTimeType::class;
}
}
and on the formType
->add('startTime', DateTimePickerType::class, [
'required' => true,
'error_bubbling' => false,
'label' => 'startTime',
'format' => "yyyy-MM-dd'T'HH:mm:ss",
'attr' => ['class' => 'js-datepicker-hours'],
])
Then the javascript part, you need to install daterangerpicker from www.daterangepicker.com
and use these following function:
const $ = require('jquery');
global.$ = global.jQuery = $;
const Moment = require('moment');
global.moment = Moment;
require('moment/locale/en-gb');
require('daterangepicker');
$(document).ready(function() {
$('.js-datepicker').daterangepicker({
language: "en",
singleDatePicker: true,
showDropdowns: true,
minYear: 1901,
//maxYear: parseInt(moment().format('YYYY'),10)
});
$('.js-datepicker-hours').daterangepicker({
language: "en",
timePicker: true,
singleDatePicker: true,
showDropdowns: true,
locale: {
format: 'yyyy-MM-DD HH:mm:ss'
}
//maxYear: parseInt(moment().format('YYYY'),10)
});

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 :)

Symfony 2.8 Form EntityType Returns Single Value

Just trying to get a select field populated from a database table. This is just a simple table with a primary key column and another column named type.
Just for testing, the example table consists of:
id type
1 Sample 1
2 Sample 2
3 Sample 3
When I create a form:
$builder
->add('account_type', EntityType::class, array(
'class' => 'AppBundle:AppAccountTypes',
'choice_label' => 'type'
));
My select dropdown simply repeats the first entry 3 times.
<select id="add_account_form_account_type" name="add_account_form[account_type]" class="form-control">
<option value="1">Sample 1</option>
<option value="1">Sample 1</option>
<option value="1">Sample 1</option>
</select>
For testing sake, the controller is just using:
$account = new Account();
$form = $this->createForm(new AddAccountForm(), $account);
return $this->render('account/new.html.twig', array(
'page_title' => 'Create Account',
'form' => $form->createView()
));
Twig template:
{% extends 'base.html.twig' %}
{% block body %}
<h1>{{ page_title }}</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
What am I missing here?
May be you need to use below code:
$builder
->add('account_type', EntityType::class, array(
'class' => 'AppBundle:AppAccountTypes(Your Entity Class)',
'mapped' => false,
'choice_label' => 'type'
));
Because as i see in your code there is no field which name is account_type, May be this is the problem.
Change your controller as below:
$form = $this->createForm(AddAccountForm::class (Your form class), $account);

Drupal 8 module - How to use a template to display a form element?

I'm currently creating a Drupal 8 module. I would like to be able to use a custom template to display a select element in a form. Here is the code I have written so far:
File my_module/templates/colorselector.html.twig:
{% spaceless %}
<div class="select-wrapper">
{% set classes = ['form-control', 'color-selector'] %}
<select{{ attributes.addClass(classes) }}>
{% for option in options %}
<option value="{{ option.value }}" data-color="{{ option.code }}"{{ option.selected ? ' selected="selected"' }}>{{ option.label }}</option>
{% endfor %}
</select>
</div>
<script>
$('.color-selector').colorselector();
</script>
{% endspaceless %}
File my_module/my_module.module:
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\RenderElement;
function my_module_theme($existing, $type, $theme, $path) {
return [
// ...
'colorselector' => [
'render element' => 'select'
]
];
}
function my_module_preprocess_colorselector(&$variables) {
$element = $variables['element'];
Element::setAttributes($element, ['id', 'name', 'size']);
RenderElement::setAttributes($element, ['form-select']);
$variables['attributes'] = $element['#attributes'];
$variables['options'] = [];
foreach ($element['#options'] as $colorName => $colorCode) {
$option = [];
$option['value'] = $colorName;
$option['label'] = $colorName;
$option['code'] = $colorCode;
$variables['options'][] = $option;
}
}
File my_module/src/Form/MyCustomForm.php:
namespace Drupal\my_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class MyCustomForm extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
// ...
$form['color'] = [
'#type' => 'select',
'#theme' => 'colorselector',
'#title' => $this->t('Couleur'),
'#required' => TRUE,
'#options' => $options
];
// ...
return $form;
}
// ...
}
I get the following error message when I try to display the form: The website encountered an unexpected error. Please try again later.
If I remove '#theme' => 'colorselector' from $form['color'], it displays the form correctly but it does not use my template obviously.
Do you know where the error comes from and how to fix it?
Thanks!
Your hook_theme function should be like this.
function my_module_theme($existing, $type, $theme, $path) {
return array(
'color_selector' => array(
'variables' => array('params' => null),
'template' => 'colorselector'
)
);
}
Now, your template name might be my_module/templates/colorselector.html.twig

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