Welcome Guest, Not a member yet? Register   Sign In
return error from form
#1

(This post was last modified: 07-23-2020, 03:00 AM by 68thorby68.)

Hi,

I'm trying to use Language files in CI4 for (amongst other things) customs errors. However, I'm not sure how to construct a response to a form submission with a customer error?

PHP Code:
if(empty($a) && !empty($b)) {
return 
response()->with('error'lang('myfile.AisRequiredWithB'));



I'm sure there is a simple process to achieve this?

Any assistance is appreciated
Reply
#2

Download and look at the Myth/Auth AurhController and views and they will show
you how it is done.

Myth:Auth
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Wow,

very cool.

Thank you.

Also looks like I need to use myth:Auth instead of trying to reinvent the wheel.

Many thanks.
Reply
#4

Your very welcome.

A lot of us on here are using it, I use it with Boilerplate which is AdminLTE 3 backend Dashboard.
and I' am adding my Blog into it now.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(07-23-2020, 08:45 AM)InsiteFX Wrote: Your very welcome.

A lot of us on here are using it, I use it with Boilerplate which is AdminLTE 3 backend Dashboard.
and I' am adding my Blog into it now.

Yes I'm getting on with it very well.      Just one question though? 

When I login the user I set additional session data (AuthController::attemptLogin), this works very well.

However, if session expires and user is still logged in ($this->auth->check()) I need to add the additional session data again.

PHP Code:
public function login()
    {
        
// No need to show a login form if the user
        // is already logged in.
        
        
if ($this->auth->check())
        {    
            
//get my data
            //add data to session - this->session->set($data);
            
            
            
$redirectURL session('redirect_url') ?? '/';
            unset(
$_SESSION['redirect_url']);

            return 
redirect()->to($redirectURL);
        }

            // Set a return URL if none is specified
            $_SESSION['redirect_url'] = session('redirect_url') ?? previous_url() ?? '/';

        return 
view($this->config->views['login'], ['config' => $this->config]);
    } 


So I tried to configure a filter to check the session data exists and if not, redirect to login. But I get caught in an infinite loop.

Filter
PHP Code:
public function before(RequestInterface $request) {    
        
$auth service('session');
        
$uri service('uri');;
        
        
            if(
$uri->getPath!='login' 
                        {

                if (!
$auth->get('user_id') )
                {
                return 
redirect('login');
                }
            }
    

I'm not sure why because if I type http://mysite/login direct into browser, all is OK

Any ideas?
Reply
#6

If the session is logging out you will need to add it again, you can up the session timeout

so that this does not happen.

sessionExpiration = 0

Will destroy the session on web browser exit.

seconds -----------------------------|
minutes -----------------------|      |
hours  -------------------|      |      |
days    -----------|        |     |      |
years  ------|      |       |      |      |
                y      d       h     m     s
time () + (20 * 365 * 24 * 60 * 60)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(07-24-2020, 08:06 AM)InsiteFX Wrote: If the session is logging out you will need to add it again, you can up the session timeout

so that this does not happen.

sessionExpiration = 0

Will destroy the session on web browser exit.

seconds -----------------------------|
minutes -----------------------|      |
hours  -------------------|      |      |
days    -----------|        |     |      |
years  ------|      |       |      |      |
                y      d       h     m     s
time () + (20 * 365 * 24 * 60 * 60)
 Many thnaks,  but I was more concerned why there was an infinite loop when trying to redirect to the login page?

Code:
if($uri->getPath!='login' ) 
                        {

                if (!$auth->get('user_id') )
                {
                return redirect('login');
                }
            }
    } 
Reply
#8

Only thing I can think of would be your redirect.

Check your if user_id it might not be removed.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB