Polwatta Plant Nursery
Polwatta Plant Nursery - Sri Lanka
Visit Our New Web Site Info- Polwatta Plant Nursery - Colombo Sri Lanka
Sunday, September 14, 2014
How To Work With CodeIgniter Forms
For a web application, HTML forms are the probably most important sections. Because, these are the real interface which make it happen to communicate and receives/store data from visitors. Now a days, we can’t even imagine a website without at least a contact form or so. As always, codeigniter framework have done an incredible job by providing integrated supports to deal with these forms. Basically, codeigniter support for forms has two sections. A library class, known ‘codeigniter forms validation class’. Another is form helper, which is a set of functions to provide easy way to render HTML forms input controls.hese.
Today, in post tutorial, we will learn:
How to create form validation rule
Render a form with codeigniter form helper And
Process submitted data
As Example, We will consider a traditional contact form to illustrate these.
Create Validation Rule Set:
There are two ways to define rules for a form. You can either have it predefined in a config file. Otherwise, you can define the rules dynamically inside the controller function as well. First is better if you wish to reuse the configuration on different pages/controller functions.
Here is an example rule definition in config:
$config['contact_rules'] = array(
'name' => array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|xss_clean'
),
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email'
),
'subject' => array(
'field' => 'subject',
'label' => 'Subject',
'rules' => 'trim|required|xss_clean'
),
'message' => array(
'field' => 'message',
'label' => 'Message',
'rules' => 'trim|required|xss_clean'
)
);
As you can see on above code, the codeigniter forms configuration is a two-dimensional key/value pair based array. Where, first dimension defines fields and second dimension defines three different property of each item:
field: Defines that name attribute of that particular field.
label: Defines the label text for that field.
rules: Defines the criteria that filed need to be fulfilled. Such as ‘required’ tells the codeigniter form validation class that this field can’t be empty. ‘valid_email’ rule tells to validate the submitted input to a valid email format. ‘trim’ rule actually doesn’t validate anything, rather it just perform an action of trim operation on the submitted input. Similarly, ‘xss_clean’ rule make sure and process input to become safe from security vulnerability.
As an alternative, we can define the rules dynamically in controller function as below:
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'message', 'trim|required|xss_clean');
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment