Adding page name field to form-express

Permalink
I've been working with the form-express block, however, when I put the express form block into a page type to prevent having 10 separate forms with the same questions, I can't see on which page a form has been submitted. I've looked into the code but wasn't sure if it is possible to add a hidden field containing the page name, so my question is:

Can and if yes how do I add a hidden field to an express-form which saves the page name?

 
mjamil4it replied on at Permalink Reply
You cannot achieve this by a custom template as form get rendered by calling this function:
$renderer->render();

You can only customize before and after the form see the documentation for more details:
https://documentation.concrete5.org/developers/express/express-forms...

Solution:
I have created an additional attribute in the form called PageName, idea is to make this field hidden with the page name its value.

Then I override the Application\elements\form\bootstrap3.php with the following, make sure you update the control id with your control id.

<?php
defined('C5_EXECUTE') or die("Access Denied.");
$c = Page::getCurrentPage();
$hidden = ($view->getControlID() == 'akID[100][value]') ? 'display: none' : '';
?>
<div class="form-group" style="<?= $hidden ?>">
    <?php if ($view->supportsLabel()) { ?>
        <label class="control-label" for="<?=$view->getControlID()?>"><?=$view->getLabel()?></label>
    <?php } ?>
    <?php if ($view->isRequired()) { ?>
        <span class="text-muted small"><?=t('Required')?></span>
    <?php } ?>
    <?php
    if($view->getControlID() == 'akID[100][value]') {
        echo '<input type="text" id="akID[100][value]" name="akID[100][value]" value="'.$c->getCollectionName().'" placeholder="" class="form-control ccm-input-text">';
mnakalay replied on at Permalink Reply
mnakalay
If anybody is still interested in this, I have a new package in the marketplace that lets you do just that.
You can get the current page, current user, hidden checkboxes... and it's extendable so you can grab any information you want and send it along with your form.

https://www.concrete5.org/marketplace/addons/hidden-data-attributes-...
iswelgoed replied on at Permalink Reply
I did eventually fix it using Javascript, it was easier to do.

I made a new template for the express form with a javascript file added, with the following content:
$(document).ready(function(){
    $('.form-group:contains("url_hidden")').children(".form-control").attr("value", window.location.href);
    $('.form-group:contains("url_hidden")').hide();    
});


update:
For this to work, I manually add a field to the form with the name url_hidden, this then gets searched by the javascript file, edited, and hidden.
mnakalay replied on at Permalink Reply
mnakalay
I'm not sure I understand. What is "url_hidden"? As far as I know, Express forms don't let you give your fields attributes or anything like that so I'm not sure where "url_hidden" comes from. Could you explain, please?
And why don't you just hide it with CSS?
iswelgoed replied on at Permalink Reply
It is a bit easier, I add a field to the form with the name:
url_hidden
Then the javascript file searches for that text, ads to the next field the URL of the page.
It then hides the entire parent div where the url_hidden field is in.
cmerritt replied on at Permalink Reply
cmerritt
mnakalay replied on at Permalink Reply
mnakalay
The drawback with this method is the data only appears in the email you receive. It will not be saved in the DB along with your Express object. So depending on the use case it might be okay or not.