Welcome Guest, Not a member yet? Register   Sign In
Extended form validation and blank fields
#1

[eluser]urrmurrmur[/eluser]
Hi,

I have extended the form validation library to add some of my own validation functions without having to resort to callbacks. It nearly works, but there's a strange behaviour which I can't figure out.

I have a new validation rule in my extended library, which should always evaluate to false (just for testing purposes). See below.

Code:
function lamosaurus( $str )
{
    $this->CI->form_validation->set_message( 'lamosaurus', 'LAMOSAURUS!' );
    return FALSE;
}

Now, when I add this function to my controller and call it as a callback, it works. If I add it to the extended library and the input field it is linked to is NOT empty, it also works. If the input field is empty, however, the rule is not executed.


Should I add my new rule to a list of validation rules which are called on empty fields, or something?
#2

[eluser]bhogg[/eluser]
Could you post the code calling this validation rule? Are you doing something like required|lamosaurus, in which case it fails on the required rule without hitting your new rule?
#3

[eluser]urrmurrmur[/eluser]
Sure thing, here's the config file:

Code:
<?php

$config = array(
    
    'user/update_user_info' => array(
        array(
            'field' => 'firstname',
            'label' => 'Voornaam',
            'rules' => 'lamosaurus'
        ),
        array(
            'field' => 'lastname',
            'label' => 'Naam',
            'rules' => 'required'
        ),
        array(
            'field' => 'email',
            'label' => 'E-mail',
            'rules' => 'required|valid_email'
        ),
        array(
            'field' => 'params[]',
            'label' => 'Parameter',
            'rules' => 'required|array_has_values'
        )
    )
    
    //Add new validation
);

It's the first field, and as you can see, I don't test required. If I do test required as well ('required|lamosaurus', as you posted) required runs whether the field is blank or not. If required doesn't pass, it doesn't test lamosaurus (as expected), but if required passes, it doesn't test it either.

What I find strangest is that everything works as expected when I do it as a callback function - but I don't like the callback principle, extending is much cleaner.


By the way, the final field params[] also uses a validation function I've written (array_has_values), which behaves in the exact same way as lamosaurus.


EDIT: In case it's of any use, here is the user/update_user_info function.

Code:
function update_user_info()
    {    
        if( $this->form_validation->run() )
        {
            //Get relevant data from POST and SESSION
            $firstname =     $this->input->post('firstname');
            $lastname =     $this->input->post('lastname');
            $email =         $this->input->post('email');
            $occupation =     $this->input->post('occupation');
            $params =         $this->input->post('params');
            $id =             $this->session->userdata('id');
            
            //Update general info in the user table
            $this->user_model->update_account_data( $id, $firstname, $lastname, $email );
            
            //Insert profile info into the profile table
            $this->profile_model->insert_profile_data( $id, $params );
            
            //Send the user to step 2 in the profile creation process
            $body_data['races'] = $this->flock_model->get_races();
            $body_data['housing_systems'] = $this->flock_model->get_housing_systems();
            $body_data['create_profile_step'] = CREATE_FARMPROFILE_VIEW_STEP2;
            
            $this->load_page(CREATE_FARMPROFILE_VIEW, $body_data);
        }
        else
        {
            $subtype = $this->session->userdata('subtype');
            $this->load_create_profile( $subtype );
        }
    }
#4

[eluser]urrmurrmur[/eluser]
Hm, I was hoping there was something obvious I was missing. Is it simply not possible to write ones own validation rule which works on a blank field?
#5

[eluser]bhogg[/eluser]
I'm not seeing the issue at first glance - are you running the latest version of codeigniter? I know there were issues with callback functions and possibly the validation lib in 1.7.0.
#6

[eluser]urrmurrmur[/eluser]
Yeah, I read that too, but I'm using 1.7.2. Which is the latest one, I think?

Maybe it's a bug then.
#7

[eluser]WanWizard[/eluser]
What exactly is 'lamosaurus'? Where is this method defined? And how?
#8

[eluser]urrmurrmur[/eluser]
I extended the form_validation library and made a new class, MY_Form_validation. below is the code.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    function My_Form_validation($rules = array())
    {
        parent::CI_Form_validation($rules);
    }
    
    function array_has_values($array)
    {
        $this->CI->form_validation->set_message( 'array_has_values', 'U moet minstens één parameter selecteren' );
        return !empty($array);
    }
    
    function lamosaurus( $str )
    {
        $this->CI->form_validation->set_message( 'lamosaurus', 'LAMOSAURUS!' );
        return FALSE;
    }
}

?>

I don't think there's anything wrong with the way I extend the class, since it does actually execute the function as expected when the field is not blank.

Ignore the function array_has_values for now, it's just a stub.
#9

[eluser]urrmurrmur[/eluser]
I just read your reply in the bug reports forum, which states that it is impossible to call validation rules unless the field is defined as required.

Fair enough, but then my question becomes: I want several different validation rules, each testing if a field is blank, but each with a different error message. I could of course set the error message in each individual controller method, but that seems like overkill. So what I was trying to do was to create an extension to the form validation class with three or four self-defined methodes to test if a field is blank.

But as I understand it now, that's impossible? Any other suggestions on a clean way to do this?




Theme © iAndrew 2016 - Forum software by © MyBB