Different value_options in Form Collection - zend-framework

I hava a Collection, in which a field User (a multiselect) depends on a previous Select, the Department. Therefore each User select contain a different "value_options".
How can I set different "value_options" when retrieving the form for each row of the Collection?

You have different options:
You create an API endpoint to retrieve form options
You make this into two different pages, the first one you choose the department and the second one you choose the user (ew)
You populate the form in server-side and you filter the selects on client side
I personally discourage the second option. Is there just to say that it is a possible solution, but NO.
The first option, the API, is interesting, but requires actually more work, especially if it is the only endpoint in your application.
The third option is the one I always use, since it requires the less code and it quite simple to implement.
In your form, you have your two elements:
$this->add([
'name' => 'department_id',
'type' => 'Select',
'attributes' => [
'id' => 'department_id'
],
'options' => [
'value_options' => [
1 => 'Marketing',
2 => 'IT',
3 => 'Logistic'
]
]
]);
$this->add([
'name' => 'user_id',
'type' => 'Select',
'attributes' => [
'id' => 'user_id',
'multiple' => true
],
'options' => [
'value_options' => [
[
'value' => 1,
'label' => 'John Doe - Marketing',
'attributes' => ['data-department-id' => 1]
],
[
'value' => 2,
'label' => 'Jane Doe - Marketing',
'attributes' => ['data-department-id' => 1]
],
[
'value' => 3,
'label' => 'Jack Doe - IT',
'attributes' => ['data-department-id' => 2]
],
[
'value' => 4,
'label' => 'Dana Doe - IT',
'attributes' => ['data-department-id' => 2]
],
[
'value' => 5,
'label' => 'Frank Doe - Logistic',
'attributes' => ['data-department-id' => 3]
],
[
'value' => 6,
'label' => 'Lara Doe - Logistic',
'attributes' => ['data-department-id' => 3]
]
]
]
]);
As you can see, all the users are put in the value_options. Keep in mind that this is just an example, you should use custom elements to populate this kind of selects ;)
Then, in your view, you render the elements:
<?= $this->formElement($this->form->get('department_id')); ?>
<?= $this->formElement($this->form->get('user_id')); ?>
And you finally add the JS code to handle the filter. Here I use jQuery, but it's not necessary to use it:
$(function () {
// Filter users when you load the page
filterUsers(false);
// Filter users when you change value on multiselect
$('#department_id').on('change', function () {
filterUsers(true);
});
});
function filterUsers(resetValue) {
var departmentId = $('#department_id').val();
// Remove previous value only when filter is changed
if (resetValue) {
$('#user_id').val(null);
}
// Disable all options
$('#user_id option').attr('disabled', 'disabled').attr('hidden', true);
// Enable only those that respect the criteria
$('#user_id option[data-department-id=' + departmentId + ']').attr('disabled', false).attr('hidden', false);
}
Final tip: don't forget to create and add to the form a Validator to check the couple department_id - user_id is correct, just to avoid (on my example) to accept Lara Doe (logistic) with IT department ;)

Related

Extbase proper relation discovery

I've come accross a weird issue with extbasewhile working on some semi-complicated logic for filtering of courses for a LMS tools that we work on.
The logic is as follows:
There are course templates and seminars
A course template always has a start and end date, it contains courses that depend on one another
A seminar contains multiple courses that do not depend on one another
As long as a course template starts after the selected date, it has to be displayed
As long as a seminar contains a course that starts after the selected date, it has to be displayed
There are other filters that do not matter here and that do not play into this issue
In order to solve this request, I resorted to the power of extbase being able to simply create a subquery by using something like $query->greaterThanOrEqual('template_children.start_date', $date) (see below for concrete example). This now generates the below result:
Resulting SQL:
SELECT `tx_xxx_domain_model_courseprogrammetemplate`.*
FROM `tx_xxx_domain_model_courseprogrammetemplate`
`tx_xxx_domain_model_courseprogrammetemplate`
LEFT JOIN `tx_xxx_domain_model_courseprogrammetemplate`
`tx_xxx_domain_model_courseprogrammetemplate0`
ON Find_in_set(
`tx_xxx_domain_model_courseprogrammetemplate0`.`uid`,
`tx_xxx_domain_model_courseprogrammetemplate`.`template_children`)
The relations are built by an important and there are no values written to the field template_children on this side of the relation, thus no result is found.
AFAIK, this should work without having to populate this field with anything else than maybe an amount of children (and I'm not sure if this is even necessary anymore).
Here's my TCA configuration and the PHP code handling the logic.
TCA:
'template_children' => [
'exclude' => true,
'label' => 'LLL:EXT:xxx/Resources/Private/Language/locallang_db.xlf:tx_xxx_domain_model_courseprogrammetemplate.template_children',
'config' => [
'items' => [
['', 0]
],
'type' => 'select',
'renderType' => 'selectSingleBox',
'foreign_table' => 'tx_xxx_domain_model_courseprogrammetemplate',
'foreign_table_where' => 'AND tx_xxx_domain_model_courseprogrammetemplate.template = ###REC_FIELD_uid### AND tx_xxx_domain_model_courseprogrammetemplate.sys_language_uid = 0',
'readOnly' => 1,
'size' => 5,
'maxitems' => 100,
'autoSizeMax' => 20,
],
],
Extbase:
$constraints[] =
$query->logicalAnd(
[
$query->logicalOr(
[
// If the learning form is a course, the start and end date should be in between the period
$query->logicalAnd(
[
$query->greaterThanOrEqual('start_date', $demand->getStartDate()->format('Y-m-d H:i:s')),
$query->logicalNot($query->equals('learning_form', 'Seminar'))
]
),
// If the learning form is seminar, we only want to display it, if there is at least one course that starts in this period
$query->logicalAnd(
[
$query->logicalOr(
[
$query->greaterThanOrEqual('templateChildren.start_date', $demand->getStartDate()->format('Y-m-d H:i:s')),
]
),
$query->equals('learning_form', 'Seminar')
]
)
]
)
]
);
I tried switching the TCA field type to inline but this didn't change the behaviour.
Another way to do this would be to get all objects that relate to each seminar that match the filter, but that would mean creating some thousands of separate queries while filter :-/
Thanks for your support.
PS: I found this article, but it does not describe, how to configure the TCA accordingly, so that it works:
TYPO3 Extbase: Filtering a 1:N relation
Also sadly the documentation doesn't say much about what to configure how in TCA for this to work:
https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/6-Persistence/3-implement-individual-database-queries.html
I ended up finding the solution to my problem: you have to use inline as a type so that extbase has a chance to know how to resolve the relation:
'template_children' => [
'exclude' => true,
'label' => 'LLL:EXT:xxx/Resources/Private/Language/locallang_db.xlf:tx_xxx_domain_model_courseprogrammetemplate.template_children',
'config' => [
'items' => [
['', 0]
],
'type' => 'inline',
'foreign_table' => 'tx_xxx_domain_model_courseprogrammetemplate',
'foreign_field' => 'template',
'appearance' => [
'collapseAll' => 1,
'levelLinksPosition' => 'top',
'showSynchronizationLink' => 1,
'showPossibleLocalizationRecords' => 1,
'showAllLocalizationLink' => 1
],
'overrideChildTca' => [
'ctrl' => [
'types' => [
'1' => ['showitem' => 'sys_language_uid, l10n_parent, l10n_diffsource, hidden, title'],
],
],
],
],
],

TYPO3 TCA label with UserFunc - how to get HTML formatted label?

I want to format the title showing in a list of TCA items which can contain italic text. But whatever I try, I get only unformatted text - even from RTE text fields.
My base information is "partA", "partB", "partC" and I need a title like "partA : partC - part B"
My Code so far:
<?php
return [
'ctrl' => [
'title' => 'LLL:EXT:myext/Resources/Private/Language/myext.xlf:tx_myext_domain_model_myitem',
'label' => 'partC',
'label_alt' => 'partA',
'formattedLabel_userFunc' => T395\myExt\Classes\UserFuncs\MyBEUserFuncs::class.'->getFullMyitemTitle',
'formattedLabel_userFunc_options' => [
'sys_file' => [
'partC','partA','partB'
]
],
'iconfile' => 'fileadmin/Resource/icons/svgs/myext.svg',
],
'columns' => [
'partC' => [
'label' => 'LLL:EXT:myext/Resources/Private/Language/myext.xlf:tx_myext_domain_model_myitem.partC',
'config' => [
'type' => 'text',
'enableRichtext' => true,
],
],
'partA' => [
'label' => 'LLL:EXT:myext/Resources/Private/Language/myext.xlf:tx_myext_domain_model_myitem.partA',
'config' => [
'type' => 'input',
'size' => '5',
'eval' => 'trim',
],
],
'partB' => [
'label' => 'LLL:EXT:myext/Resources/Private/Language/myext.xlf:tx_myext_domain_model_myitem.partC',
'config' => [
'type' => 'input',
'size' => '5',
'eval' => 'trim',
],
],
],
'types' => [
'0' => ['showitem' => 'partA,partB,partC'],
],
];
And the UF:
<?php
T395\myExt\Classes\UserFuncs;
class MyBEUserFuncs
{
public function getFullMyitemTitle(&$params, &$pObj)
{
echo "Hello World!";
$params['title'] = $params['row']['partA'].' : '.$params['row']['partC'].' - '.$params['row']['partB'];
}
}
Even the echo is not showing. Changing the formattedLabel_userFunc to label_userFunc results in getting a string in right order - but right without any text formats like <i> etc but showing them as text. I'm sure, I'm missing something, but I can't figure out what it is - I was also unable to find any code snippets or examples showing the right way - and the docs from TYPO3 saying only that exists formattedLabel_userFunc and it has options - but no proper example there. Hope you can help me. Thank you!
in the documentation for formattedlabel_userfunc you can find:
[...] return formatted HTML for the label and used only for the labels of inline (IRRE) records.
and for label_userfunc there is the warning:
The title is passed later on through htmlspecialchars() so it may not include any HTML formatting.

Add Taxvat field to magento 2 checkout page

I'm from Brazil, and here we use the "taxvat" customer field to store a number called "CPF". I managed to make the field appear on checkout by adding it to an layoutProcessor, like this:
$shippingFields['taxvat'] = [
'component' => 'Magento_Ui/js/form/element/abstract',
'label' => __('CPF'),
'config' => [
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/input',
],
'placeholder' => 'CPF *',
'validation' => [
'required-entry' => 1
],
'provider' => 'checkoutProvider',
'source' => 'customer.taxvat',
'dataScope' => 'customer.taxvat',
'sortOrder' => 1,
];
But, i don't know why, this field it's not saving on database. It only works if i save this field on customer form, not on checkout form.
Anyone know what i'm missing?
thanks!

How to validate a checkbox in ZF2

I've read numerous workarounds for Zend Framework's lack of default checkbox validation.
I have recently started using ZF2 and the documentation is a bit lacking out there.
Can someone please demonstrate how I can validate a checkbox to ensure it was ticked, using the Zend Form and Validation mechanism? I'm using the array configuration for my Forms (using the default set-up found in the example app on the ZF website).
Try this
Form element :
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'agreeterms',
'options' => array(
'label' => 'I agree to all terms and conditions',
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 'no'
),
));
In filters, add digit validation
use Zend\Validator\Digits; // at top
$inputFilter->add($factory->createInput(array(
'name' => 'agreeterms',
'validators' => array(
array(
'name' => 'Digits',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
Digits::NOT_DIGITS => 'You must agree to the terms of use.',
),
),
),
),
)));
You could also just drop the hidden form field (which I find a bit weird from a purist HTML point of view) from the options instead of setting its value to 'no' like this:
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'agreeterms',
'options' => array(
'label' => 'I agree to all terms and conditions',
'use_hidden_element' => false
),
));
I had the same problem and did something similar to Optimus Crew's suggestion but used the Identical Validator.
If you don't set the checked_value option of the checkbox and leave it as the default it should pass in a '1' when the data is POSTed. You can set it if you require, but make sure you're checking for the same value in the token option of the validator.
$this->filter->add(array(
'name' => 'agreeterms',
'validators' => array(
array(
'name' => 'Identical',
'options' => array(
'token' => '1',
'messages' => array(
Identical::NOT_SAME => 'You must agree to the terms of use.',
),
),
),
),
));
This won't work if you use the option 'use_hidden_element' => false for the checkbox for the form. If you do this, you'll end up displaying the default NotEmpty message Value is required and can't be empty
This isn't directly related to the question, but here's some zf2 checkbox tips if you're looking to store a user's response in the database...
DO use '1' and '0' strings, don't bother trying to get anything else to work. Plus, you can use those values directly as SQL values for a bit/boolean column.
DO use hidden elements. If you don't, no value will get posted with the form and no one wants that.
DO NOT try to filter the value to a boolean. For some reason, when the boolean value comes out to be false, the form doesn't validate despite having 'required' => false;
Example element creation in form:
$this->add([
'name' => 'cellPhoneHasWhatsApp',
'type' => 'Checkbox',
'options' => [
'label' => 'Cell phone has WhatsApp?',
'checked_value' => '1',
'unchecked_value' => '0',
'use_hidden_element' => true,
],
]);
Example input filter spec:
[
'cellPhoneHasWhatsApp' => [
'required' => false,
],
]
And here's an example if you want to hide some other form fields using bootstrap:
$this->add([
'name' => 'automaticTitle',
'type' => 'Checkbox',
'options' => [
'label' => 'Automatically generate title',
'checked_value' => '1',
'unchecked_value' => '0',
'use_hidden_element' => true,
],
'attributes' => [
'data-toggle' => 'collapse',
'data-target' => '#titleGroup',
'aria-expanded' => 'false',
'aria-controls' => 'titleGroup'
],
]);
I'm a ZF2 fan, but at the end of the day, you just have to find out what works with it and what doesn't (especially with Forms). Hope this helps somebody!
Very old question, but figured it might still be used/referenced by people, like me, still using Zend Framework 2. (Using ZF 2.5.3 in my case)
Jeff's answer above helped me out getting the right config here for what I'm using. In my use case I require the Checkbox, though leaving it empty will count as a 'false' value, which is allowed. His answer helped me allow the false values, especially his:
DO NOT try to filter the value to a boolean. For some reason, when the boolean value comes out to be false
The use case is to enable/disable certain entities, such as Countries or Languages so they won't show up in a getEnabled[...]() Repository function.
Form element
$this->add([
'name' => 'enabled',
'required' => true,
'type' => Checkbox::class,
'options' => [
'label' => _('Enabled'),
'label_attributes' => [
'class' => '',
],
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 0,
],
'attributes' => [
'id' => '',
'class' => '',
],
]);
Input filter
$this->add([
'name' => 'enabled',
'required' => true,
'validators' => [
[
'name' => InArray::class,
'options' => [
'haystack' => [true, false],
],
],
],
])

Populating $form_input with 'select' element options?

I'm trying to understand the data-structure required to populate a
form with 'select' element values (options).
When I dump (Data::Dumper) the FormFu object, I see that the object structure
looks similar to the following:
'name' => 'EmailDL',
'_options' => [
{
'label_attributes' => {},
'value' => 'm',
'container_attributes' => {},
'label' => 'Male',
'attributes' => {}
},
{
'label_attributes' => {},
'value' => 'f',
'container_attributes' => {},
'label' => 'Female',
'attributes' => {}
}
],
Seeing this, I figured that the way to structure $form_input (being that $form_input = \%cgivars) would be something like the following:
'Firstname' => 'Faisal',
'EmailDL' => [
{
'value' => 'myvalue',
'label' => 'mylabel'
}
],
However this doesn't seem to work. I've found that structuring $form_input correctly, and then issuing a $fu->default_values($form_input) to be simple and effective, except in this instance when I'm trying to include the select/options sub-structure.
So the question is: How should I structure 'EmailDL' above to correctly populate 'select' options when doing $fu->default_values($form_input) or $fu->process($form_input)?
To set the options you use the options call,
$fu->get_all_element('EmailDL')->options([ [ 'myvalue', 'mylabel' ],
[ 'val2', 'label2' ] ]);
If you then want to set one of those values you can use the default_values.
$fu->default_values({ EmailDL => 'val2' });
Further help is available here in the Element::Group documentation. Note the code examples are in the text of the help.