Giter Club home page Giter Club logo

Comments (14)

tflight avatar tflight commented on May 27, 2024

You can Return a redirect, is that what you are looking for? On successful login you can return a redirect to the dashboard, or on failure you can return a redirect back to the login page.

from twig-view.

mquestionable avatar mquestionable commented on May 27, 2024

Something like that but I want also to pass some data/errors to that page, how could I do that?

from twig-view.

tflight avatar tflight commented on May 27, 2024

You can use Slim-Flash or another Flash message package to set the error and then retrieve it from the next page view (which is the result of the redirect). You might also store the error/data in the session and then clear it, which is essentially what Slim-Flash is doing.

from twig-view.

tflight avatar tflight commented on May 27, 2024

Something like this...

$this->flash->addMessage('errors', 'Your username or password is incorrect.');
return $response->withRedirect('/login');

from twig-view.

mquestionable avatar mquestionable commented on May 27, 2024

Great! I got it working with Slim-Flash and slim-twig-flash.

But there is one more thing I want to do... This works great for showing messages but what if I want to show also error on specific input?

from twig-view.

tflight avatar tflight commented on May 27, 2024

Like applying a specific class to an input field? When I pass $errors to a view, it is an associative array with field names and error messages. My Twig views might look like this.

<div class="input-group{% if errors.age %} has-error{% endif %}">

from twig-view.

mquestionable avatar mquestionable commented on May 27, 2024

Okay, I think I got this. I'm using Violin for validation and I'm trying somehow to format their messages in view, but I'll probably end up using some helper class.

Thank you so much @tflight

from twig-view.

tflight avatar tflight commented on May 27, 2024

It doesn't look like Violin has been updated in a couple of years. I use Valitron and get the error messages from it fairly easily. When I pass the data to a view I'll do something like

$data = [
    'somemodel' => $model,
    'someUser' => $user,
    'errors' => $validator->errors()
];

from twig-view.

mquestionable avatar mquestionable commented on May 27, 2024

Hmm, thanks, I will switch to that one as I just started the project. In your example, are you passing the whole $data in flash? Can you paste here piece of the actual project code where you are returning errors and other data in view?

from twig-view.

tflight avatar tflight commented on May 27, 2024

I'm not using Slim-Flash, so that might be part of what is different. I'm using a different flash messages package. But no, I'm not passing $data to my flash message, just the specific message along the lines of "Oops, check you're work". That is all I put into flash. The $data goes into the view, and from there I have a Twig partial that checks if an 'errors' variable exists, and if so loops through them which are the errors from Valitron. (Running out for the day now, but I can drop you some code tomorrow if that isn't clear.)

from twig-view.

mquestionable avatar mquestionable commented on May 27, 2024

Yeah, I figured out that part. What I'm wondering then how are you handling rendering view from controller or more specifically passing data to the view from controller (my initial question)?

I'm looking in Bookshelf example app, and I see that they do it with $this->view->render but as I said earlier, it does opens up correct view but URL stays the same, it doesn't go by the routes.

I can also see, example app is using the same function for editAuthor GET and POST, is this how you normally do in Slim? And I see why will this return correct URL and view but what if I want to show some other view and pass data while in different controller?

from twig-view.

mquestionable avatar mquestionable commented on May 27, 2024

I guess I'll be using same the route name for GET and POST when I need to return additional data to the same route. Thanks once again @tflight for your time and patience :)

from twig-view.

tflight avatar tflight commented on May 27, 2024

In the bookshelf app, they pass data to the view from the controller her.

You are correct that it when returning withRedirect it will only accept the URL to redirect to and a status code. (Part of the stateless nature of the web.) So you could add the data you wish to send to the redirected view from a session, perhaps using a flash message. Assuming your session storage message can handle objects, you could add a new flash message with a string, array, or object. Then in the controller that responds to that redirect, check if there is a flash message with the key that contains the array or object, and adjust how the controller responds accordingly.

I typically have different actions (controller methods) respond to GET and POST requests, unlike the bookshelf example. I tend to follow the structure here.

from twig-view.

mquestionable avatar mquestionable commented on May 27, 2024

Yeah, I'm following the same routing structure here too. Thanks

from twig-view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.