I made a simple form, a newsletter subscription, which has two inputs: email
and city
. I use is_unique
in form validation, but this error shows as text. I need to modify this text show in an alert box or warning.Need something to fix this error in design, suggestions please.
user.php controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class User extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('user_agent');
$this->load->library('form_validation');
}
public function create_user() {
// field name, error message, validation rules
$lang = $this->input->post("lang");
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if ($this->form_validation->run() == FALSE) {
if ($lang == "en") {
if ($this->agent->is_mobile()) {
$this->load->view('m_english_signup');
}
else
{
$this->load->view('d_english_signup');
}
} //if ($this->agent->is_mobile())
else
{
if ($this->agent->is_mobile()) {
$this->load->view('m_arabic_signup');
}
else
{
$this->load->view('d_arabic_signup');
}
}
}
else
{
$this->load->model('Users_model');
if ($query = $this->Users_model->create_member()) {
if ($lang == "en") {
if ($this->agent->is_mobile()) {
$this->load->view('m_english_thanks');
}
else
{
$this->load->view('d_english_thanks');
}
}
else
{
if ($this->agent->is_mobile()) {
$this->load->view('m_arabic_thanks');
}
else
{
$this->load->view('d_arabic_thanks');
}
}
}
}
}
}
You need to set your own error message ?
try CodeIgnitor Userguide http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html#settingerrors
I think Nilesh has the best solution that you need. By using javascript you can, if the email entered already exists, you can generate an alert. Alternatively you can use Bootstrap to style your div
(assuming you have one above your email input):
<div class="alert" style="display: none">
<a class="close" data-hide="alert" >×</a>
<Strong><?php echo form_error('email'); ?></strong>
</div>
<input name="email" value="<?php echo set_value('email'); ?>" />
Then write some javascript:
$(document).ready(function(){
$("[data-hide]").on("submit", function(){
$("." + $(this).attr("data-hide")).hide();
});
});
I hope this helps.