Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validation form_error() #911

Closed
catwithredhat opened this issue Jan 20, 2018 · 4 comments
Closed

validation form_error() #911

catwithredhat opened this issue Jan 20, 2018 · 4 comments

Comments

@catwithredhat
Copy link

Why theres no helper form_error() like ci3?

https://bcit-ci.github.io/CodeIgniter4/helpers/form_helper.html

The doc describe abt form_error
But theres no function form_error

https://github.com/bcit-ci/CodeIgniter4/blob/develop/system/Helpers/form_helper.php

@lonnieezell
Copy link
Member

You are correct, form_error no longer exists. This is because validation was separated from the form to make it more flexible. The fact that it still exists in the docs is an artifact of porting the docs over from CI3.

See the Validation docs for a (hopefully) up to date explanation.

@akrindev
Copy link
Contributor

so sad heard dat the form_error helper is removed on ci4, it is very helpful

@lonnieezell
Copy link
Member

@akrindev It's not needed. use session() and it's just as simple:

In controller:

if (! $this->validate($rules))
{
    return redirect()->back()->withInput()->with('errors', $userModel->errors());
}

In view:

<div class="invalid-feedback">
    <?= session('errors.pass_confirm') ?>
</div>

@akrindev
Copy link
Contributor

Controller

<?php

namespace App\Controllers;

use CodeIgniter\Controller;

class UserController extends Controller
{  
  public function login()
  {
    helper('form');
    
    echo view('header');
    echo view('login');
    echo view('footer');
  }
  
  public function loginPost()
  {
    if(! $this->validate([
    	'email' => 'required|valid_email',
      	'password' => 'required'
    ]))
    {
   		return $this->login(); 
    }
    
    // everything is fine
    
  }
  
}

view

<div class="form-group form-group-sm">
	<label>email or username</label>
	<input type="text" name="email" class="form-control">
    <?= form_error('email');   ?>
</div>

if we want
helper

if( ! function_exists('form_error'))
{  
    function form_error(string $field = '', string $template = 'single')
    {
      return Services::validation()->showError($field, $template);
    }  	
}

but redirecting with input is fine ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants