-
pippuccio76 Senior Member
   
-
Posts: 547
Threads: 231
Joined: Jun 2017
Reputation:
2
Hi in ci3 i have this for validation :
PHP Code: $this->form_validation->set_rules('email','Email',"required|trim|min_length[0]|max_length[100]|valid_email|is_unique[users.email]",array('min_length'=>'Lunghezza minima Email 0 caratteri', 'max_length'=>'Lunghezza massima Email 100 caratteri','required'=>'Email obbligatorio','valid_email'=>'Inserisci una mail valida ')); $this->form_validation->set_rules('promemoria_votazione','Promemoria votazione',"required|trim|min_length[0]|max_length[1]",array('min_length'=>'Lunghezza minima Promemoria votazione 0 caratteri', 'max_length'=>'Lunghezza massima Promemoria votazione 1 caratteri','required'=>'Promemoria votazione obbligatorio')); $this->form_validation->set_rules('risultati','Risultati',"required|trim|min_length[0]|max_length[1]",array('min_length'=>'Lunghezza minima Risultati 0 caratteri', 'max_length'=>'Lunghezza massima Risultati 1 caratteri','required'=>'Risultati obbligatorio'));
Than i have an if ...
PHP Code: if($this->form_validation->run()){
in ci 4 i see instead :
$this->form_validation->set_rules i must use $validation->setRule ( before $validation = \Config\Services::validation(); )
and instead of this->form_validation->run() i must use $validation->run()
how can i set my own error message ?
-
jreklund Administrator
      
-
Posts: 1,408
Threads: 3
Joined: Aug 2017
Reputation:
43
-
pippuccio76 Senior Member
   
-
Posts: 547
Threads: 231
Joined: Jun 2017
Reputation:
2
i try to create my own auth this is my login view :
PHP Code: <?= $this->extend('templates/layout_user_no_menu') ?>
<?= $this->section('content') ?> <style type="text/css"> body{ padding-top:4.2rem; padding-bottom:4.2rem; background:rgba(0, 0, 0, 0.76); } a{ text-decoration:none !important; } h1,h2,h3{ font-family: 'Kaushan Script', cursive; } .myform{ position: relative; display: -ms-flexbox; display: flex; padding: 1rem; -ms-flex-direction: column; flex-direction: column; width: 100%; pointer-events: auto; background-color: #fff; background-clip: padding-box; border: 1px solid rgba(0,0,0,.2); border-radius: 1.1rem; outline: 0; max-width: 500px; } .tx-tfm{ text-transform:uppercase; } .mybtn{ border-radius:50px; }
.login-or { position: relative; color: #aaa; margin-top: 10px; margin-bottom: 10px; padding-top: 10px; padding-bottom: 10px; } .span-or { display: block; position: absolute; left: 50%; top: -2px; margin-left: -25px; background-color: #fff; width: 50px; text-align: center; } .hr-or { height: 1px; margin-top: 0px !important; margin-bottom: 0px !important; } .google { color:#666; width:100%; height:40px; text-align:center; outline:none; border: 1px solid lightgrey; } form .error { color: #ff0000; }
</style>
<div class="container"> <div class="row"> <div class="col-md-5 mx-auto"> <div id="first"> <div class="myform form "> <div class="logo mb-3"> <div class="col-md-12 text-center"> <h1>Login</h1> </div> </div> <?php if(!empty($validation)) :?> <div class='col-md-12 text-center'>
<div class='alert alert-danger'> <span class='login_error'><?= $validation->listErrors(); ?></span> </div>
</div>
<?php endif; ?>
<?php $attributes = array('class' => '', 'id' => 'myform', 'name' => 'login', 'method' => 'POST', //'onSubmit' => 'return controlla_e_invia();', 'autocomplete' => 'off'
); echo form_open('/user/login', $attributes);
?> <div class="form-group"> <label for="email">Email</label> <input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email"> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" name="password" id="password" class="form-control" aria-describedby="emailHelp" placeholder="Enter Password"> </div> <div class="form-group"> <p class="text-center">By signing up you accept our <a href="#">Terms Of Use</a></p> </div> <div class="col-md-12 text-center "> <button type="submit" class=" btn btn-block mybtn btn-primary tx-tfm">Login</button> </div> <div class="form-group"> <p class="text-center">Non hai un account? <a href="/user/registrazione" id="signup">Registrati Qui</a>| <a href="/user/reset_pw">Pasword Dimenticata</a></p> </div> </form> </div> </div> </div> </div> </div>
<?= $this->endSection() ?>
This is the controller method :
PHP Code: public function login() {
$data=[];
$model = new UsersModel(); //se è settato l'user id in sessione //lo reindirizzo al profilo //o alla pagina user bannato
if (isset($_SESSION['user_id'])) {
$user =$model->find($id) ;
switch ($user->id_stato_utenti) { case 1: redirect()->to('user/user_stato/1'); break; case 2: redirect()->to('user/user_stato/2'); break; case 3: redirect()->to('user/user_stato/3'); break; case 4: redirect()->to('user/user_stato/4'); break; case 5: redirect()->to('user/user_stato/5'); break; case 6: redirect()->to('user/user_stato/6'); break; default: redirect()->to('user/index'); break; }
}else if (isset($_COOKIE['user_id'])) { //Checks for cookie
$id_user = $_COOKIE['user_id'];
$user = $model->find($id_user);
$_SESSION['user_id'] = $user->id; $_SESSION['user_id_stato_utenti'] = $user->id_stato_utenti; $_SESSION['user_username'] = $user->username; $_SESSION['user_email'] = $user->email;
}else{
$validation = \Config\Services::validation(); $validation->setRules([
'email'=>[ 'label'=>'Email', 'rules'=>'required|trim|min_length[0]|max_length[100]|valid_email|validateEmail', 'errors'=>[ 'min_length'=>'Lunghezza minima Email 0 caratteri', 'max_length'=>'Lunghezza massima Email 100 caratteri', 'required'=>'Email obbligatorio', 'valid_email'=>'Inserisci una mail valida ', 'validateEmail' => 'Email non presente !!!' ] ], 'password'=>[ 'label'=>'Password', 'rules'=>'required|trim|min_length[0]|max_length[255]|validatePassword[email,password]', 'errors'=>[ 'min_length'=>'Lunghezza minima Password 0 caratteri', 'max_length'=>'Lunghezza massima Password 255 caratteri', 'required'=>'Password obbligatorio', 'validatePassword' => 'Password non corretta!!!' ] ], ]);
if($validation->run()) {
redirect()->to('user/pannello_user');
}
}
$data['validation'] = $validation;
echo view('empty_view',$data); echo view('user/login');
}
If i insert email or password i have always the error of fields required(email and passord)
Why ?
-
jreklund Administrator
      
-
Posts: 1,408
Threads: 3
Joined: Aug 2017
Reputation:
43
08-17-2020, 09:09 AM
(This post was last modified: 08-17-2020, 09:12 AM by jreklund.)
Because you are redirecting, killing your data.
PHP Code: if($validation->run()) {
redirect()->to('user/pannello_user');
}
I think you can use withInput() but then you need to use the old() function as well or use the form_helper for everything.
https://codeigniter.com/user_guide/gener...l#redirect
https://codeigniter.com/user_guide/gener...s.html#old
Take a look at the tutorial.
https://codeigniter.com/user_guide/tutorial/index.html
PHP Code: public function create() { $model = new NewsModel();
if ($this->request->getMethod() === 'post' && $this->validate([ 'title' => 'required|min_length[3]|max_length[255]', 'body' => 'required' ])) { $model->save([ 'title' => $this->request->getPost('title'), 'slug' => url_title($this->request->getPost('title'), '-', TRUE), 'body' => $this->request->getPost('body'), ]);
echo view('news/success');
} else { echo view('templates/header', ['title' => 'Create a news item']); echo view('news/create'); echo view('templates/footer'); } }
-
jreklund Administrator
      
-
Posts: 1,408
Threads: 3
Joined: Aug 2017
Reputation:
43
Sorry about that, missed the part where it said you didn't post any data.
Should be:
PHP Code: if($validation->withRequest($this->request)->run()) {
https://codeigniter.com/user_guide/libra...ithrequest
Or you need to manually input an $data array:
PHP Code: if($validation->run($data)) {
Where $data have been grabbed from the POST.
PHP Code: $data = [ 'email' => $this->request->getPost('email'), 'password' => $this->request->getPost('password'), ];
|