0

I want to display error message if no gender is selected using Codeigniter callback validation error...

My HTML Page

<label class="control-label labeller" for="gender">Gender</label>
     <input type="radio" id="gender" name="gender" value="Male">Male
     <input type="radio" id="gender" name="gender" value="Female">Female

My Controller

$this->form_validation->set_rules('gender', 'Gender', 'callback_gender');

public function gender($str)
{
    if ($str == '')
    {
        $this->form_validation->set_message( 'gender', 'Please Choose gender' );
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

I tried a lot of times... Please help....

1 Answer 1

2

Try using $this->input->post('gender') instead of $str

<?php

class Welcome extends CI_Controller {

  public function __construct() {
     parent::__construct();
     $this->load->library('form_validation');
  }

  public function index() {

    $this->form_validation->set_rules('gender', 'Gender', 'callback_gender');


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

     // Load view

    } else {

     // Success Stuff
    }

  }


  public function gender() {
    if ($this->input->post('gender')) {
        return TRUE;
    } else {
        $this->form_validation->set_message( 'gender', 'Please Choose gender' );
        return FALSE;
    }
  }

}

Or on callback $this->form_validation->set_rules('gender', 'Gender', 'callback_gender[gender]');

<?php

class Welcome extends CI_Controller {

  public function __construct() {
     parent::__construct();
     $this->load->library('form_validation');
  }

  public function index() {

    $this->form_validation->set_rules('gender', 'Gender', 'callback_gender[gender]');


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

     // Load view

    } else {

     // Success Stuff
    }

  }


  public function gender($str) {
    if ($str == '') {
        $this->form_validation->set_message( 'gender', 'Please Choose gender' );
        return FALSE;

    } else {
         return TRUE;
    }
  }

}
Sign up to request clarification or add additional context in comments.

3 Comments

@RobertWillis Do you have form tags
You don't have much detail in your question
Sir, its working... I followed your answer and its working... Thanks...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.