-
happyape
Member
-
Posts: 83
Threads: 25
Joined: Feb 2015
Reputation:
1
Here is my code -
This gives me "Undefined property: App\Controllers\Home::$validation" error.
Code: public function login()
{
if (! $this->validate([
'email' => 'required',
'password' => 'required'
])) {
echo Services::plates('login', [
'validation' => $this->validation
]);
} else {
//'title' => $this->request->getVar('title'),
}
}
However, I can fix this error using this b
Code: $validation = \Config\Services::validation();
but I think as per documentation I should be able to pass validation object as above.
Another issue is in the view Code: <?= $validation->listErrors() ?>
It just shows me error messages for the given rules above without even submitting the form along with the name of the validation view file name.
Code: 1 ROOTPATH/system/Validation/Views/list.php
The email field is required.
The password field is required.
What am I doing wrong?
-
unodepiera
Israel Parra
-
Posts: 20
Threads: 1
Joined: Nov 2014
Reputation:
3
09-30-2018, 12:26 AM
(This post was last modified: 09-30-2018, 12:27 AM by unodepiera.)
(09-27-2018, 12:47 AM)happyape Wrote: Here is my code -
This gives me "Undefined property: App\Controllers\Home::$validation" error.
Code: public function login()
{
if (! $this->validate([
'email' => 'required',
'password' => 'required'
])) {
echo Services::plates('login', [
'validation' => $this->validation
]);
} else {
//'title' => $this->request->getVar('title'),
}
}
However, I can fix this error using this b
Code: $validation = \Config\Services::validation();
but I think as per documentation I should be able to pass validation object as above.
Another issue is in the view
Code: <?= $validation->listErrors() ?>
It just shows me error messages for the given rules above without even submitting the form along with the name of the validation view file name.
Code: 1 ROOTPATH/system/Validation/Views/list.php
The email field is required.
The password field is required.
What am I doing wrong?
Validation works fine for me:
Some Controller:
PHP Code: $validation = $this->validate(Array rules, Array messages); if ( ! $validation) { return redirect('/register')->withInput(); }
withInput method from CodeIgniter Core:
PHP Code: public function withInput() { $session = $this->ensureSession();
$input = [ 'get' => $_GET ?? [], 'post' => $_POST ?? [], ];
$session->setFlashdata('_ci_old_input', $input);
// If the validator has any errors, transmit those back // so they can be displayed when the validation is // handled within a method different than displaying the form. $validator = Services::validation(); if (! empty($validator->getErrors())) { $session->setFlashdata('_ci_validation_errors', serialize($validator->getErrors())); }
return $this; }
Register View:
PHP Code: <?php echo service('validation')->listErrors() ?>
If you want access to validator from some controller you can do it:
-
unodepiera
Israel Parra
-
Posts: 20
Threads: 1
Joined: Nov 2014
Reputation:
3
09-30-2018, 10:48 AM
(This post was last modified: 09-30-2018, 11:40 AM by unodepiera.)
(09-30-2018, 03:57 AM)InsiteFX Wrote: This has to do with the News Tutorial. Try that and tell me what you get.
You are right InsiteFX, the documentation is wrong, the validation accept an array of rules, but $this->request is an object, this works:
PHP Code: public function create() { helper('form'); $model = new \App\Models\News();
if (! $this->validate([ 'title' => 'required|min[3]|max[255]', 'text' => 'required' ])) { echo view('templates/header', ['title' => 'Create a news item']); echo view('news/create'); echo view('templates/footer');
} else { $model->save([ 'title' => $this->request->getVar('title'), 'slug' => url_title($this->request->getVar('title')), 'text' => $this->request->getVar('text'), ]); echo view('news/success'); } }
-
happyape
Member
-
Posts: 83
Threads: 25
Joined: Feb 2015
Reputation:
1
09-30-2018, 02:46 PM
(This post was last modified: 09-30-2018, 02:46 PM by happyape.)
(09-30-2018, 10:48 AM)unodepiera Wrote: (09-30-2018, 03:57 AM)InsiteFX Wrote: This has to do with the News Tutorial. Try that and tell me what you get.
You are right InsiteFX, the documentation is wrong, the validation accept an array of rules, but $this->request is an object, this works:
PHP Code: public function create() { helper('form'); $model = new \App\Models\News();
if (! $this->validate([ 'title' => 'required|min[3]|max[255]', 'text' => 'required' ])) { echo view('templates/header', ['title' => 'Create a news item']); echo view('news/create'); echo view('templates/footer');
} else { $model->save([ 'title' => $this->request->getVar('title'), 'slug' => url_title($this->request->getVar('title')), 'text' => $this->request->getVar('text'), ]); echo view('news/success'); } }
I just tested this.
My controller method -
PHP Code: public function login() { $validation = $this->validate([ 'email' => 'required|valid_email', 'password' => 'required|min_length[8]']); if ( ! $validation) { echo view('form'); } else { echo 'Valid.'; } }
in my views/form.php
Code: <?php echo service('validation')->listErrors() ?>
<form action="/login" method="post" class="dsform">
<h1 class="h3 mb-3 font-weight-normal">Please login</h1>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="text" id="inputEmail" name="email" class="form-control" placeholder="Email address" autofocus value="">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" value="">
<button class="btn btn-success btn-large btn-block" type="submit">Sign in</button>
</form>
It all works fine except when you open login page it shows you error list before even submitting the form.
I also noticed you mentiond this validation rule ...
Code: 'title' => 'required|min[3]|max[255]',
Min and max don't seem to be valid rule as they are min_length and max_length https://bcit-ci.github.io/CodeIgniter4/l...hlight=max
-
unodepiera
Israel Parra
-
Posts: 20
Threads: 1
Joined: Nov 2014
Reputation:
3
09-30-2018, 10:47 PM
(This post was last modified: 09-30-2018, 10:52 PM by unodepiera.)
(09-30-2018, 02:46 PM)happyape Wrote: (09-30-2018, 10:48 AM)unodepiera Wrote: (09-30-2018, 03:57 AM)InsiteFX Wrote: This has to do with the News Tutorial. Try that and tell me what you get.
You are right InsiteFX, the documentation is wrong, the validation accept an array of rules, but $this->request is an object, this works:
PHP Code: public function create() { helper('form'); $model = new \App\Models\News();
if (! $this->validate([ 'title' => 'required|min[3]|max[255]', 'text' => 'required' ])) { echo view('templates/header', ['title' => 'Create a news item']); echo view('news/create'); echo view('templates/footer');
} else { $model->save([ 'title' => $this->request->getVar('title'), 'slug' => url_title($this->request->getVar('title')), 'text' => $this->request->getVar('text'), ]); echo view('news/success'); } }
I just tested this.
My controller method -
PHP Code: public function login() { $validation = $this->validate([ 'email' => 'required|valid_email', 'password' => 'required|min_length[8]']); if ( ! $validation) { echo view('form'); } else { echo 'Valid.'; } }
in my views/form.php
Code: <?php echo service('validation')->listErrors() ?>
<form action="/login" method="post" class="dsform">
<h1 class="h3 mb-3 font-weight-normal">Please login</h1>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="text" id="inputEmail" name="email" class="form-control" placeholder="Email address" autofocus value="">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" value="">
<button class="btn btn-success btn-large btn-block" type="submit">Sign in</button>
</form>
It all works fine except when you open login page it shows you error list before even submitting the form.
I also noticed you mentiond this validation rule ...
Code: 'title' => 'required|min[3]|max[255]',
Min and max don't seem to be valid rule as they are min_length and max_length https://bcit-ci.github.io/CodeIgniter4/l...hlight=max
It is the expected behavior, you are validating the form each time that the login method is called, you only need to validate the form if it is a POST request, refactor your code:
PHP Code: public function login () { // if ($this->request->getPost()) { Also works if ($this->request->getGetPost()) { $validation = $this->validate( [ 'email' => 'required|valid_email', 'password' => 'required|min_length[8]']); ] ); if ( ! $validation) { return redirect('/login')->withInput(); } else { //TODO: login user } } else { return view( 'login'); } }
|