flash problem

31 views
Skip to first unread message

amka...@gmail.com

unread,
May 7, 2017, 6:45:31 AM5/7/17
to Mojolicious
Hi,

Something strange happens to me for a flash message.

Have a form, and when this one is correctly filled and the user clic on ok, a calculation is done.

Depending of the result on this, I have to stay on the form and put an (error) flash message to the user.

In this last case, nothing happens on the first clic on ok (the use stays on the same form, no message).

The message is displayed on the second clic...

Can someone please help me ? Thanks  :)


  post '/enreg' => sub {
    my $c = shift;

    my $validation = $c->validation;
    return $c->render unless $validation->has_data;

    $validation->required('mo')->like(qr/^\d*\.?\d*$/)->minimum(0);
    $validation->required('z_1')->like(qr/^\d+$/)->range(5, 250);
    
    my $mots = $c->session('mots');
    
    for (my $i=2 ; $i<=$mots ; $i++) {
      my $j = $i-1;
      $validation->required('cusse_' . $j . '_' . $i)->like(qr/^\d*\.?\d*$/);
      $validation->required('z_' . $i)->like(qr/^\d+$/)->range(5, 250);
    }
    
    return $c->render('derns', mots => $mots) if $validation->has_error;
    
    if (not $validation->has_error) {

      ...
      
      my $error = suite_roues(\%r);
      if ($error) { $c->flash(error_message => "Il n'y a pas de solution au problème tel que vous l'avez posé.", aide => 1) ; return $c->render('derns', mots => $mots) }
    }
    $c->redirect_to('vueprincipale');
  }

@@ derns.html.ep
% layout 'default';

%= form_for enreg => begin
  ...
  %= submit_button 'OK'
% end

% if (my $err_msg = flash 'error_message') {
  <br><font color="red"><b><%= $err_msg %></b></font><br><br>
% }
%= link_to Déconnexion => 'logout'

Stefan Adams

unread,
May 7, 2017, 11:36:36 AM5/7/17
to mojolicious

On Sun, May 7, 2017 at 5:45 AM, <amka...@gmail.com> wrote:
Something strange happens to me for a flash message.

Have a form, and when this one is correctly filled and the user clic on ok, a calculation is done.

Depending of the result on this, I have to stay on the form and put an (error) flash message to the user.

In this last case, nothing happens on the first clic on ok (the use stays on the same form, no message).

The message is displayed on the second clic...

From flash:
Data storage persistent only for the next request, stored in the "session".

Also notice how the documentation shows pairing it with "redirect_to" (a next request).  Rendering, as you're doing, isn't a next request, the rendering is happening on this request.  So rather than flash, I think you just want to stash.

...
if ($error) { return $c->render('derns', mots => $mots, error_message => "Il n'y a pas ...", aide => 1) }
...

@@ derns.html.ep
...
% if (my $err_msg = stash 'error_message') {
...

Now, if you want an error message to show when you redirect_to("vueprincipale") then you'd want to flash.

$c->flash(error => 'message')->redirect_to('vueprincipale');
...

@@ vueprincipale.html.ep
...
if (my $err_msg = flash 'error') {
...

amka...@gmail.com

unread,
May 9, 2017, 3:52:15 PM5/9/17
to Mojolicious
Thanks,

That works now !
Reply all
Reply to author
Forward
0 new messages