Welcome Guest, Not a member yet? Register   Sign In
Extending Form_validation
#1

[eluser]Unknown[/eluser]
Am having trouble extending the Form_Validation class

Here is a sample of my code and config

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

class MY_Form_validation extends CI_Form_validation{
protected $CI;
public function __construct($config = array()){
parent::__construct($config);
$this->CI =& get_instance();
}

function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}

public function validateName($name){

$this->CI->form_validation->set_message('validateName','The %s field must be in text format');

return preg_match('/^(?:[A-Za-z\.\'\-]+(?:\s+|$))$/',$name);
}

public function validateNumber($number){
if(ctype_digit($number)){
return TRUE;
}else{
$this->CI->form_validation->set_message('validateNumber','The %s field must be in numeric format');
return FALSE;
}
}

public function validateAddress($address){
$status = preg_match('/^(?:[0-9A-Za-z\.\'\-]+(?:\s+|$))$/',$address);
if($status){
return TRUE;
}else{
$this->CI->form_validation->set_message('validateAddress','The %s field must be in alpha-numeric format');
return FALSE;
}
}

public function validateEmail($email){
if(ctype_graph($email)){
return TRUE;
}else{
$this->form_validation->set_message('validateEmail','The %s field must be in alpha-numeric format');
return FALSE;
}
}
}

<?php

$config = array(
'welcome/verifysignup' => array(
array(
'field' => 'fname',
'label' => 'First name',
'rules' => 'validateName'
),
array(
'field' => 'lname',
'label' => 'Last name',
'rules' => 'required'
),
array(
'field' => 'address',
'label' => 'Address',
'rules' => 'required'
),
array(
'field' => 'city',
'label' => 'City',
'rules' => 'required'
),
array(
'field' => 'zip',
'label' => 'Zip(Postal code)',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
),
array(
'field' => 'remail',
'label' => 'Confirm email',
'rules' => 'required'
),
array(
'field' => 'remail',
'label' => 'Confirm Email',
'rules' => 'required'
),
array(
'field' => 'pphone',
'label' => 'Primary phone number',
'rules' => 'required'
),
array(
'field' => 'mphone',
'label' => 'Mobile phone number',
'rules' => 'required'
),
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'pwd',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'cpwd',
'label' => 'Confirm Password',
'rules' => 'required'
)

)
);

Submitting the form without the first name does not generate an error meaning the custom validateName method is not called.
What could be the problem..
Using CI 2.1.3
Please help!!...
#2

[eluser]qICEp[/eluser]
Not sure but i think you close more brackets than you open (that lead to errors and failure)
Check closing bracket's at the end of the file

I also assume that you attached model/controler file in same code tag




Theme © iAndrew 2016 - Forum software by © MyBB