0

I am having trouble setting my custom Error Messages upon Form Validations for every Rule.

I have tried it from Bonfire documentation here

Here is some code of my Model of a Module

class Content_management_system_model extends BF_Model {

    protected $table_name = 'article';
    protected $key = 'id';

    // be updating a portion of the data.
    protected $validation_rules = array(
         array(
            'field' => 'article_alias',
            'label' => 'lang:content_management_system_article_alias',
            'rules' => 'unique[article.article_alias,article.id]',
            'errors' => array(
                'unique' => 'This is my custom error',
            ),
        ),

And here the rules are being set from Admin Controller when Inserting

private function save_content_management_system($type = 'insert', $id = 0) {

        // Validate the data
        $this->form_validation->set_rules($this->content_management_system_model->get_validation_rules());
        if ($this->form_validation->run() === false) {
            return false;
        }

But it always shows default message The value in "Article Alias" is already being used.

As per documentation in mentioned link, it should show error This is my custom error

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146

2 Answers2

1

use callback function:

$this->form_validation->set_rules('current_pswd', 'Current Password', 'trim|required|callback_alias_exist_check');


public function alias_exist_check($str)
            {
>>Put your code here
            }
0

I noticed at the end of your $validation_rules array, it is ended by a comma (,) instead of a semicolon (;). Please also remove the comma for the nested array since there are no other arrays after your first nested array.

Remove the comma(,) and replaced with a semicolon (;)

protected $validation_rules = array(
         array(
            'field' => 'article_alias',
            'label' => 'lang:content_management_system_article_alias',
            'rules' => 'unique[article.article_alias,article.id]',
            'errors' => array(
                'unique' => 'This is my custom error',
            )
        );

Also, your

if ($this->form_validation->run() **===** false) {
            return false;
        }

have a 3 'equal to' operator, which is unnecessary. Make it to only 2 'equal to' operator.

Another suggestion:

Since you are calling the content_management_system_model's function get_validation_rules why not create a function get_validation_rules() and create an array inside the function and then return the array instead of assigning the array to a protected variable?

function get_validation_rules()
{
    $validation_rules = array(
         'field'  => 'article_alias',
         'label'  => 'lang:content_management_system_article_alias',
         'rules'  => array(
              'unique'   => 'This is my custom error',
          )   
    );
    return $validation_rules;
}

Let me know if you have further questions and if the error still persists. Cheers!