Custom form validation error message for Codeigniter 2 - php

I have a drop down named "business_id".
<select name="business_id">
<option value="0">Select Business</option> More options...
</select>
Here comes the validation rule, user must select an option.
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]');
Problem being the error message says: The Business field must contain a number greater than 0. Not very intuitive! I want it to say "You must select a business".
I tried:
$this->form_validation->set_message('Business', 'You must select a business');
But CI complete ignores this. Does anyone have a solution for this?

I had the same requirement for adding custom form validation error messages in codeigniter 2 (e.g. "You must agree to our Terms & Conditions"). Naturally it would be wrong to override the error messages for require and greater_than as it would erroneously produce messages for the rest of the form. I extended the CI_Form_validation class and have overridden the set_rules method to accept a new 'message' parameter:
<?php
class MY_Form_validation extends CI_Form_validation
{
private $_custom_field_errors = array();
public function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
// Execute the parent method from CI_Form_validation.
parent::_execute($row, $rules, $postdata, $cycles);
// Override any error messages for the current field.
if (isset($this->_error_array[$row['field']])
&& isset($this->_custom_field_errors[$row['field']]))
{
$message = str_replace(
'%s',
!empty($row['label']) ? $row['label'] : $row['field'],
$this->_custom_field_errors[$row['field']]);
$this->_error_array[$row['field']] = $message;
$this->_field_data[$row['field']]['error'] = $message;
}
}
public function set_rules($field, $label = '', $rules = '', $message = '')
{
$rules = parent::set_rules($field, $label, $rules);
if (!empty($message))
{
$this->_custom_field_errors[$field] = $message;
}
return $rules;
}
}
?>
With the above class you would produce your rule with a custom error message like so:
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', 'You must select a business');
You may also use '%s' in your custom message which will automatically fill in the label of fieldname.

If you'd like to customize the error messages that are displayed with each rule, you can find them in an array at:
/system/language/english/form_validation_lang.php

Try not setting the value attribute on the default select...
<select name="business_id">
<option value>Select Business</option> More options...
</select>
and then just using required for your form validation rule...
$this->form_validation->set_rules('business_id', 'Business', 'required');
I suppose you could try editing the way that you're trying to set the message also...
$this->form_validation->set_message('business_id', 'You must select a business');
instead of
$this->form_validation->set_message('Business', 'You must select a business');
I'm not entirely sure if that will do the trick though.

You should extend the Form_validation library as Anthony said.
For instance, I do something like this in a file called MY_Form_validation.php which should be put on /application/libraries
function has_selection($value, $params)
{
$CI =& get_instance();
$CI->form_validation->set_message('has_selection', 'The %s need to be selected.');
if ($value == -1) {
return false;
} else {
return true;
}
}
In your case, because your first option (Guidance option - Please select ...) has the value of 0, you may want to change the conditional statement from -1 to 0. Then, from now on, you could have this line to check selection value:
$this->form_validation->set_rules('business_id', 'Business', 'has_selection');
Hope this helps!

Here's a simple CI2 callback function that I used. I wanted something other than just 'required' as a default param of the validation. The documentation helps: http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks
public function My_form() {
...Standard CI validation stuff...
$this->form_validation->set_rules('business_id', 'Business', 'callback_busid');
...
if ($this->form_validation->run() == FALSE) {
return false;
}
else {
...process the form...
$this->email->send();
}
} // Close My_form method
// Callback method
function busid($str) {
if ($str == '') {
$this->form_validation->set_message('business_id', 'Choose a business, Mang!');
return FALSE;
}
else {
return TRUE;
}
} // Close the callback method
For your case you could change the callback to check for if($str<0) - I'm assuming you've used numbers in your selection/dropdown menu.
If the callback returns false, the form is held and the error message is shown. Otherwise, it's passed and sent to the 'else' of the form method.

A little hack might not good for you but I have done this for a little change.
Example,
I want to change message 'The Email field must be a unique value'.
I have done this as
<?php
$error = form_error('email');
echo str_replace('field must be a unique value', 'is already in use.', $error);
// str_replace('string to search/compare', 'string to replace with', 'string to search in')
?>
If string found then it prints our custom message else it will display error message as it is like 'The Email field must be a valid email' etc...

for those of you working on CodeIgniter 3 you can do the following:
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', array(
'greater_than' => 'You must select a business',
));
And if you are using CodeIgniter 2, you will need to extend and override the CI_Form_validation class (https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html for more info on how to do so) with the new CodeIgniter 3 CI_Form_validation class and use the function above.

The name of the rule is the last parameter.
Please try:
$this->form_validation->set_message('greater_than[0]', 'You must select a business');
More info:
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

I extended the form_validation library with a simple function that makes sure a drop down box does not have its default value selected. Hope this helps.
application/libraries/MY_Form_validation.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {
function __construct()
{
parent::__construct();
$this->CI->lang->load('MY_form_validation');
}
/**
* Make sure a drop down field doesn't have its default value selected.
*
* #access public
* #param string
* #param field
* #return bool
* #author zechdc
*/
function require_dropdown($str, $string_to_compare)
{
return ($str == $string_to_compare) ? FALSE : TRUE;
}
}
application/language/english/MY_Form_validation_lang.php
$lang['require_dropdown'] = 'The %s field must have an item selected.';
How to Use:
1) Make your form drop down box:
<select name="business_id">
<option value="select">Select Business</option> More options...
</select>
2) Create Validation Rule. You might be able to set the value to 0 and use require_dropdown[0] but I've never tried that.
$this->form_validation->set_rules('business_id', 'Business', 'require_dropdown[select]');
3) Set your custom message: (Or skip this step and use the one in the language file.)
$this->form_validation->set_message('business_id', 'You must select a business');

Create Method username_check call back function
01.
public function username_check($str)
{
if ($str=="")
{
$this->form_validation->set_message('username_check', 'Merci d’indiquer le nombre d’adultes');
return FALSE;
}
else
{
return TRUE;
}
}
--
02. Then Put this Validation Code on your class
$this->form_validation->set_rules('number_adults', 'Label Name','Your Message',) 'callback_username_check');
This may help you

Related

Codeigniter: Custom validation is not executed

I'm trying to create my custom validation which is customer ID is required if certain option is selected. Right now, I just want to test if the custom validation is working so I don't care about the options and only set message and always return false. I don't want to put the validation in my controller for MVC pattern reason. It doesn't work if my custom validation is put in model, so I created a new validation file in libraries folder called MY_Form_validation.
MY_Form_validation.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
protected $CI;
function __construct($rules = array())
{
parent::__construct($rules);
}
public function customer_required($str)
{
$this->set_message('customer_required', 'Customer is required if you choose option A');
return false;
}
}
in model, I call it like this:
public function save()
{
/* other form validation */
$this->form_validation->set_rules('customer_id', 'Customer', 'customer_required');
return $this->form_validation->run();
}
I also put it in autoload
$autoload['libraries'] = array('session','database','table','form_validation', 'MY_Form_validation');
It should always fail to save because the validation only return false. But it looks like the custom validation is not executed at all because it always return true. Is there something that I missed? It's been days and I still don't know where I did wrong. Please help.
Update
As Marleen suggested, I tried using callable but again, function check_customer doesn't seem executed because I have a successful save.
Customer_model
$this->form_validation->set_rules('customer_is_required', array($this->customer_model, 'check_customer'));
$this->form_validation->set_message('customer_is_required', 'Customer is required of you choose option A');
private function check_customer()
{
return false;
}
Your method is not being triggered because your customer_id field is submitted empty. Codeigniter does not validate empty fields unless the rule is one of required/isset/matches or a callback or a callable. (See Form_validation.php line 700.)
If you specify your rule as a callable it can remain in the model and will be executed, even if the field is submitted empty:
$this->form_validation->set_rules('customer_id', 'Customer', array(
array($this->your_model, 'customer_required')
));
(See also: https://codeigniter.com/userguide3/libraries/form_validation.html#callable-use-anything-as-a-rule )
$this->form_validation->set_rules('customer_is_required', 'Customer', array(
array($this->customer_model, 'check_customer')
));
public function check_customer($str) {
return false;
}
And to add the message, use:
$this->form_validation->set_rules('customer_is_required', 'Customer', array(
array('customer_is_required', array($this->customer_model, 'check_customer'))
));
$this->form_validation->set_message('customer_is_required', 'Customer is required of you choose option A');

CakePHP 2.x: How to manually set validationErrors without a model?

After reading cakePHP: how set error validation to input field manually in controller, I was wondering how to show a validationError from a controller if we use a form without a model?
So for example, we have a view checkSomething.ctp, with a Form that we can submit.
echo $this->Form->create(false); // If we put "SomeModel" here, it would work.
echo $this->Form->input("myField");
And say we are on /Home/CheckSomething/.
class HomeController extends AppController{
public function CheckSomething(){
// So manually validate a field
if(strlen($this->request->data["myField"]) < 5){
// myField is not valid, so we need to show an error near that field
$this->SomeModel->invalidateField("myField", "You must enter at least 5 characters");
// How to do this?
}
}
}
We cannot use a model here... How to set a validationError for a field without a model? How to manually invalidate a field that comes from such a form?
The easiest way would be to send the error to the view directly:
$errors = [];
if (strlen($this->request->data["myField"]) < 5) {
$errors['myField'] = 'You must enter at least 5 characters';
}
$this->set('errors', $errors);
And in your view:
echo $this->Form->create(false);
echo $this->Form->input('myField', [
'error' => isset($errors['myField']) ? $errors['myField'] : false
]);

is_unique for codeigniter form validation

I'm trying to figure out how I can use the is_unique rule from the Codeigniter form validation library in the following situation.
I'm trying to submit a edit user form and have the rule:
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique[users.user_name]');
What if other values in the form are being changed but this value stays the same. The form is going to see that this value already exists so how would I protect it from editing if this value isn't changed.
Using your code as an example, the is_unique validation rule works by looking for a field called user_name in your users database table. If the field with the same value exists it validates as false.
To make sure it runs only when the user submits a new value, you could check the posted value $this->input->post('user_name') against the value you pulled from the database to populate your form with. If they are the same, don't validate is_unique;
if($this->input->post('user_name') != $original_value) {
$is_unique = '|is_unique[users.user_name]'
} else {
$is_unique = ''
}
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);
There's a better way to go around it, I think, still using CodeIgniters' validation library...
Use edit_unique where you pass an extra parameter which is the id of the row you're editing.. See below.. I use it and works pretty fine for me.. hope it helps
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
$something = $this->input->post('something');
$this->form->validation->set_rules('something','Something','xss_clean|is_unique['tbl'.users]');
if($this->form_validation->run()== FALSE){
}
Simple Way
Just Change isset to is_object in system/libraries/form_validation.php
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
return is_object($this->CI->db) //default isset
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
Here's an easy method that worked for me and uses well documented code (thanks to https://github.com/ivantcholakov for sharing it!). I found it referenced at https://github.com/bcit-ci/CodeIgniter/issues/3109#issuecomment-46346280
Download https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php (MIT licensed) and save it to your application at application\libraries\MY_Form_validation.php
Delete these two lines from __construct():
$this->CI->load->helper('checkbox');
$this->CI->load->helper('email');
Delete all the functions except __construct() and unique().
At the end of the __construct() method of your controller add this line:
$this->load->library('form_validation');
As per the documentation of the unique() method update your validation rule to add a "unique" rule like this (e.g. if you already have required and trim rules):
…|required|unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]|trim...
Extend Form_validation.php library create class inside of application/libraries file name MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation{
protected $ci;
public function __construct($config = array()){
parent::__construct($config);
$this->ci =& get_instance();
}
public function is_unique_update($str, $field){
$explode=explode('#', $field);
$field_name=$explode['0'];
$field_id_key=$explode['1'];
$field_id_value=$explode['2'];
sscanf($field_name, '%[^.].%[^.]', $table, $field_name);
if(isset($this->ci->db)){
if($this->ci->db->limit(1)->get_where($table, array($field_name => $str,$field_id_key=>$field_id_value))->num_rows() === 0){
$this->ci->form_validation->set_message('is_unique_update', 'The {field} field must contain a unique value.');
return false;
}
return true;
}
}
}
Now in your controller
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique_update[users.user_name#id#'.$id.']');
"#" I used for explode the string
where id is primary key of users table
and $id is the value of id.
Now you can use this is_unique_update validation in any controller.
This question is very old but maybe some new people experience this problem and this is the solution for it.
I bet your are using Modular Extensions (HMVC) and you have created a new library, MY_Form_validation. You did id for callbacks, so you have this line of code on your class in order to use callbacks:
$this->form_validation->CI =& $this;
Well, the solution to this is whenever you want to use "is_unique" you must delete this line of code "$this->form_validation->CI =& $this;" from the class. I have experienced this problem and i fix it this way, it works fine now.
If you realy want to use callbacks "$this->form_validation->CI =& $this;", then do it only on required "methods" / "functions" where you don't want to use is_unique.
This code helpful for unique validation to create and update function...
In controller
Add this form validation code in both create and update function
$this->form_validation->set_rules('order_no', 'Order no', 'required|callback_check_order_no');
Add this call back function in controller
function check_order_no($order_no) {
if($this->input->post('id'))
$id = $this->input->post('id');
else
$id = '';
$result = $this->Data_model->check_unique_order_no($id, $order_no);
if($result == 0)
$response = true;
else {
$this->form_validation->set_message('check_order_no', 'Order no already exist');
$response = false;
}
return $response;
}
In model
function check_unique_order_no($id = '', $order_no) {
$this->db->where('order_no', $order_no);
$this->db->where('status', "A");
if($id) {
$this->db->where_not_in('id', $id);
}
return $this->db->get('delivery_order')->num_rows();
}
I'm using codeigniter3 and it shows me error when I check username on updating the value, is_unique is not designed to work with update scenario
so using #Anthony Mutisya's answer, here is the complete solution
in your controller, add this line while validation username of the current user with the database
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
You can get that $id from your submited form.
Now, add the following function to /system/libraries/Form_Validation.php this file. System folder is present in your root of CodeIgniter3 folder.
/**
* edit_unique // for check on update value
*
* Check if the input value doesn't already exist
* in the specified database field.
*
* #param string $str
* #param string $field
* #return bool
*/
function edit_unique($value, $params) {
$CI =& get_instance();
$CI->load->database();
$CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");
list($table, $field, $current_id) = explode(".", $params);
$query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();
if ($query->row() && $query->row()->id != $current_id)
{
return FALSE;
} else {
return TRUE;
}
}
It works perfectly fine in my case
CodeIgniter 4 has already solution for that,
$validation->setRules([
'email' => 'required|valid_email|is_unique[users.email,id,{id}]',
]);
$_POST = [
'id' => 4,
'email' => 'foo#example.com',
];
then the {id} placeholder would be replaced with the number 4, giving this revised rule:
$validation->setRules([
'email' => 'required|valid_email|is_unique[users.email,id,4]',
]);
Official Documentation
we must have to add table name for is_unique
for Exp.
is_unique[users.email]

Perform validation only on create using php-activerecord

I am creating a User Model using Codeigniter and php-activerecord and the wiki says I can use 'on' => 'create' to have a validation only run when a new record is created, like this,
static $validates_presence_of = array(
array('title', 'message' => 'cannot be blank on a book!', 'on' => 'create')
);
It also states that we have access to "save", "update" and "delete"...
None of these are working for me though and I can figure out why, here is my code,
// Validations
static $validates_presence_of = array(
array('email', 'message' => 'Please enter a valid email address.'),
array('password', 'message' => 'Password must be provided.', 'on' => 'create')
);
I want to set it up like this so that when a user updates their profile, they can leave their password blank to keep their current one.
I would appreciate any help or guidance! Thanks!
The reason for this is most likely because it's not been implemented.
Relevant classes are lib/Model.php and lib/Validations.php
From a purely abstract standpoint, you would need to track the mode of operation between save and create. To do this, I created a public property (public $validation_mode) within lib/Model.php and set that property to 'create' or 'save' in private methods Model::insert() and Model::update() respectively. These values match the 'on' property you are trying to use.
Then within lib/Validations.php, I modified the following methods:
Validations::validates_presence_of()
public function validates_presence_of($attrs)
{
$configuration = array_merge(self::$DEFAULT_VALIDATION_OPTIONS, array('message' => Errors::$DEFAULT_ERROR_MESSAGES['blank'], 'on' => 'save'));
foreach ($attrs as $attr)
{
$options = array_merge($configuration, $attr);
$this->record->add_on_blank($options[0], $options['message'], $options);
}
}
Errors::add_on_blank()
public function add_on_blank($attribute, $msg, $options = array())
{
if (!$msg)
$msg = self::$DEFAULT_ERROR_MESSAGES['blank'];
if (($value = $this->model->$attribute) === '' || $value === null)
{
if(array_key_exists('on', $options))
{
if($options['on'] == $this->model->validation_mode)
{
$this->add($attribute, $msg);
}
} else {
$this->add($attribute, $msg);
}
}
}
What this does basically is passes ALL the $options specified in your model (including the 'on' property) down to the Errors::add_on_blank() method where it now has enough information to differentiate between 'on' => 'create' and the default ('on' => 'save'). Using the public $validation_mode property from the Model class ($this->model->validation_mode), we can determine what the current mode of operation is and whether or not we wish to continue adding the error message or skip it this time around.
Obviously you would want to document any changes you make and test thoroughly. According to the documentation, all validation methods supposedly support this "common option" along side allow_null, allow_blank but again, if it's not implemented, you will have to make it happen yourself by making these necessary changes.
should be call the validation method like this:
#example
$your_obj = new Instace();
if($your_obj->is_valid()) {
// if all is correct your logical code
}
else {
// your logical to show the error messages
}
//doesnt work if you write
if(!$your_obj->is_valid())
//the other way you must be use the next method
if($your_obj->is_invalid())
I'm find a answer for your question without edit library.
Add the before_validation callback and add in this callback a validation rule. It works for me.
static $before_validation_on_create = array('before_validation_on_create');
static $validates_presence_of = array(
array('login'),
);
public function before_validation_on_create() {
self::$validates_presence_of[] = array('password');
}

Yii Conditional Validation

I have a question about Yii validation. I have a dropdown whose options are Y and N. If the user select Y the user must explain why he chose Y hence a textArea box will become required.
My code for the rule looks at follows.
array('explain', 'check', 'trigger'=>'med_effects'),
Check is my function used for validation
public function check($attribute, $params)
{
if($this->$params['trigger'] == 0 && $this->$attribute == '') {
$this->addError($attribute, 'Explain the effects of the medicine');
}
}
The value for $this->$params['trigger'] does not change. I'm assuming because the saved value was 0(Y) and does not change even if the user choose N. How am I supposed to determine which option the user chose when he sumbits the form?
Thanks.
Create a property inside your model:
public $isDropDownChecked;
In your view, create a dropdown wired to new property created.
And return a array of rules inside the method rules() like this:
public function rules()
{
$rules[] = array();
if ($this->isDropDownChecked == 'Y')
$rules[] = array('explain', 'check', 'trigger'=>'med_effects');
return $rules;
}
This may also help someone on Yii1,
say you have three fields with a lookup of [Yes|NO], and you want atleast one field to be selected as Yes
Here is the solution
on your model add
public $arv_refill;
public $prep_refilled;
public $prep_initiate;
on your rules add
public function rules()
{
return array(
array('arv_refill,prep_refilled,prep_initiate','arvPrepInitiateValidation'),
);
}
arvPrepInitiateValidation being a function
public function arvPrepInitiateValidation($attribute_name,$params)
{
if($this->arv_refill != 1 && $this->prep_refilled != 1 && $this->prep_initiate != 1){
$msg = "arv_refill, prep_refilled or prep_initiate field must have one field as Yes";
$this->addError('arv_refill',$msg);
$this->addError('prep_refilled',$msg);
$this->addError('prep_initiate',$msg);
}
}

Categories