Welcome Guest, Not a member yet? Register   Sign In
set_message() from form_validation
#1

Hi guys,
I can't understand how to use set_message() form from_validation libraries.
I've read doc, consult stackoverflow and I can't figure it out on how to use it...
What I'm trying to achieve (for now) is checking if a checkbox is cheked or not...



PHP Code:
/*just output the page where user can compile a form*/
 public function nuovocosplay()
    {
      $this->load->view('auth/template/auth_header'$this->pagedata);
      $this->load->view('auth/template/auth_nav'$this->cliente);
      $this->load->view('auth/clienti/auth_sidebar');
      $this->load->view('auth/clienti/nuovocosplay');
      $this->load->view('auth/template/auth_footer');
    


This is the part in nuovocosplay.php view that call the checkbox and the error

Code:
                      <?php echo form_error('accetta_misure', '<div class="error">', '</div>'); ?>

                      <?php echo form_checkbox('accetta_misure','accetta', FALSE); ?>



PHP Code:
/*this is the validation that call the misure_db model function to write data to DB*/
    public function misure_validation() {
      $this->load->library('form_validation');
      $this->form_validation->set_rules('accetta_misure','accetta','required|md5|trim');
      // la funzione run ritorna un true solo se le regole sopra sono verificate
      if ($this->form_validation->run() == true) {
        $this->misure_db();
      } else {
        $this->form_validation->set_message('accetta_misure','accetta''devi accettare i termini e le condizioni della commissione.');
        $this->nuovocosplay(); //ritorna alla schermata delle misure
      }
    

I really don't understand what's wrong, I've read that I have to use callbacks? but how...

Thank you in advance and sorry for my noobness
Reply
#2

A callback is just another function / method in you code.

function checkbox_callback(){}

You then need to add the callback function / method name to the set_rule
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(02-25-2016, 06:41 AM)InsiteFX Wrote: A callback is just another function / method in you code.

function checkbox_callback(){}

You then need to add the callback function / method name to the set_rule

yes...I've already used a callback for my login system...the problem is that I don't know what code to write in the callback if I've already coded the check in the misure_validation()
Reply
#4

PHP Code:
/*Invio dati al modello per l'inserimento dei dati nel db*/
    public function misure_validation() {
      $this->load->library('form_validation');
      $this->form_validation->set_rules('accetta_misure','accetta','required|md5|trim|callback_convalida_tos');
      // la funzione run ritorna un true solo se le regole sopra sono verificate
      if ($this->form_validation->run() == true) {
        $this->misure_db();
      } else {
        $this->nuovocosplay(); //ritorna alla schermata delle misure
      }
    }

    public function convalida_tos()
    {
      if (isset($_POST['accetta_misure'])) {
         return true;
      } else {
        $this->form_validation->set_message('convalida_tos''devi accettare i termini e le condizioni della commissione.');
        return false;
      }
    

I'm trying anything and nothing seems to work
Reply
#5

(This post was last modified: 02-25-2016, 10:06 AM by davicotico. Edit Reason: Fix )

(02-25-2016, 09:47 AM)Psygnosis Wrote:
PHP Code:
/*Invio dati al modello per l'inserimento dei dati nel db*/
    public function misure_validation() {
      $this->load->library('form_validation');
      $this->form_validation->set_rules('accetta_misure','accetta','required|md5|trim|callback_convalida_tos');
      // la funzione run ritorna un true solo se le regole sopra sono verificate
      if ($this->form_validation->run() == true) {
        $this->misure_db();
      } else {
        $this->nuovocosplay(); //ritorna alla schermata delle misure
      }
    }

    public function convalida_tos()
    {
      if (isset($_POST['accetta_misure'])) {
         return true;
      } else {
        $this->form_validation->set_message('convalida_tos''devi accettare i termini e le condizioni della commissione.');
        return false;
      }
    

I'm trying anything and nothing seems to work

Here is a little example with checkbox:

Controller: (Welcome.php)
PHP Code:
public function index()
 {
 
$this->load->helper(array('form''url'));
 
$this->load->library('form_validation'); 
 
$this->form_validation->set_rules('user''User''required');
 
$this->form_validation->set_rules('agree''Acceta''required');

 if (
$this->form_validation->run() == TRUE)
 {
 
//Here save your data
 
$data['post'] = $this->input->post();
 
var_dump($data);
 }
 else
 {
 
$this->load->view('formulario');
 }
 } 
View (formulario.php)
PHP Code:
<body>

<
div id="container">
 <
h1>Form Validation Testing</h1>
 <
div id="body">
 <?
php echo validation_errors(); ?>
 <?php echo form_open('welcome'); ?>
 <h5>Username</h5>
 <input type="text" name="user" value="<?php echo set_value('user'); ?>" size="50" />
 <h5>Accetare</h5>
 <!--<input type="checkbox" name="agree" value="1" /> Funziona anche in questo modo -->
 <?php echo form_checkbox('agree','Acceta'FALSE); ?>
 <div><input type="submit" value="Submit" /></div>
 </form>
 </div>
</div>

</body> 

Reply
#6

but you don't set a custom error message in your example
Reply
#7

(02-25-2016, 11:34 AM)Psygnosis Wrote: but you don't set a custom error message in your example


PHP Code:
// If you need to set a custom error message for a particular field on some particular rule, use the set_rules() method:

$this->form_validation->set_rules('field_name''Field Label''rule1|rule2|rule3',
 
       array('rule2' => 'Error Message on rule2 for this field_name')
);

// Where rule corresponds to the name of a particular rule, and Error Message is the text you would like displayed. 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB