I have a site develop in codeigniter.
I want to validate a form and display some message.
With my code I don't see any error if my input "name_it" is blank. Why?
This is my controller:
public function nation_create()
{
if($_POST)
{
//salvo i dati
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p class="error">Errore: ', '</p>');
$this->form_validation->set_rules('name_it', 'name_it', 'required');
if ($this->form_validation->run() !== FALSE){
$this->Nation_model->createNation();
redirect('backend/nation/nation_list/');
}
else{
$data['errors'] = validation_errors();
$this->load->view('backend/include/header_view_logged');
$this->load->view('backend/nation_create_view',$data);
$this->load->view('backend/include/footer_view');
}
}
else{
$this->load->view('backend/include/header_view_logged');
$this->load->view('backend/nation_create_view');
$this->load->view('backend/include/footer_view');
}
}
And this is my view (I have cut more code to read well)
<form action='' method='POST'>
<?php echo validation_errors();
if (isset($errors))
echo $errors;
?>
<?php echo form_error('name_it'); ?>
<input type="text" name="name_it" />
<span id="errorsDiv_name_it"></span>
</form>
Try this:
change:
if ($_POST)
for:
if ($this-input->post())
Info: http://ellislab.com/codeigniter/user-guide/libraries/input.html
You can use the form_validation()->run() to complete this kind of task. It checks if a POST have been sent and if validation_rules returns true.
if($this->form_validation()->run() == false)
{
//load the view
// other stuff you do while POST is not yet sent or if Input from the user returns false
}else{
// post has been sent and validiation is TRUE
// Input is good. . do something
}
The problem was in the class error with attribute display:none
Related
Don't get this.
I have to validate basic form. Im using CI form_validation library, form validation rules are set just to "required"...if form validation fails it just need to echo validation error in login form. Im using by default <?php echo validation_errors(); ?> Helper is autoloaded. Form functionality is OK it works how it should, only problem is that framework dont display error messagges if dont fill required fields?
login_form.php - VIEW
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
login.php - CONTROLLER
<?php
Class Login extends CI_Controller {
function index () {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
function validate() {
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
else {
redirect("user/profile");
}
}
}
?>
Why don't you use code in controller like this hope will help you just set form action to login.php
login.php - CONTROLLER
function index () {
$data ['content'] = "login_form";
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
$this->load->view("template", $data);
}
else {
redirect("user/profile");
}
}
login_form.php - VIEW
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
Don't redirect, else error will not be available. Instead of that keep
if( $this->form_validation->run() == FALSE) {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
Check the following link for more details:
Form Validation : CodeIgniter User Guide
I have been trying to learn CI and implement a blog content management system following a lightweight tutorial I found online today, but I'm currently experiencing some problems in the view.
Adding new entries "works", but I get the following errors on top of the add_new page:
http://pastebin.com/4UhMTqLj
CONTROLLER
function new_entry()
{
$this->load->helper('form');
$this->load->library(array('form_validation','session'));
//set validation rules
$this->form_validation->set_rules('entry_name', 'Title', 'required|xss_clean|max_length[200]');
$this->form_validation->set_rules('entry_body', 'Body', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('header', $data);
$this->load->view('admin/new_entry', $data);
$this->load->view('footer', $data);
}
else
{
//if valid
$name = $this->input->post('entry_name');
$body = $this->input->post('entry_body');
$this->articles_model->new_entry($name,$body);
$this->session->set_flashdata('message', '1 new entry added!');
redirect('articles/new_entry');
}
}
VIEW
<h2>Add new entry</h2>
<?php echo validation_errors(); ?>
<?php if($this->session->flashdata('message')){echo $this->session->flashdata('message');}?>
<?php echo form_open('articles/new_entry');?>
<p>Title:<br />
<input type="text" name="entry_name" />
</p>
<p>Body:<br />
<textarea name="entry_body" rows="5" cols="50" style="resize:none;"></textarea>
</p>
<input type="submit" value="Submit" />
<?php echo form_close();?>
MODEL
function new_entry($name,$body)
{
$data = array(
'entry_name' => $name,
'entry_body' => $body
);
$this->db->insert('entry',$data);
}
What can be the cause?
Edit: I'm using Codeigniter 3.
You must initiate the $data variable before passing in to the views.
eg.
$data = array();
put it on the new entry class
I completely agree with the answer from Cha Hernandez, however, another possibility would be:
To remove the $data variable altogether:
$this->load->view('header');
$this->load->view('admin/new_entry');
$this->load->view('footer');
This is mainly because you're not actually passing any information to the view, so there is no point in having it in the instance.
Hope this helps!
Change your CONTROLLER TO file below code where i removed variable $data since it is not assigned to any values and passing undefined variable $data is not necessary.
function new_entry()
{
$this->load->helper('form');
$this->load->library(array('form_validation','session'));
//set validation rules
$this->form_validation->set_rules('entry_name', 'Title', 'required|xss_clean|max_length[200]');
$this->form_validation->set_rules('entry_body', 'Body', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('header');
$this->load->view('admin/new_entry');
$this->load->view('footer');
}
else
{
//if valid
$name = $this->input->post('entry_name');
$body = $this->input->post('entry_body');
$this->articles_model->new_entry($name,$body);
$this->session->set_flashdata('message', '1 new entry added!');
redirect('articles/new_entry');
}
}
For some reason I can't get forms to post in Codeigniter 2.1.4, I honestly have no clue where I am going wrong. Can anyone see any obvious problems? No post data is returned just an empty form the vardump returns (boolean false)
public function testpost()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
var_dump($this->input->post());
$this->form_validation->set_rules('testt', 'Test', 'required');
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
echo form_open('test/testpost');
echo form_input(array('type'=>'text', 'id'=>'testt'));
echo form_submit(array('value' => 'submit'));
echo form_close();
}
else
{
echo "true";
}
}
You did not create name of text field.
echo form_input(array('type'=>'text', 'id'=>'testt', 'name'=>'testt'));
another question again regarding codeigniter, here a few details:
view page:
<?php if (isset($error)){echo $error; } ?>
<form action="<?php echo site_url('mem_posting/post');>" method="post">
<input type="text" name="fname">
some fields goes here...
</form>
controller page(mem_posting):
public function post_form()
{
$this->load->view('header');
$this->load->view(form_page);
}
public function post()
{
$post_data=array(
'mem_id'=>$this->input->post('mem_id'),
//other inputs...
)
$this->load->model('member_model');
if ($this->member_model->check_member($post_data)===true)
{
//row exist
// **i would like to load the same page but
// **with error message "like already exist".
}else{
$this->member_model->inset_member($post_data);
}
}
model page:
public function insert_member($post_data=array())
{
extract($post_data);
$this->db->where('member_id', $member_id);
$this->db->insert('membership', $post_data);
}
public function check_member($post_data=array())
{
extract($post_data);
$this->db->select('mem_id');
$this->db->where('mem_id', $mem_id);
$query = $this->db->get('membership');
if ($query->num_rows() > 0){
return true;
}else{
return false;
}
}
as you can see the view page contains the form, now what i would like to achieve is to echo an error like 'already exist' so i dont have to code another $this->load->view('post_form') inside the if statement.
thank you in advance..
Do you mean just sending a variable to the view?
$data['error'] = "error message";
$this->load->view('some/view', $data);
Well! you shoul run validation when your form is submited. So, imagine you have 2 fields submited. It would be like this:
$this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
$this->form_validation->set_rules('mem_id', 'ID Member', 'required|trim|is_unique[yourtable.mem_id]|xss_clean');
if ($this->form_validation->run()){
// Do your cool stuff now because validation passed
}else{
// Whatever...validation fails, load your view.
}
So in your view you shoul have somethin like this at top of the form:
<?php echo validation_errors(); ?>
is_unique[yourtable.mem_id] will validate if the input is unique in your DB. So if is really unique, OK.
I am using form validation of CodeIgniter, it works fine but when, form validation fails, it does not display the validation errors, by using
<?php echo validation_errors();?>
I am using
function insertProduct(){
$this->load->library('form_validation');
$this->form_validation->set_rules('pname','ProductName','trimirequired');
if($this->form_validation->run()){
$this->addProduct();
}
else{
$this->load->model('inventory/stock');
}
In your view you should have something like (this example shows errors individually);
<?php echo form_error('p_name'); ?>
<label for="p_name">Product Name</label>
<input type="text" id="p_name" name="p_name" value="<?php echo set_value('p_name'); ?>" />
You need to tell the method in your controller to render a view on success/failure of the form validation.
If you change your insertProduct method to the following, it 'should' solve your issue.
function insertProduct(){
$this->load->library('form_validation');
$this->form_validation->set_rules('pname','ProductName','trimirequired');
if($this->form_validation->run()){
$this->addProduct();
$this->load->view('{name_of_your_view}');
} else{
$this->load->model('inventory/stock');
$this->load->view('{name_of_your_view}');
}
}
Where 'name_of_your_view' is the view that you've placed the validation_errors() code in.
This example from the CodeIgniter tutorial pages explains how to validated submitted data to display validation errors at the header of the form like you might expect:
http://codeigniter.com/user_guide/tutorial/create_news_items.html
The example code for the creation function looks like this:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
As others have said, though, you need to add a view to handle success and return them to the form to display errors on failure.
We can change the line containing following code:
$this->form_validation->set_rules('pname','ProductName','trimirequired');
to:
$this->form_validation->set_rules('pname','ProductName','trim|required');
if($this->form_validation->run($this) == false)
{
$this->addProduct();
}