Multiple Date Form Element in Drupal 9 - drupal-8

I am trying to add a multiple date field (unlimited cardinality) in my custom module configuration form.
$form['holidayForm']['holidays'] = [
'#type' => 'date',
'#multiple' => TRUE,
'#title' => $this->t('Holidays'),
'#default_value' => $config->get('holidayForm.holidays'),
];
As you can see, I have added the #multiple property but I still get a single field
Is there a different property that I needed to add.

Related

Gravity forms repeater with upload file error

I'm building a form with a repeater and a file upload inside. When I submit the form, the input file filed always give me the error: There was an error while uploading the file. Error code: 1
Add
Here's my code:
$uploadfiles = GF_Fields::create( array(
'type' => 'fileupload',
'id' => 1006, // The Field ID must be unique on the form
'formId' => $form['id'],
'multipleFiles' => false,
'allowedExtensions' => 'gif,jpg,jpeg,png,txt,rtf,pdf,doc,docx,odt,ppt,pptx,odp,xls,xlsx,ods,xml',
'description' => '<br>Relevant documents: our transport way bill, probill, photos, etc.<br>Files must be less than 2 MB.<br>Allowed extensions: gif jpg jpeg png txt rtf pdf doc docx odt ppt pptx odp xls xlsx ods xml.',
'label' => __('Please attach any relevant document', 'srx'),
'pageNumber' => 1, // Ensure this is correct
));
// Create a repeater for the team members and add the name and email fields as the fields to display inside the repeater.
$team = GF_Fields::create( array(
'type' => 'repeater',
'description' => '',
'id' => 1000, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => __('Products', 'srx'),
'addButtonText' => __('Add product', 'srx'),
'removeButtonText' => __('Remove product', 'srx'),
'maxItems' => 100, // Optional
'pageNumber' => 1, // Ensure this is correct
'fields' => array( $reference, $quantity, $product, $lista, $uploadfiles ), // Add the fields here.
) );
Even tho the field is not required, it gives me the error anyways.
Another question I have is about the use of multipleFiles = true. This doesn't work at all. Any idea why ?
Thank you so much !
The Repeater API does not currently support File Upload fields. Here's a full list of known limitations:
https://docs.gravityforms.com/repeater-fields/#limitations
If you need repeating file uploads, check out Gravity Forms Nested Forms:
https://gravitywiz.com/documentation/gravity-forms-nested-forms/

How to render custom form inside a custom block programmatically with drupal 8?

I need render a custom form in a custom block programmatically. This is my code inside a controller:
$form = \Drupal::formBuilder()->getForm('Drupal\wa_encuesta\Form\NewForm', $extra);
[enter image description here][1] $form=render($form);
$blockContent = BlockContent::create([
'info' => $title,
'type' => 'basic',
'body'=>[
'value' => $form,
'format' => 'full_html'
]
]);
$blockContent->save();
//$block = Block::create([
$block = \Drupal\block\Entity\Block::create([
'id' => 'about_us',
'plugin' => 'block_content:' . $blockContent->uuid(),
'region' => 'header',
'provider' => 'block_content',
'weight' => -100,
'theme' => \Drupal::config('system.theme')->get('default'),
'visibility' => array(),
'settings' => [
'label' => 'About us',
'label_display' => FALSE,
],
]);
$block->save();
The form render a custom block but this not working when submit.
I usually get this done by combination of hook_preprocess_block or hook_preprocess_node and twig file.
Example:
Say, you want to render this in a block:
Define hook_preprocess_block() in your theme file:
function THEME_preprocess_block(&$variables) {
$blockId = $variables['elements'][#id];
//check for your block id
$render_service = Drupal::service('renderer');
$form_html = $render_service->renderPlain(Drupal\wa_encuesta\Form\NewForm::class, $extra);
//set in variables
$variables['my_form_html'] = $form_html;
}
Now, identify your twig file name for your block, and just put:
{{ my_form_html }}
please see my answer to the same question here: How to create a form using block module in drupal 8?
Basically you just create a separate form and a block, render the form in the block and then place the block in the desired region.

How to get a full_html textarea field in a Drupal 8 configuration form?

In my module I have a configuration form (ConfigFormBase) with a simple textarea field without formatting that works correctly.
$form['page_message'] = [
'#type' => 'textarea',
'#title' => $this->t('Message'),
'#description' => $this->t('Message display to customer contacts.'),
'#default_value' => $config->get('page_message'),
];
But I would like a textarea full_html field.
Is this possible and how with Drupal 8?
Yes it is possible, but you have to do it like this:
$form['page_message'] = [
'#type' => 'text_format',
'#title' => $this->t('Message'),
'#format' => 'full_html',
'#description' => $this->t('Message display to customer contacts.'),
'#default_value' => $config->get('page_message'),
];
You can check Drupal api for TextFormat.php here, just click on view source and you will have all the info you need.

How to add regex to attribute name in custom validations in Laravel?

In custom validation as shown below:
'custom' => array(
'attribute-name' => array(
'rule-name' => 'custom-message',
),
),
How is it possible to define regex along with the attribute-name-{regex} as well?!
Example:
'custom' => array(
'institute_regex:{/d{1}}' => array(
'required' => 'the field is required',
),
),
I think that might be what you were looking for ages ago, but anyway, here is what I did.
Had to find a way to define custom validation messages (in another language) for inputs dynamically generated in the frontend. The result is that I would get the following type of classes:
project_name-1, -2, -3 etc to keep it simple.
So what I did, and it worked just fine for me was adding the following lines to my validation.php file (whatever the language is):
'project_name-*' => [
'required' => 'Please name your project.',
],
And voilĂ , I hope it will help someone!

How to set the default selected value for a Doctrine ObjectRadio element

I've got a simple user registration form where a user can choose their own user type. The user type maps to a role. This is part of a zf2 application using the doctrine2 module.
The relevant part of the init() method of my user fieldset looks like this:
public function init()
{
// ... other field definitions ...
$roleRadio = new ObjectRadio('role');
$roleRadio->setLabel('What type of user are you?')
->setOptions(
array(
'object_manager' => $this->objectManager,
'target_class' => 'MyUser\Entity\Role',
'property' => 'roleId',
'is_method' => true,
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('userselectable' => true),
'orderBy' => array('displayorder' => 'ASC'),
),
),
)
);
$this->add($roleRadio);
// ... more stuff ...
}
I'm using Doctrine's ObjectRadio class for this element to automatically populate the value options. Is there any way to set the default selected value?
I know I can just do something like this:
$form->get('user')->get('role')->setValue(3);
But I don't want to hard code this and I also don't want to put that kind of logic in my controller.
Any suggestions?
I don't know what do you mean by "I don't want to hard code this", but you can do it as you said in your controller, or you can do it in the form definition by setting attributes as the following:
$roleRadio->setAttributes(array('value' => 3));