How can I make Perl Dancer display my index page properly when the back button is used? - perl

When I fill out a form, press submit, and get the results page everything works perfectly. When I want to go back an fill out a new form the page is broken. It seems to be an amalgam of the index page and the results page. The only way I can get it to work is to re-start the Dancer web engine. Here is a copy of the pm that handles the routes:
package NNSP;
use Dancer2;
use Template;
our $VERSION = '0.1';
get '/' => sub {
template 'index';
};
post '/' => sub {
set layout => 'result_format';
template 'result';
};
true;

I think it's better do
template 'result', $hashref, {layout => 'result_format'};
instead of set layout => 'result_format';
or you should do
set layout => 'default_layout';
in hook 'before' or 'before_template' as set sets global parameters.

Related

In Mojolicious how do I have a route override a static path?

In Mojolicious (or a plugin) I would like to have a /app.js route that - in development mode - would ignore the file in ./public/app.js even if it's there, and serve a response dynamically created by a template instead.
However a basic app that looks like this:
use Mojolicious::Lite -signatures;
get '/' => sub ($c) {
$c->render(template => 'index');
};
get '/app.js' => sub ($c) {
$c->render(template => 'app', format => 'js');
};
app->start;
always serves the static file no matter what - I guess because static routes come first.
Is there a way to do this?

Drupal Node comment redirection when validation fail

I've been banging my head on this trying to find a solution, searching all around for something that would work, but I got no chance.
I have a "dashboard" where users have a list of event they took part in where they can rate/comment the event. I'ts basically a custom comment form for a node that is not displaying on the node page itself. The user click on an icon in their dashboard next to the event they want to comment, they get to the form, fill it and it returns them back to the dashboard. The return is adding parameters with a custom submit function and using the redirect function to make sure the user return to the proper tab in their dashboard.
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'comment_node_event_form') {
$form['#submit'][] = 'customcomment_form_submit';
}
}
function customcomment_form_submit($form, &$form_state) {
if($form['#form_id']=='comment_node_event_form'){
$pos = strpos($_SERVER['HTTP_REFERER'], 'qt-dashboard');
if ($pos !== FALSE) {
$form_state['redirect'] = array(
'dashboard',
array(
'query' => array(
'qt-dashboard' => '2',
'qt-dashboard_event' => '2',
),));
}else{
$form_state['redirect'] = array(
'dashboard',
array(
'query' => array(
'qt-dashboard' => '2',
'qt-dashboard_event' => '1',
),));
}
}
}
This portion is working as it should and expected. The problem is when form validation fails, it send the comment form error message and form to refill to the node page instead of staying where it is.
I found that if I set the #action with the link where my comment form is, it does send the fail to the proper page
$form['#action']='/rating_comment/'.$form['#node']->vid.'?destination=dashboard&qt-dashboard=2&qt-dashboard_event=2';
But, doing so break the redirect when successfully submitting the form and it doesn't take the parameter in the redirect..it basically send the user directly to dashboard and scrapes the parameter. Now there might be a better solution for form validation fail to stay on the same page and that is pretty much what I am looking for.
Thanks
Looks like this form isn’t in your module - and you’re altering the other module.
Now, when the validate function gets invoked at the end you can check for failure and if there is failure cancel processing/redirect etc.
$form_state['no_redirect'] = FALSE:
Also, you can use the error function to check for errors and if so cancel the rest. This goes inside validate method.
if (form_get_errors()) { return FALSE ; }
// .. Otherwise, process validation
Check out the following
https://drupal.stackexchange.com/questions/170815/is-it-possible-to-stop-a-webform-form-during-submission
https://drupal.stackexchange.com/questions/5861/how-to-redirect-to-a-page-after-submitting-a-form

How to add a class to a postLink form - CakePHP 3.4

Is it possible to add a class to the hidden form created by CakePHP's postLink form helper?
Here's my code
Form->postLink(
' ' . __('Delete'),
['action' => 'delete', $this->fetch('item')],
['confirm' => __('Are you sure you want to delete # {0}?', $this->fetch('item'))
,'escape' => false
,'title' => __('Delete')
,'class' => 'btn btn-danger btn-xs isAction'
]) ?>
Note that I'm not looking to add a class to the link that gets created.
Any ideas are welcome!
There's pretty much only one way currently, and that would be to change the formStart template temporarily, something along the lines of this:
// read current template and set the new one
$formStart = $this->Form->getTemplates('formStart');
$this->Form->setTemplates([
'formStart' => '<form class="hiddenFormClass"{{attrs}}>'
]);
echo $this->Form->postLink(/* ... */);
// set the template back to its previous state
$this->Form->setTemplates([
'formStart' => $formStart
]);
It's also possible to reset the templates to their default state using the resetTemplates() method, however this would reset all possible changes made to any of the templates, so it's probably better to play it safe as shown above.
See also
Cookbook > Controllers > Views > Helpers > Form > Customizing the Templates FormHelper Uses
API \Cake\View\Helper\FormHelper::setTemplates()
API \Cake\View\Helper\FormHelper::getTemplates()
API \Cake\View\Helper\FormHelper::resetTemplates()

TYPO3: Use t3lib_TCEforms in frontend plugin

I would like to use as much standard TYPO3 as possible to create a form to edit single records from tx_mytable.
In pi1 i load the tca for the table:
t3lib_div::loadTCA('tx_mytable');
Now I would like to use standard functions to create my form elements more or less like it is done in the backend...
I found this for the front end but cannot find any working examples:
t3lib_TCEforms_fe.php (that extends the normal t3lib_TCEforms)
Is this the right way to go or is there a better way?
I got something working but not really that nice code in the frontend
Here is a link that telss that TCA is not enough but two new entries in the array is needed
http://www.martin-helmich.de/?p=15
It is itemFormElName and itemFormElValue
// include tceforms_fe (place outside class where pipase is included)
require_once(PATH_t3lib.'class.t3lib_tceforms_fe.php');
// load TCA for table in frontend
t3lib_div::loadTCA('tx_ogcrmdb_tasks');
// init tceforms
$this->tceforms = t3lib_div::makeInstance("t3lib_TCEforms_FE");
$this->tceforms->initDefaultBEMode(); // is needed ??
$this->tceforms->backPath = $GLOBALS['BACK_PATH']; // is empty... may not be needed
//////////REPEAT FOR EACH INPUT FIELD/////////
// start create input fields, here just a single select for responsible
// conf used for tceforms similar to but not exactly like normal TCA
$conftest = array(
'itemFormElName' => $GLOBALS['TCA']['tx_ogcrmdb_tasks']['columns']['responsible']['label'],
'itemFormElValue' => 1,
'fieldConf' => array(
'config' => $GLOBALS['TCA']['tx_ogcrmdb_tasks']['columns']['responsible']['config']
)
);
// create input field
$this->content .= $this->tceforms->getSingleField_SW('','',array(),$conftest);
// wrap in form
$output = '<form action="" name="editform" method="post">';
$output .= $this->content;
$output .= '</form>';
// wrap and return output
return $output;
Still looking for a working example with custem template for input fields.

Need help with routing in Mojolicious

I have the "Pages" controller with the "show" method and "Auths" controller with the "check" method which returns 1 if user is authenticated.
I have "default" page ("/profile").
I need to redirect to / if the user is authenticated and redirect all pages to / with the authorization form if the user is not authenticated. My code does not want to work properly (auth based on the FastNotes example application): (
auths#create_form - html-template with the authorization form.
$r->route('/') ->to('auths#create_form') ->name('auths_create_form');
$r->route('/login') ->to('auths#create') ->name('auths_create');
$r->route('/logout') ->to('auths#delete') ->name('auths_delete');
$r->route('/signup') ->via('get') ->to('users#create_form') ->name('users_create_form');
$r->route('/signup') ->via('post') ->to('users#create') ->name('users_create');
#$r->route('/profile') ->via('get') ->to('pages#show', id => 'profile') ->name('pages_profile');
my $rn = $r->bridge('/')->to('auths#check');
$rn->route ->to('pages#show', id => 'profile') ->name('pages_profile');
$rn->route('/core/:controller/:action/:id')
->to(controller => 'pages',
action => 'show',
id => 'profile')
->name('pages_profile');
# Route to the default page controller
$r->route('/(*id)')->to('pages#show')->name('pages_show');
It seems you want / to render either a login form OR a profile page. The code above will always show / as login because it hits that route condition first and will never care if you're authenticated or not.
Try a switch in your initial route for / (your default route after the bridge is unnecessary).
my $r = $self->routes;
$r->get('/' => sub {
my $self = shift;
# Check whatever you set during authentication
my $template = $self->session('user') ? '/profile' : '/login';
$self->render( template => $template );
});
A couple of notes on your example:
Its much easier to help debug issues if you use Mojolicious::Lite for examples.
Try using under instead of bridge.
Try using $r->get(..) instead of $r->route(..)->via(..)
Hope this helps.