• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use customer validation rules and error messages

#1
[eluser]jprateragg[/eluser]
EDIT: Typo in the title. Should be "custom" instead of "customer". I get a license error whenever I try to edit it.

I'm in the process of porting an existing application to CI. I have several forms where I check the values of the form and then set an error message if required. This is from my old application:

Code:
if($member_value == 0 && $adjusted_value == 0)
$error[] = "You must enter at least one member or adjusted value.";

if(($asset_category_id == 4 || $asset_category_id == 15 || $asset_category_id == 16)
&& $description == "") $error[] = "You must enter a description.";

In this example, I don't think it's possible to do this using the standard validation rules. This is more of custom error checking of data entered in by the user. For example, $member_value and $adjusted_value aren't allowed to be zero. And if the $asset_category_id = 4, 15, 16, then the user must enter a $description. I've also set a custom error message in the $error array.

Is this kind of error checking possible? Thanks!

#2
[eluser]CroNiX[/eluser]
Sure, you can create custom validation callback rules (explained in form validation class docs) or extend the form validation library with your own custom rules (explained in Creating your own Libraries in user guide).

The built in rules are for the most basic of validation.

Don't forget that within your validation rules, you can also use $this->input->post() to access other fields that you might need to use to validate this field/rule.

#3
[eluser]jprateragg[/eluser]
Ah. I have lots of fields I will need to do this to. So I'm guessing I'll have to create lots of functions for individual fields to compare post values. I guess that's not that bad as long as I keep all of them in the extended form validation library.

I'm also assuming I can create my own custom error messages for these functions:

Code:
$this->form_validation->set_message('custom_function', 'Error message goes here.');

#4
[eluser]CroNiX[/eluser]
Yes, and you can make them specific to whatever field they were applied to by using %s in the message so the field name will show up (second parameter of what was used in set_rules() for that field is what will show).

A lot of your validation might be reusable if you code it right, so it might not be as much as you are thinking.

#5
[eluser]jprateragg[/eluser]
So I'm doing as you said and just putting the functions in my extended MY_Form_validation.php class:

Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
    public function __construct()
    {
        parent::__construct();
    }

public function asset_description_required() {
  if($this->input->post('member') == 0 and $this->input->post('adjusted') == 0) {
   return false;
  } else {
   return true;
  }
}
    //...add more rules
}

However, I'm getting an error saying "Undefined property: MY_Form_validation::$input." Any ideas why I can't interact with the input class? I don't need to use get_instance do i?

#6
[eluser]jprateragg[/eluser]
Looks like I had to put this in the __construct() function:

Code:
$this->ci = & get_instance();

I assumed all of the CI classes would be available since this extends the CI_Form_validation class.

#7
[eluser]CroNiX[/eluser]
Also remember that the first value that is automatically passed to the validation function as the first parameter is the value of the form field that the rule is currently being run on, which you aren't using. You don't HAVE to, but it IS already automatically passed whether you use it or not.

Code:
$this->form_validation->set_rules('form_field1', 'Form Field 1', 'your_rule');

Code:
function your_rule($val) {} //$val = value of "form_field1"
//same thing as $val = $this->CI->input->post('form_field1');


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.