Welcome Guest, Not a member yet? Register   Sign In
Validation oddities
#1

(This post was last modified: 09-29-2020, 01:22 AM by Fil.)

Hi! I'm learning CI4, I have been using CI for a while but I figured out it's time to (try to) switch Wink

I'm facing some oddities in form validation

app/Controllers/Login.php 

Code:
public function index(){
  $output=array();

  echo view('templates/header', $output);
  echo view('pages/login.php'.$page, $output);
  echo view('templates/footer', $output);
}


public function login_user(){
    $validation = \Config\Services::validation();

    $validation->setRules([
      'user_email' => [
          'label'  => 'my_text_lang.text_formvalidation_email',
          'rules'  => 'trim|required'
      ],
      'user_password' => [
          'label'  => 'my_text_lang.text_formvalidation_password',
          'rules'  => 'trim|required'
      ]
    ]);

    if ($validation->withRequest($this->request)->run() == FALSE) {
        return redirect('Login')->withInput();
    }else{
        echo "test";
    }
}


app/Views/pages/login.php have this code on top:
Code:
$validation = \Config\Services::validation();

      foreach ($validation->getErrors() as $error_description) {
        echo('<div class="alert alert-danger alert-dismissible fade show">'.$error_description.'<button type="button" class="close" data-dismiss="alert" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button></div>');
      }




app/Views/templates/header.php have this code on top: 
this is "if a user is logged in, use his language otherwise if hl=something is set in the url use that (or fallback to English) otherwise just use English"
Code:
<?php
    $request = \Config\Services::request();
    $lang = 'en';
    if(isset(session()->logged_user)){
        $userLanguage = session()->logged_user['userData']['language'];
        $lang = $this->config->languages[$userLanguage];
    }else if($request->getGet('hl') != null){
      switch ($request->getGet('hl')) {
        case '1':
          $lang = 'it';
          break;
        default:
          $lang = 'en';
          break;
      }
      session()->sitelang = $lang;
    }else if(isset(session()->sitelang)){
          $lang = session()->sitelang;
    }

    $language = \Config\Services::language();
    $language->setLocale($lang);
?>



app/Config/Routes.php
Code:
$routes->get('/', 'Login::index');
$routes->add('Login', 'Login::index');


Oddities are:
1) the validation default messages are always localized in Italian. I have downloaded the "it" package from here https://github.com/codeigniter4/translations
and put that in the Lang folder. without it/Validation.php every validation message is in English, with the it/Validatio.php every message is in Italian.
I have labels in my login.php view that are showing correctly in English/italian (for instance <?=lang('my_text_lang.text_login_remember');?> ) so I think the code at the beginning of my header should work.
I have buttons/labels in English but validation errors in Italian...

2) redirect('Login')->withInput(); will bring me to //localhost:8888/MY_CI_PROJECT/public/index.php/Login instead of //localhost:8888/MY_CI_PROJECT/public

3)I'm not completely sure about this but, after triggering the validation, if I manually change the URL to

//localhost:8888/MY_CI_PROJECT/public/Login something redirects me to 

//localhost:8888/Login

opening a new browser and go to //localhost:8888/MY_CI_PROJECT/public/Login doesn't do that... 

I'm developing with MAMP on macOS, htaccess file is the default one.


thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB