CodeIgniter Training 4th part: Forms

Created by Eric Murillo on 2019-06-14

4z.com

 

 

Table of Contents

Presentation. 1

Creation of the form.. 1

Validating the form.. 1

Error messages. 1

Bibliography. 1

End of document. 1

 

Presentation

In this new training we will create records in the database, and to control the information that we will introduce, we will use the validation library and the CodeIgniter forms assistant. Before continuing, make sure you have completed and understood the previous training.

Creation of the form

CodeIgniter provides us with a series of functions that will allow us to show forms in the views. The framework does not require us to use its procedures but using them will help us to have a more orderly and clean code.

First, we will create a new function in the "employees.php" driver, which in this first step, will only call the necessary view:

First, we carry out the loading of this assistant, essential if we want to use its functions. Once in the view, we will create a form to introduce employees in the table we used in the previous chapter.

First, we use the form_open function to create the opening tag of the form, the first argument is the 'action' attribute (CodeIgniter adds the root URL of the site). This function can also be passed an array with other attributes that we want to add (class, id ...), for more information visit the page dedicated to the assistant in the documentation.

The following lines are responsible for showing the controls that we will need to create an employee, in our case we have only used the labels label, input and submit, but the assistant allows us to use all the controls available to HTML.

Now that we have the form ready, what we will do is verify that the data is correct, in our case, that both the name and salary have been entered, that the name exceeds 2 characters long and that the salary is a numeric value.

Validating the form

To carry out these checks we can use the Form Validation library, which allows us to introduce validation rules in the following way:

First we load the library to use the "set_rules" function, in which we pass, in this same order, the attribute "name" of the label, the name it will have in the error message, and the validation functions , separated by the character "|". In this way we indicate that the field is mandatory and that it must be at least 3 characters long (here you can see the rest of the available functions).

Once established the rules, we can check if the form has been sent correctly, using the function "run ()" after the call to "set_rules ()":

With this we can validate our form and send the data to the database, the function would look like this:

Function insert_employee() (employees_model.php):

To check if the data is inserted correctly, you can use the show_home_page function that we developed earlier.

Error messages

If the user makes a mistake when inserting the data, it is a good idea to inform him/her correctly of his error, CodeIgniter automates this with the function "form_error ()". We can use it in the following way in the view:

So, we can see the errors in the form:

Validation

You must provide screenshots of the developed code and screenshots of the different states that a view may have by entering incorrect information in the forms.

Bibliography

https://docs.switzernet.com/3/public/190611-CodeIgniter1/

https://docs.switzernet.com/3/public/190612-CodeIgniter2/

https://docs.switzernet.com/3/public/190613-CodeIgniter3/

https://docs.switzernet.com/3/public/190615-CodeIgniter5/

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mariadb-php-lamp-stack-debian9

https://www.apachefriends.org/index.html

https://www.codeigniter.com/

End of document

 

 

*   *   *

© 4z.com