I have the following code of select form:
$this->add([
'name' => 'username',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => [
'object_manager' => $this->getObjectManager(),
'target_class' => 'Application\Entity\Player',
'property' => 'username',
'is_method' => true,
'empty_option' => '-- Please select Unsername --',
'find_method' => [
'name' => 'getUsername',
],
'label_generator' => function ($targetEntity) {
return $targetEntity->getId() ;
},
],
]);
My questions are:
Is it posible selected value to display as default?
(example: https://www.w3schools.com/tags/att_option_selected.asp)
Is it possible to do submit when changing position
Thank in advance for your help
Related
how can I add an virtual column to the TCA (TYPO3 8)? I have in a 1:n table with data and I want to display the count of the data in the backend to current element.
I need something like this:
$fields = [
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '7d'
]
]
],
'counts30d' => [
'exclude' => false,
'label' => 'last 30 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '30d'
]
]
],
];
pseudo function:
public function myMethod($element, $params){
$sql = "SELECT count(*) FROM TABLE WHERE pid=$element[uid] and date > $params[period]";
return sql_count…
}
The field should only be informative for the backend users.
Does anyone have an idea?
Thanks
Oliver
The TCA field type user is exactly what you are looking for:
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'user',
'userFunc' => \Namespace\MyClass::class . '->MyMethod',
'parameters' => [
'period => '7d',
],
],
],
The TCA field type none is exactly what you are looking for. Type none is the only type that does not necessarily need a database field. To manipulate it you can use userFunc which allow you to use custom php function.
I am getting the error:
The input was not found in the haystack
After form post. Please see the selection tag code lines below:
// Add "roles" field
$this->add([
'type' => 'select',
'name' => 'roles',
'attributes' => [
'multiple' => 'multiple',
'options'=>$this->role_desc,
'inarrayvalidator' => false,
'class'=>'form-control'
],
'options' => [
'label' => 'Role(s)',
],
]);
// Add input for "roles" field
$inputFilter->add([
'class' => ArrayInput::class,
'name' => 'roles',
'required' => true,
'haystack'=>$this->role_ids,
'filters' => [
['name' => 'ToInt'],
],
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
['name'=>'InArray', 'options'=>['haystack'=>$this-
>role_ids]]
],
]);
The InArray seems to be validating well, lm just no sure what is bringing up the exception. Thank you in advance.
Actually , your issue is similar to link
To solve this, change your validators definition to:
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
[
'name' => 'Explode',
'options' => array(
'validator' => [
'name'=>'InArray',
'options'=> [
'haystack'=>$this->role_ids
]
]
)
]
],
Unfortunately, I do not think there is a "cleaner" way to do this.
Alternately, Maybe you could use the MultiCheckbox.
I am trying to create a column that displays a glyphicon. The glyphicon will link to an url which allows the user to download a file. Any help would be massively appreciated. Current code is as follows:
GridView::widget([
'dataProvider' => $dataProvider,
'pager' => [
'class' => 'common\widgets\Pagination',
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Date',
'attribute' => 'call_datetime',
'format' => 'date',
],
[
'label' => 'Time',
'attribute' => 'call_datetime',
'format' => 'time',
],
'call_from',
'call_to',
'duration',
'call_type',
'extension',
[
'label' => 'File',
'attribute' => 'fname',
'value' => 'callRecFiles.fname',
],
It is the last attribute 'fname' that the user will be downloading.
Change your fname field array to:
[
'label' => 'File',
'attribute' => 'fname',
'value' => function($model) {
//here create glyphicon with URL pointing to your action where you can download file, something like
return $model->callRecFiles ? Html::a('Download', ['myController/download-action', 'fname' => $model->callRecFiles->fname]) : null;
}
],
And prepare proper action to allow user to download file.
I use this php code:
public function checkout(&$order, &$response)
{
$settings = Settings::get($order->seller_id);
$readon= 'maksājums';
$readon= $order->id;
$order->custom([
'payment_details' => [
'bank_name' => [ 'label' => $this->_->_('Bank Name'), 'value' => $settings->bank_name ],
'account_owner' => [ 'label' => $this->_->_('Account Owner'), 'value' => $settings->account_owner ],
//'bic' => [ 'label' => $this->_->_('BIC/SWIFT'), 'value' => $settings->bic ],
'iban' => ['label' => $this->_->_('IBAN'), 'value' => $settings->iban ],
'reason' => [ 'label' => $this->_->_('Reason for Payment'), 'value' => $readon],
],
]);
$response = [];
$response['redirect'] = $this->meta('manual_url');
return Payment::STATUS_OK;
}
Problem is that I want to add additional text on value readon. Right now $readon show only order number, but I want that it display text like "Please make payment for Order No.$order->id"
Just concatenate the string with what you need. Like this,
$readon= "Please make payment for Order No. ".$order->id;
I have a moneyFieldset with 2 fields, amount and currency.
class MoneyFieldset ...
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->setHydrator(...);
$this->add(array(
'name' => 'currency',
'type' => 'select',
'options' => array(
'value_options' => \Core\Service\Money::getAvailableCurrencies(true),
),
'attributes' => array(
'value' => \Core\Service\Money::DEFAULT_CURRENCY,
),
));
$this->add(array(
'name' => 'amount',
'type' => 'text',
));
}
}
public function getInputFilterSpecification()
{
$default = [
'amount' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => AmountFilter::class]
],
'validators' => [
]
],
'currency' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => StringToUpper::class]
],
'validators' => [
]
]
];
return \Zend\Stdlib\ArrayUtils::merge($default, $this->filterSpec, true);
}
I'm using moneyFieldset in my other fieldsets like this:
// Price Field
$this->add(array(
'name' => 'price',
'type' => 'form.fieldset.moneyFieldset',
'attributes' => array(
'required' => true,
'invalidText' => 'Please type an amount'
),
'options' => array(
...
),
));
When I set filter like this:
function getInputFilterSpecification()
{
'price' => [
'required' => true,
'allow_empty' => false,
],
}
It's not working because price has 2 fields, so how can I say price[amount] and price[curreny] is required?
You can provide the input specifications for the nested fieldset (within the context of the form) using the following array structure.
public function getInputFilterSpecification()
{
return [
// ...
'price' => [
'type' => 'Zend\InputFilter\InputFilter',
'amount' => [
'required' => true,
],
'currency' => [
'required' => true,
]
],
//...
];
}
If you are dynamically modifying the values of the input filter it might be worth considering creating the validator using a service factory class and then attaching it to a form using the object API rather than arrays.
As I said in #AlexP's comment, a field, or a group of field declared as Required like this :
function getInputFilterSpecification()
{
'price' => [
'required' => true,
'allow_empty' => false,
],
}
Not means that it will be print an html like this :
<input type="text" required="required"/>
It just check when you'll do $form->isValid() if your fields are empty and required or other checks.
To achieve that, you just have to set in attributes that you want to require those fields. As you already did. Attributes can add, same as class attribute, html code to an input.
P.S : AlexP's answer is the best answer I just give more info about it.