[eluser]gedev2006[/eluser]
Can someone please advise me as to why the validation_errors() is outputting nothing at all. Here is my controller code ( unfinished and pasted from Dreamweaver )
Code: <?php
class Register extends Controller {
public $css = array ( 'register' );
/*
function: __construct()
load up required helpers, libraried and models
*/
public function Register() {
parent::Controller();
// Load form helper
$this->load->helper ( 'form' );
// Load up form helper and validation libraru
$this->load->library ( 'form_validation' );
// Load users model
$this->load->model ( 'users_model' );
}
/*
function: index
simply show the registration form
*/
public function index() {
// Set page title
$this->siteSettings->page = 'Register For An Account';
// Create page
$_pageContent = $this->load->view ( 'register/register', array(), true );
// Show layout
$this->load->view ( 'layouts/main', array ( 'page_content' => $_pageContent ) );
}
/*
function: process
process a submitted register form.
must include the hidden field "register" with value set to "1"
*/
public function process() {
// Check the register form has been sent
if ( $this->input->post ( 'register' ) && $this->input->post ( 'register' ) == '1' ) {
// Set validation rules
$_rules = array (
array ( 'username', 'Username', 'required|callback_unique_username' ),
array ( 'password', 'Password', 'required' ),
array ( 'verifypassword', 'Verify Password', 'required|matches[password]' ),
array ( 'firstname', 'First Name', 'required' ),
array ( 'lastname', 'Last Name', 'required' ),
array ( 'email', 'Email', 'required|valid_email' ),
array ( 'dob', 'Date Of Birth', 'required|callback_valid_dob' )
);
$this->form_validation->set_rules ( $_rules );
if ( $this->form_validation->run() ) {
die ("TESTING FORM VALIDATION SUCCESS" );
// Perform registration
$this->users_model->registerUser (
$this->input->post ( 'username' ),
$this->input->post ( 'password' ),
$this->input->post ( 'firstname' ),
$this->input->post ( 'lastname' ),
$this->input->post ( 'email' ),
$this->input->post ( 'dob' )
);
// Show register thanks page
$this->load->view ( 'layout/main', array ( 'page_content' => $this->load->view ( 'register/thanks', array(), true ) ) );
} else {
// invalid fields
$this->index();
}
} else {
$this->index();
}
}
/*
validation callback functions
*/
/*
function unique_username
check the username against the database
*/
public function unique_username ( $username ) {
if ( $_user = $this->users_model->getByUsername ( $username ) ) {
// user already exists
$this->form_validation->set_message ( 'unique_username', 'Username is already taken' );
return false;
} else {
return true;
}
}
/*
function: valid_dob
perform REGEX on supplied DOB to make sure the text
entered is returned in the format of mm/dd/yyyy
*/
public function valid_dob ( $dob ) {
$_pattern = '%[0-1][1-2]/[0-3][1-1]/[1-2][9-0][0-9][0-9]%'; /// may need a little revision!
if ( !preg_match ( $_pattern, $dob ) ) {
$this->form_validation->set_message ( 'valid_dob', 'Date of birth invalid' );
return false;
} else {
return true;
}
}
}
?>
[eluser]gedev2006[/eluser]
View file code
Code: <div id="innerContent">
<div class="contentContainer">
<div class="contentHeader"><h1><?= $this->siteSettings->pageTitle(); ?></h1></div>
<div class="content">
<div class="errors"><?= validation_errors(); ?></div>
<?= $this->blocks_model->getByIdentifier ( 'registerMessage' )->getContent(); ?>
<form action="<?= $this->siteSettings->getLink ( 'register/process' ); ?>" method="post">
<fieldset>
<table>
<tr>
<th>Username: <span class="required">*</span></th>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<th>Password: <span class="required">*</span></th>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<th>Re-type password: <span class="required">*</span></th>
<td><input type="password" name="verifypassword" /></td>
</tr>
<tr>
<th>First name: <span class="required">*</span></th>
<td><input type="text" name="firstname" /></td>
</tr>
<tr>
<th>Last name: <span class="required">*</span></th>
<td><input type="text" name="lastname" /></td>
</tr>
<tr>
<th>Email: <span class="required">*</span></th>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<th>Date of birth<br />(mm/dd/yyyy): <span class="required">*</span></th>
<td><input type="text" name="dob" /></td>
</tr>
<tr>
<th>Website: </th>
<td><input type="text" name="website" /></td>
</tr>
<tr>
<th>MSN: </th>
<td><input type="text" name="msn" /></td>
</tr>
<tr>
<th>AIM: </th>
<td><input type="text" name="aim" /></td>
</tr>
<tr>
<th>YIM: </th>
<td><input type="text" name="yim" /></td>
</tr>
<tr>
<th>Avatar: </th>
<td><input type="file" name="avatar" /></td>
</tr>
<tr><td> </td></tr>
<tr>
<th class="required">* required fields</th>
<td colspan="2" align="left"><input type="submit" value="Complete registration" /></td>
</tr>
</table>
<input type="hidden" name="register" value="1" />
</fieldset>
</form>
</div>
</div>
</div>
[eluser]gedev2006[/eluser]
my fault! didnt read the documentation quite right. revised code:
Code: // Set validation rules
$_rules = array (
array ( 'field' => 'username', 'label' => 'Username', 'rules' => 'required|callback_unique_username' ),
array ( 'field' => 'password', 'label' => 'Password', 'rules' => 'required' ),
array ( 'field' => 'verifypassword', 'label' => 'Verify Password', 'rules' => 'required|matches[password]' ),
array ( 'field' => 'firstname', 'label' => 'First Name', 'rules' => 'required' ),
array ( 'field' => 'lastname', 'label' => 'Last Name', 'rules' => 'required' ),
array ( 'field' => 'email', 'label' => 'Email', 'rules' => 'required|valid_email' ),
array ( 'field' => 'dob', 'label' => 'Date Of Birth', 'rules' => 'required|callback_valid_dob' )
);
|