Welcome Guest, Not a member yet? Register   Sign In
Disable error message on a single rule in form validation
#1

I want to disable the error message for one rule in form validation. How to do that?

On the following example, if the email is provided but not valid, it should displays a message. 
If the email is not provided, I don't want a message to be displayed. 

Quote:$validation->setRules([
            'email' => [
                'label'  => 'email',
                'rules'  => 'trim|required|valid_email',
                'errors' => [  'valid_email' => 'You must provide a valid email address.',
                                'required' => "I don't want a message here !!!"]
            ]
        ]);

if (!$validation->run())
 {

    //...


I read the following note on this page : https://codeigniter4.github.io/userguide...r-messages
Quote:If you pass the last parameter the labeled style error messages will be ignored.

But it's not clear for me.
Reply
#2

You are looking for the rule named "permit_empty" add that to your rules string. And you can remove trim, it dosen't do anything to my knowledge.
Reply
#3

(This post was last modified: 05-10-2020, 10:01 AM by imabot.)

(05-10-2020, 08:55 AM)jreklund Wrote: You are looking for the rule named "permit_empty" add that to your rules string. And you can remove trim, it dosen't do anything to my knowledge.

That not my question. If the email is not provided, I expect the form validation to fail, but I don't want a error message. Is it possible?
By the way, are data from form automatically trimmed ?
Reply
#4

That kinda what you asked, you only want it to validate if it's not empty. So remove "required" and add "permit_empty".

If it's possible to disable it, it would be by passing "null" or just an empty string for said message.

It's not trimmed, but that rule dosen't trim it, at least not for when you are saving it into the database. It dosen't touch your original data.
Reply
#5

(05-10-2020, 10:11 AM)jreklund Wrote: That kinda what you asked, you only want it to validate if it's not empty. So remove "required" and add "permit_empty".

If it's possible to disable it, it would be by passing "null" or just an empty string for said message.

It's not trimmed, but that rule dosen't trim it, at least not for when you are saving it into the database. It dosen't touch your original data.

I'm migrating my code from CI3 to CI4. I will explain my problem.

I have a login page email/password associated to a single controller, when the user reaches the form for the first time, the form is empty, no message is display.

If the user try to login, but the validation fails, the login page is displayed with the appropriate error message ($validation->getErrors()) and the email is reported in the form with set_value().

Quit standard, CI should deal with it easily. 

The first time the user reaches the page, since there is no data posted, the validation fails and displays an error message because the field email is empty. Of course, I could check by my own if the field is provided without the form validation mechanism. But it was working with CI3.
Reply
#6

Oh, yeah. That's new... It looks for $_GET as well know. Don't think you can turn that off at the moment. You can hack it away with:

PHP Code:
if ($this->request->getMethod() === 'get' || !$validation->run()) 
Reply
#7

(05-10-2020, 01:08 PM)jreklund Wrote: Oh, yeah. That's new... It looks for $_GET as well know. Don't think you can turn that off at the moment. You can hack it away with:

PHP Code:
if ($this->request->getMethod() === 'get' || !$validation->run()) 

hallo @jreklund,
i try your code but it still display error when i load the page for the first time,


in my controller :
PHP Code:
$val $validation->setRules([
            'email' => ['label' => 'Email''rules' => 'required|trim|valid_email'],
            'password' => ['label' => 'Password''rules' => 'trim|required'],
        ]);
        $data['title'] = 'Login Area';
        $data['validation'] = $validation;

        if ($this->request->getMethod() === 'get' || !$val->run()) {
            echo view('auth/login'$data);
        } else {
            $this->_login();
        

in my view
Code:
<div class="form-group">
                                <label>Email Address</label>
                                <input class="au-input au-input--full" type="email" name="email" placeholder="Email">



                                <div class="alert alert-danger" role="alert">

                                    <?= $validation->showError('email'); ?>
                                </div>

                            </div>

i use CI 4.0.3
Reply
#8

You are assigning $validation, but execute $val. It's now to different functions.
Reply
#9

(This post was last modified: 06-04-2020, 06:04 PM by sandywicaksono.)

(06-01-2020, 02:37 PM)jreklund Wrote: You are assigning $validation, but execute $val. It's now to different functions.

Hi,


Code:
$validation = \Config\Services::validation();
        $validation->setRules([
            'email' => ['label' => 'Email', 'rules' => 'required|trim|valid_email'],
            'password' => ['label' => 'Password', 'rules' => 'trim|required'],
        ]);
if ($this->request->getMethod() === 'get' ||  !$validation->run()) {
            echo view('auth/login', $data);
        } else {
            $this->_login();
        }
in my view


Code:
<input class="au-input au-input--full" type="email" name="email" placeholder="Email">
                                <?php if (!$validation->showError('email')) : ?>

                                <?php else : ?>
                                    <div class="alert alert-danger" role="alert">

                                        <?= $validation->showError('email'); ?>
                                    </div>
                                <?php endif; ?>
                            </div>


[align=center]
but it still no work, give me a keyword or references if you know.?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB