ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[CI_NOVICE_HELP] Setting Custom Error Message

November 27, 2011 10:47pm

Subscribe [2]
  • #1 / Nov 27, 2011 10:47pm

    Andy Armstrong

    17 posts

    I really want to create a custom message with all set rules with “required” rule. This is my sample code, creating a call back function is what i want for my script however i am having problems displaying the custom error on the page, i am just a novice for this new framework, I hope you CI geek guys could help me it would be great and i am honored to belong with this friendly CI community. Thank you very much..


    public function addnew(){


    $this->form_validation->set_rules('fullname','Full Name','trim|required');
    $this->form_validation->set_rules('phonenumber','Phone Number','trim|required');
    $this->form_validation->set_rules('age','Age','trim|required|numeric');
    $this->form_validation->set_rules('address','Address','trim|required');
    $this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[phonebook.email]');

    if($this->form_validation->run() == FALSE){

    //Supposed to be my custom error message for all set_rule that has a "required" rule
    $this->form_validation->set_message('required','important!');
    $this->load->view('addnewcontacts');
    }
      else
      {
    $fullname = $this->input->post('fullname');
    $phonenumber = $this->input->post('phonenumber');
    $age = $this->input->post('age');
    $address = $this->input->post('address');
    $email = $this->input->post('email');

    $this->phonebook->insert_phonebook($fullname,$phonenumber,$age,$address,$email);

    }
    }

    //I want to set a call back function to be called by email checking if the user has a unique email
    private function error_email(){

    if($this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[phonebook.email]')):
    $this->set_message('error_email','This email is already taken');
    endif;
    }

  • #2 / Nov 28, 2011 1:14am

    CroNiX

    4713 posts

    Validation rules must send back TRUE or FALSE to say whether it passed or failed validation.  If it failed, it will display the message associated with whatever rule it failed.

    I’m not sure what you are trying to do with this line:

    if($this->form_validation->set_rules(‘email’,‘Email’,‘trim|required|valid_email|is_unique[phonebook.email]’)):

    See the userguide for more.

  • #3 / Nov 28, 2011 2:04am

    Andy Armstrong

    17 posts

    Thank you for your reply Sir,

    that line of code, i am trying to check whether the posted value is a valid and a unique email if its not it will do a custom error message. however i am really confused on making some custom error message on a page.I want to try a different error message rather than to adapt the default error message on form validation. Could you give me a hint or a solution to my problem? I appreciate your help..

    Thanks in advance Sir 😊

  • #4 / Nov 28, 2011 1:16pm

    CroNiX

    4713 posts

    If you want to permanently change the messages, you can edit the /system/language/english/form_validation_lang.php file.

    If you want to dynamically change the messages for existing rules, you can just change it in a controller like:

    $this->form_validation->set_message($rule_name, $message);

    so, for “required” you could do:

    $this->form_validation->set_message('required', 'The %s field is a mandatory field.  Please enter a value.');

     

  • #5 / Nov 28, 2011 9:04pm

    Andy Armstrong

    17 posts

    Wow! it works! :lol: Thank you for helping a CI novice like me.. two thumbs for you sir..

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases