Codeigniter form validation for big form (28 fields) - php

I have form consists of 28 fields (client requirement).
I am using Codeigniter framework. I have to type 28 lines of code for form validation.
Is there anything else I can do like creating helper for form validation.
Any help would be appreciated.
Thanks

There is no standard here or a right way to do it and your best option is to create a validation rule for every single input of them in your validation array and maybe you can make a check to categorize some of them like "fname, lname, ..." those have the same validations, but that won't save you anything just added overhead.
But maybe your best shot is to create a function in your base_controller like this:
function set_validation_rules()
{
$this->load->library('form_validation');
$config = array(
array(
'field' => 'firstname',
'label' => $this->lang->line($line.'firstname'),
'rules' => 'trim|required|alpha|min_length[3]|max_length[15]'
),
array(
'field' => 'lastname',
'label' => $this->lang->line($line.'lastname'),
'rules' => 'trim|required|alpha|min_length[3]|max_length[15]'
),
array(
'field' => 'password',
'label' => $this->lang->line($line.'password'),
'rules' => 'trim|required|min_length['.$min.']|max_length['.$max.']'
),
array(
'field' => 'passconf',
'label' => $this->lang->line($line.'password_confirm'),
'rules' => 'trim|matches[password]'
)
);
$this->form_validation->set_rules($config);
}
or create a helper as you suggested, but the most important step is to make a convention for yourself for var/inputs naming and make it a habit so inputs from different view have the same names and pass all inputs to this function for validation after modifying by adding a switch cases to it so every firstname has its own rules and every password has its own and so on, hope you got the idea.

Related

Form Validation required either two fields or a third one

I've searched for a while around the internet and here on StackOverflow.
I've found similar needs, but I'm having a hard time adapting those solutions to my specific need.
I'm using CodeIgniter 2, and I've created a form.
I'm using form_validation to validate each values entered in the fields.
Now, I have 2 fields which are used for a period of time, and a third field used for a fileID.
The user must either fill [the period of time] fields, or the [fileID] field, or both.
$this->form_validation->set_rules('idx_start', 'lang:idx_start', 'trim');
$this->form_validation->set_rules('idx_end', 'lang:idx_end', 'trim');
$this->form_validation->set_rules('fileID', 'lang:fileID', 'trim');
I tried to look into Callbacks and create a custom validation rule but I can't seem to figure out an easy and efficient way for my need.
Thanks in advance for your help.
You could always create different rules arrays. I do this in a couple of projects.
So something like this in your controller:
if($condition1){
$rules = $this->my_model->rules1;
} else {
$rules = $this->my_model->rules2;
}
$this->form_validation->set_rules($rules);
And where $rules1 and $rules2 in 'my_model' would look something like this:
public $rules1 = array(
'idx_start' => array(
'field' => 'idx_start',
'label' => 'start',
'rules' => 'trim|required'
),
'idx_end' => array(
'field' => 'idx_end',
'label' => 'end',
'rules' => 'trim|required'
),
'fileID' => array(
'field' => 'fileID',
'label' => 'file id',
'rules' => 'trim'
)
);
public $rules2 = array(
'idx_start' => array(
'field' => 'idx_start',
'label' => 'start',
'rules' => 'trim'
),
'idx_end' => array(
'field' => 'idx_end',
'label' => 'end',
'rules' => 'trim'
),
'fileID' => array(
'field' => 'fileID',
'label' => 'file id',
'rules' => 'trim|required'
)
);
Or however they need to be. Hope this helps!
Probably there is better way, but this could work imho:
$this->form_validation->set_rules('fileID', 'lang:fileID', 'trim')
if ( $this->form_validation->run() != true ) {
$this->form_validation->set_rules('idx_start', 'lang:idx_start', 'trim');
$this->form_validation->set_rules('idx_end', 'lang:idx_end', 'trim');
}
if ( $this->form_validation->run() == false ) {
// form failed
} else {
// form passed
}
I've used following Add in to CodeIgniter for my specific need.
http://forum.codeigniter.com/thread-22443.html
Works fine for me.
Thanks

Where should I put my form validation in codeigniter in fat model and thin controller approach?

I am confused on where to put the form validation in fat model and thin controller approach in codeigniter. I want to separate the business logic. Is including the form validation destroying the business logic separation?
I would personally keep you're form_validation out of the models, however, you don't have to put all the rules in the controller either.
application/config/form_validation.php
<?php
//array('field' => '', 'label' => '', 'rules' => '')
function arrayf($field, $label, $rules)
{
return array('field' => $field, 'label' => $label, 'rules' => $rules);
}
$config = array(
'recipients/add' => array(
arrayf('title', 'Title', 'required|trim|min_length[3]'),
arrayf('description', 'Description', 'min_length[10]'),
arrayf('amount', 'Amount', 'required|numeric'),
arrayf('date', 'Date', 'required|valid_date'),
),
'recipients/delete' => array(
arrayf('id', 'Id', 'required'),
arrayf('confirm', 'Confirm', 'required'),
),
);
The above is an a basic example.
Then in your controller you would just have:
if ($this->form_validation->run('recipients/add') !== FALSE) {
//Do whatever
}
Notice how the first validation group has the same index as param passed to
$this->form_validation->run('recipients/add')
This way you can use the same validation rules in different controllers without writing them all out again.
Note
The function arrayf() is used to format the array because otherwise you would have to write out each validation rule with the array keys as well e.g.
array('field' => '', 'label' => '', 'rules' => '')
Hope this helps!

Input Filter For Multiple table form in Zend Framework 2?

I am trying to build multiple table form in zend framework 2. I have developed a single form by separating the field using HTML input array for example one field which belongs to table 'pages' looks like something
$this->add(array(
'name' => 'pages[title]',
'attributes' => array(
'type' => 'text',
'id' => 'title'
),
'options' => array(
'label' => 'Title',
),
));
But its filter is
$inputFilter->add($factory->createInput(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
),
),
)));
The reason behind changing name is I am validating field step by step by assigning the values accordingly in my controller like following
$form->setData($this->request->getPost()->pages);
The problem is occurring while getting the array of messages. Form isValid() function is working fine but it returns empty array of messages in case of invalid input. How can I get the messages?
Upon digging deeper I have found out that filter is returning messages but when form is mapping error messages through the name of filter and element and as I am using different names it is unable to map... nay work around for this
And before you ask me to assign the same names to form elements and filters it will won't work because the form can't assign data to element with pages[title] because in request it won't receive any input with name pages[title] rather it will receive array of pages
I would take another approach, Keep your names simple, without adding in array name notation.
then you can validate different steps by using Validation Groups and Fieldsets.
http://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html#validation-groups
If you can give more details about exactly what you're trying to achieve it will be easier to respond.

codeigniter how can I get access to config/form_validation.php data?

I have duplicated data across my config/form_validation.php and my controller.
the field and label from form_validation is the same that I specify in my controller for the id, name and placeholder
Do I have to extract that data to yet a third location and reference it in both of these?
application/config/form_validation.php
$config = array(
'register' => array(
array(
'field' => 'register_username',
'label' => 'Username',
'rules' => 'trim|required|exact_length[5]'
),
.....
application/controllers/mycontroller.php
$this->viewdata['register_username'] = array(
'id' => 'register_username',
'name' => 'register_username',
'type' => 'text',
'placeholder' => 'Username'
);
...
I'm not sure why you have your data duplicated in the controller itself. You might find it easier to use Jamie Rumbelow's model/schema libraries mashup.
This will clean up your model/application structure as a whole. The model extension library itself allows for the automation of CRUD methods.

CodeIgniter 2, DataMapper Validation Rules

I'm using CodeIgniter 2 with DataMapper ORM.
For Users, I have confirm_password and confirm_email fields (plus others) which both aren't fields in the database (table users does not have these fields), but it's just there to show on the sign-up form:
I also have back-end where these 2 fields (confirm_password and confirm_email) do not exist in the form.
public $validation = array(
'first_name' => array(
'label' => 'lang:common_first_name',
'rules' => array('required', 'trim')
),
'last_name' => array(
'label' => 'lang:common_last_name',
'rules' => array('trim')
),
'email' => array(
'label' => 'lang:common_email',
'rules' => array('required', 'trim', 'unique', 'valid_email')
),
'confirm_email' => array(
'label' => 'lang:common_confirm_email',
'rules' => array('matches' => 'email')
),
'password' => array(
'label' => 'lang:common_password',
'rules' => array('required', 'min_length' => 6, 'encrypt')
),
'confirm_password' => array(
'label' => 'lang:common_confirm_password',
'rules' => array('matches' => 'password')
)
);
If I don't make the confirm_email or confirm_email fields required, the validator won't trigger the matches rule.
If I make them required, then the back-end that does not have these fields, triggers the confirm_email and confirm_password, but it shouldn't.
Is it best to include ALL possible validation rules (in the model of course) that we may have in the application?
Is it a good idea to alter these rules in the controller (say remove
confirm_email index from $validation array) when adding user on
back-end?
I appreciate any thoughts.
Thanks
Do you have the latest version of ORM Mapper, according to documentation you can add non database fields.
http://stensi.com/datamapper/pages/validation.html
Also, you can now add validation rules for non-Database Table fields,
such as 'Confirm Email Address' or 'Confirm Password'. For example:
and you don't need required rule for the confirmation fields - as they indicate that filed is required in DB hence the DB error) , - the matches property will do the required validation against the matched field (e.g. if it dosen't match it will throw the error.) In other words you only need required on 'email' field, confirmation_email will throw the error if filed don't match. On empty field you really need to show that email is required.
Finally - you can remove the index, but generally thats not good idea. I would , instead, if above fails - add the form validation rule in controller.

Categories