So I have simple registration form and for some weird reason, some of the validation works and some doesn't. I defined a separate language folder for the language in conern i.e. arabic. it works for username, password and password confirmation, but not rest of the fields. What is going wrong?
public function registration()
{
$this->form_validation->set_rules('fullname', 'الاسم الكامل', 'isset | required | alpha');
$this->form_validation->set_rules('username', 'اسم المستخدم', 'required');
$this->form_validation->set_rules('password', 'كلمة السر', 'required');
$this->form_validation->set_rules('passconf', 'إعادة كلمة السر', 'required');
$this->form_validation->set_rules('email', 'الايميل', 'required |email');
$this->form_validation->set_rules('city', 'المدينة', 'required');
$this->form_validation->set_rules('userfile', 'صورة الملف الشخصي', 'upload_no_file_selected');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else{
$this->user_model->add_user();
$this->thank();
}
}
form:
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body dir="rtl">
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('membership/registration'); ?>
<h5>الإسم الكامل</h5>
<input type="text" name="fullname" value="" size="80" />
<h5>اسم المستخدم</h5>
<input type="text" name="username" value="" size="50" />
<h5>كلمة السر</h5>
<input type="text" name="password" value="" size="50" />
<h5>إعادة كلمة السر</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>الايميل</h5>
<input type="text" name="email" value="" size="50" />
<h5>المدينة</h5>
<select name="city">
<option value="riyadh">الرياض</option>
<option value="jeddah">جدة</option>
<option value="dhahran">الظهران</option>
<option value="mecca">مكة</option>
</select>
<h5>صورة الملف الشخصي</h5>
<input type="file" name="userfile" size="20" />
<br><br>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
Appreciate the help,
Remove the spaces between the validation rules like this:
$this->form_validation->set_rules('fullname', 'الاسم الكامل', 'isset|required|alpha');
I found out the answer and oh is it silly! Just delete the spaces between the validation rules and it all works! so e.g.
alpha|isset|required
Related
i need to stop insert data when refresh page iam new in codeigniter and can't do redirect easly,please support
view:pages/home.php view which has a form
<html>
<head></head>
</body>
<?php echo validation_errors(); ?>
<?php echo form_open('speed/insert_to_db'); ?>
Branch: <input type="text" name="branch" /><br/>
Business Unit: <input type="text" name="buinessUnit" /><br/>
Device Type: <input type="text" name="deviceType" /><br/>
Brand: <input type="text" name="brand" /><br/>
Device Model: <input type="text" name="deviceModel" /><br/>
SN: <input type="text" name="SN" /><br/>
status: <input type="text" name="status" /><br/>
department: <input type="text" name="department" /><br/>
username: <input type="text" name="username" /><br/>
notes: <input type="textarea" name="notes" /><br/>
computername: <input type="text" name="computerName" /><br/>
Save:<input type="submit" name="save" />
</form>
</body>
</html>
model:add_model model which has insert_to_db function
public function insert_into_db(){
$post=$this->input->post();
$data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
$this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql
}
}
controller:
<?php
class Speed extends CI_Controller {
function insert_to_db()
{
$this->load->model('add_model');
if($this->input->post('save')){
$this->add_model->insert_into_db();
$this->load->view('pages/home');//loading success view
}
}
}
I think you should try this in your controler:
function insert_to_db()
{
$this->load->model('add_model');
if( isset( $this->input->post('save') ) )
{
$this->add_model->insert_into_db();
//loading success view
$this->load->view('pages/home');
}
}
I am using codeigniter. Below is the code for my registration page.
Controller
function register()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|is_unique[users.username]');
$this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email|is_unique[users.user_email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('ans_1', 'Security answer 1', 'required');
$this->form_validation->set_rules('ans_2', 'Security answer 2', 'required');
if($this->form_validation->run() == FALSE) {
//not validated - reload the view and display errors
$this->load->database();
$this->load->model('user');
$data['ques'] = $this->user->get_que();
$this->load->view('register',$data);
}
else {
$this->load->database();
$this->load->model('user');
//create user
$this->user->create_user();
$this->thank();
}
}
View
<!DOCTYPE html>
<html>
<head>
<title> Login</title>
<link rel="stylesheet" href="http://localhost/cinema/assets/css/form.css">
</head>
<body>
<form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" >
<h3>User Registration form:</h3>
<p><label for="first_name">First Name</label>
<input type="text" name="first_name" /></p>
<p><label for="last_name">Last Name</label>
<input type="text" name="last_name" /></p>
<p><label for="username">Username</label>
<input type="text" name="username" /></p>
<p><label for="user_email">Email</label>
<input type="text" name="user_email" /></p>
<p><label for="password">Password</label>
<input type="password" name="password" /></p>
<br><font size='4'>Select security question 1:</font>
<select name="que_1">
<?php
foreach($ques as $que)
{
echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>';
}
?>
</select><br><br>
<p><label for="ans_1">Answer:</label>
<input type="text" name="ans_1" /></p>
<br><font size='4'>Select security question 2:</font>
<select name="que_2">
<?php
foreach($ques as $que)
{
echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>';
}
?>
</select><br><br>
<p><label for="ans_2">Answer:</label>
<input type="text" name="ans_2" /></p>
<input type="submit" name="btnSubmit" class="styled-button-8" style="float:right; margin-right:300px; margin-top:-10px;" value="Sign Up!"
/>
<font color="red" size="4"><b>
<?php echo validation_errors(); ?></b></font>
</form>
</body>
</html>
<br>
My problem is that in case the user fills invalid data and clicks the signup button, the page is refreshed and the validation errors are displayed but the data entered by the user doesn't remain there.He has to again fill the whole form. I want the user data to be retained and displayed in this case. Thanks in advance .. :)
For this you need set_value()
For input fileds
<input type="text" name="last_name" value='<?php echo set_value('last_name')?>'/>
This will get the value from $_POST and print after failed validation. Also takes 2nd parameter as default value.
<?php echo set_value('last_name','John')?>
Read the Form Helper Class.
You can try like this. You can maintain session to display user fields data while validation check. I added a Sample with session in your form.
Your View :
<?php session_start(); ?>
<form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" >
<h3>User Registration form:</h3>
<p><label for="first_name">First Name</label>
<input type="text" name="first_name" value="<?php echo $_SESSION['first_name']; ?>" /></p>
<font color="red" size="4"><b>
<?php echo validation_errors(); ?></b></font>
</form>
Your controller:
function register() {
$this->load->library('form_validation');
$firstName = $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->session->set_userdata('first_name', $firstName);
if ($this->form_validation->run() == FALSE) {
//redirect to register page
} else {
//register user details
$this->session->unset_userdata('first_name');
}
Better solution is : use post data
<input type="text" name="last_name" value='<?php echo $this->input->post('last_name')?>'/>
When you use set_value(), you must send that field to validation even it is not required.
But for Post, you don't want to do any additional work.
In your controller put a data array and pass the user entered values to that.Then if there are form validation errors pass those data to the view.
In the view use CI set_value() method to show the previous entered values.
I want to fill this form, but i dont know how to do it since it only have a classname.
All the examples i saw have an id or a name, to fill the form and submit it, please help.
<form class="header_form" method="post" action="">
<div class="lgn-items">
<div class="login_item">
<label>Email</label>
<input type="text" name="email" tabindex="1" class="inputtext" id="email" />
</div>
<div class="login_item" >
<label>Password</label>
<input type="password" name="password" tabindex="2" class="inputtext" id="pass" />
</div>
<div class="lgn-add">
Registration <span>|</span>
Forgot your password ?
<div class="rembo">
<input type="checkbox" name="remember" value="1" /> Remember me
</div>
</div>
</div>
<div class="login_item lgn-btn" >
<input type="submit" name="login_button" value="Login" tabindex="3" class="login" />
</div>
</form>
You can access to your form using any selector. In your case you to it like this.
casper.then(function () {
casper.fill('form.header_form', {
/** Your parameters here **/
}, true);
});
I am trying to validate email and password using the CodeIgniter's form_validation library. But when I typed wrong email or password the error message of validation_rules is not displayed.
The controller:
public function anuncios()
{
$usr=$this->input->post('Usuario');
$this->input->post('Contrasenya');
$this->form_validation->set_rules('drcorreo','Nombre de usuario',
'trim|required|min_length[5]|xss_clean');
$this->form_validation->set_rules('contrasena','Contraseña',
'trim|required|min_length[8]|md5|xss_clean');
if($this->form_validation->run())
{
$this->load->model('modelo_usuarios');
if($this->modelo_usuarios->puede_entrar())
{
echo "Credenciales correctos";
$this->load->model("modelo_bd");
$data['vanc']=$this->modelo_bd->datos();
$this->load->view('vancios',$data);
return true;
}
else { //this one is never displayed
echo "Credenciales incorrectos";
echo "Usuario o contrasenya incorrectos<br /><br />";
$this->load->view('indice');
return false;
}
}
else { //this one is displayed but not the rules specified in
//form_validation_lang.php file
echo "Las reglas no son validas";
$this->load->view('indice');
}
}
View:
<div id="acformulario">
<form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
<label for="correo" id="dcorreo">Dirección de correo</label>
<input type="text" name="drcorreo" id="dcc"/><br /><br />
<label for="contrasenya" id="cont">Contraseña</label>
<input type="password" name="contrasena" id="cmcont"/><br /><br />
<!--<label for="enviar"></label>-->
<input type="submit" name="envia" id="bentrar" value="Entrar" />
</form>
</div>
Why error messages are not displayed?
Thanks.
i assume you call "form" helper in your autoload.php file,
use your view like this;
<div class="errors"><?php echo validation_errors(); ?></div>
<div id="acformulario">
<form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
<label for="correo" id="dcorreo">Dirección de correo</label>
<input type="text" name="drcorreo" id="dcc"/><br /><br />
<label for="contrasenya" id="cont">Contraseña</label>
<input type="password" name="contrasena" id="cmcont"/><br /><br />
<!--<label for="enviar"></label>-->
<input type="submit" name="envia" id="bentrar" value="Entrar" />
</form>
</div>
the point is "validation_errors()" function in here.
The 'can not enter' message is passed back after the rules have been passed. It is inside of the if statement when the form validation is true.
If you want this message to be displayed either load the message into the view, or set it as flash data.
Why not make the puede_entrar() model call into a custom callback form validation rule, then it can be added to the rules that are set.
Look here for information on custom callbacks.
This is how I would code your controller;
<?php
class Foo extends CI_Controller {
public function anuncios() {
// xxs_clean set globally in config
$this->form_validation->set_rules('drcorreo','Nombre de usuario', 'trim|required|min_length[5]|callback_puede_entrar');
// should use sha1 at least for hashing, see http://www.freerainbowtables.com/tables/
$this->form_validation->set_rules('contrasena','Contraseña', 'trim|required|min_length[8]|md5');
if($this->form_validation->run()) {
$this->load->model('modelo_bd');
$data['vanc']=$this->modelo_bd->datos();
$this->load->view('vancios',$data);
} else {
// redisplay form with validation errors
$this->load->view('indice');
}
}
public function puede_entrar($val) {
$this->load->model('modelo_usuarios');
if($this->modelo_usuarios->puede_entrar()) {
return TRUE;
} else {
$this->form_validation->set_message('puede_entrar', 'Las reglas no son validas.');
return FALSE;
}
}
}
The view (assuming you have autoloaded or have loaded $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); elsewhere);
<div id="acformulario">
<form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
<?php echo form_error('drcorreo'); ?>
<label for="correo" id="dcorreo">Dirección de correo</label>
<input type="text" name="drcorreo" id="dcc"/><br /><br />
<label for="contrasenya" id="cont">Contraseña</label>
<?php echo form_error('contrasena'); ?>
<input type="password" name="contrasena" id="cmcont"/><br /><br />
<!--<label for="enviar"></label>-->
<input type="submit" name="envia" id="bentrar" value="Entrar" />
</form>
</div>
I have the problem of doing the validation of fields myself. Now there are 5-6 fields in the form. So I am checking each one in my controller, and if there is wrong i wish to load the view again and pass the error array to it.
I achieved the above functionality with this:
<html>
<head>
<title>My Form</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<?php
echo $fullname;
?>
<?
echo form_open('/membership/register');
?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<h5>Mobile</h5>
<input type="text" name="mobile" value="" size="15" />
<h5>Home</h5>
<input type="text" name="home" value="" size="15" />
<h5>Full Name</h5>
<input type="text" name="fullname" value="" size="100" />
<br><br>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
and in controller the code is:
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
$this->load->view('register', $errors);
}
Now the real problem I have is if the many fields are wrong. I want to have $errors array passed to view and accessed there for all the values it contains. so I don't have to specify $fullname or $mobile to get the value. How can this be done? as to show the user everything missing in one go
First of all I advise using codeigniter's built in form validation class
Here is how I usually process my validation in the controller:
if ($this->input->post())
{
// process the POST data and store accordingly
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean');
// the rest of your form fields validation can be set here
if ($this->form_validation->run() == FALSE)
{
// validation hasnt run or has errors, here just call your view
$this->load->view('same_view_file_with_the_form', $data);
}
else
{
// process the POST data upon correct validation
}
}
In my view file I call each error like this:
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<span class="error-red"><?php echo form_error("username"); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<span class="error-red"><?php echo form_error("password"); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<span class="error-red"><?php echo form_error("cpassword"); ?></span>
Do all your checks in the controller prior to binding errors into the view.
e.g.
$errors = array();
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
}
if ( do_something_to_validate(mobile) )
{
$errors['mobile'] = 'invalid mobile';
}
// after checking everything do this
$this->load->view('register', $errors);