CodeIgniter Forums
Form validation: error message - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Form validation: error message (/showthread.php?tid=71773)



Form validation: error message - Serializable - 09-22-2018

Base URL for registration page:
Code:
http://localhost/codeigniter-project/users/register

Registration page:
PHP Code:
<?php echo validation_errors('<p class="bg-danger">'); ?>
<?php 
echo form_open('users/register', ['id' => 'register_form''class' => 'form-horizontal']); ?>
    <div class="form-group">
        <?php
        
echo form_label('First name');
        echo form_input([
            'name' => 'first_name',
            'value' => set_value('first_name'),
            'class' => 'form-control',
            'placeholder' => 'Enter first name',
        ]);
        ?>
    </div>

    ... 

Controller:
PHP Code:
class Users extends CI_Controller
{
    public function register()
    {
        $this->form_validation->set_rules('first_name''First name''trim|required|min_length[2]');
        $this->form_validation->set_rules('last_name''Last name''trim|required|min_length[2]');

        if ($this->form_validation->run() == false) {
            return $this->load->view('layouts/main', ['main_view' => 'users/register']);
        }

        ... 

When I open the URL for the very first time there were no validation errors produced.
But if I try to fill the form and for example provide only 'fisrt name' (and do not provide 'last name'), then validation errors generated and displayed on the register page.
So why were no errors genererated in the beginning, when I open the base url for the very first time?


RE: Form validation: error message - jreklund - 09-22-2018

Because you haven't submitted any data. It can't validate something that aren't there.

It works as intendent.


RE: Form validation: error message - dave friend - 09-22-2018

Loading the page the first time is a GET request. Validation only tests the rules when responding to a POST request and a form "submit" is (should be) a POST operation. (Which is just another way to say what @jreklund said.)


RE: Form validation: error message - Wouter60 - 09-22-2018

Quote:When I open the URL for the very first time there were no validation errors produced.

Validation errors are meant to inform the user that he didn't fill in the form correctly.
Why would you want to show validation errors before the user even made one attempt to fill in the form?


RE: Form validation: error message - Serializable - 09-22-2018

(09-22-2018, 10:39 AM)Wouter60 Wrote: Why would you want to show validation errors before the user even made one attempt to fill in the form?
I just follow the controller code thus it seemed that there should be errors dislayed because validation will false.