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.
Related
I am new to developing, and I want to do a simple validation in Codeigniter. I don't know where I am going wrong.
This is my form to be validated
And my controller is this
public function __construct()
{
parent::__construct();
$this->load->model('M_menu');
$this->load->model('master/M_user_type');
$this->load->helper(array('form'));
$this->load->library('form_validation');
}
public function index()
{
$data['menus']=$this->M_menu->getSideBarMenu_m();$data['error_message'] = '';
$this->load->view('master/V_user_type',$data);
}
function saveUserType_c()
{
$data['error_message'] = '';
$this->form_validation->set_rules('userType', 'UserTypeName', 'required');
echo var_dump($this->form_validation->run());
if (!$this->form_validation->run() )
{$data['menus']=$this->M_menu->getSideBarMenu_m();
$data['error_message'] .= validation_errors();
$this->load->view('master/V_user_type',$data);
}
else
{
$insert=$this->M_user_type->saveUserType_m();
if($insert){
$response=array("insert"=>true);
}else{
$response=array("insert"=>false);
}
echo json_encode($response);
}
}
With this I get the network error, and data is being saved to db. But no action in form(form not loading). Please guide me, where I go wrong. Also, if I need to give any more details
Please check your usertype view page, you can also load usertype view page before validation, so that you will be confirmed that usertype view page exist, I hope you understand my point.
You have to load a model before you call its methods. Use this:
$this->load->model('M_user_type');
$insert=$this->M_user_type->saveUserType_m();
I created a new controller questions.php which is not the default controller. I have already loaded the form_validation library but it did not echo out the validation errors when I submitted empty fields from my form. Other questions in SO seems to be completely unrelated to my situation.
Below is my questions.php controller. BTW I have only posted the necessary bits of the class controller.
<?php
class Questions extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->helper(array('form'));
$this->load->library("form_validation");
}
public function user_logged_in(){
$this->load->library('../controllers/main');
$obj = new $this->main();
$status = $obj->logged_in();
if($status){
return true;
}else{
return false;
}
}
public function question_validation(){
if($this->user_logged_in() == false){
redirect('main/index');
} else{
$this->load->library('form_validation');
$this->form_validation->set_rules('input_title','Title','required');
$this->form_validation->set_rules('message','Question Details','required');
$this->form_validation->set_rules('vidya','Tags','required');
if($this->form_validation->run() == false){
$this->load->library('../controllers/main');
$obj = new $this->main();
$getUserData = $obj->getLogged_userData();
$userdata = array(
'uservalues' => $getUserData
);
$this->load->view('view_header');
$this->load->view('view_nav',$userdata);
$this->load->view('log_user/user_second_nav');
$this->load->view('question_posts/post_questions');
$this->load->view('view_footer');
} else{
echo "form has been submitted";
}
}
}
?>
The form page in view called post_questions.php which is shown below,
<?php
$this->load->helper("form");
echo validation_errors();
echo form_open('questions/question_validation');
?>
<p><h2>Question title</h2></p>
<p><input type="text" name="input_title" id="input_title"
placeholder="what is your question please be specific and clear"></p>
<p><h3>Now Describe in detail!</h3>
<p><textarea id="message" name="message" rows="10px" cols="86px"></textarea></p>
<p><h3>Tags</h3></p><p><input type="text" id="vidyagames" name="vidya"></p>
<p><input type="submit" name="post" value="Submit"></p>
</form>
Before this, the questions controller was giving me error like undefined property Questions::$form_validation so I added a __construct() and now it wont echo out the form validation errors.
Can you help me because probably the form isn't being submitted at all.
Thank You in Advance.
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
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();
}
I have a form for user to input some feedbacks, and this form has to be resided in a product detail page. I am required to print out some error validation on the detail page instead of having the form redirected to the feedback form page with the validation message.
The product detail page is located at 'index.php/product/view/1', while the feedback form is at 'index.php/product/add_feedback'.
How can I print out the error form validation message so that it shows on the product detail page, instead of redirection to the add_feedback. Thank you.
My controller:
class Product extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('mproduct');
$this->load->model('mfeedback');
}
public function index()
{
//get product details
$data['content'] = $this->mproduct->get_details();
$this->load->view('listing', $data);
}
public function add_feedback()
{
// feedback form
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean|max_length[200]');
$this->form_validation->set_rules('feedback', 'Feedback', 'required|xss_clean|max_length[200]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('feedback');
}
else
{
$pid = $this->input->post('pid');
$name = $this->input->post('name');
$feedback = $this->input->post('feedback');
$this->MFeedback->add($pid, $name, $feedback);
redirect('product/view/'.$pid);
}
}
}
Model:
class MFeedback extends CI_Model {
function add_feedback($name, $pid, $feedback)
{
$data = array(
'name' => $name,
'feedback' => $feedback,
'pid' => $pid,
);
$this->db->insert('feedback', $data);
}
}
view - feedback.php
<h1>Add Feedback</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('product/add_feedback'); ?>
<p>Name</p>
<input type="text" name="name" size="50" value="<?php echo set_value('name'); ?>" />
<p>Feedback</p>
<textarea type="text" name="feedback"><?php echo set_value('feedback'); ?></textarea>
<?php echo form_hidden('pid', $this->uri->segment(3, 0)); ?>
<div><input type="submit" value="Add Feedback" /></div>
</form>
Simple! Just add the validation to Product/index-method, like this:
class Product extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('mproduct');
$this->load->model('mfeedback');
}
public function index()
{
// feedback form validation
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean|max_length[200]');
$this->form_validation->set_rules('feedback', 'Feedback', 'required|xss_clean|max_length[200]');
if ($this->form_validation->run() == TRUE)
{
// the validation passed, lets use the form data!
$pid = $this->input->post('pid');
$name = $this->input->post('name');
$feedback = $this->input->post('feedback');
$this->MFeedback->add($pid, $name, $feedback);
redirect('form/success'); // redirect to a page, where the user gets a "thanks" message - or redirect to the product page, and show a thanks there (but be sure to use redirect and nocht $this->load->view(..), because then the form data would be still in the header and a reload of the page would send another mail :)
}
// form did not pass the validation, lets get and show the product details
$data['content'] = $this->mproduct->get_details();
$this->load->view('listing', $data);
}
}
And in the file feedback.php you'll have to change the form target to something like this:
<?php echo form_open('product/'.$this->uri->segment(3, 0)); ?>
... or even better:
<?php echo form_open('product/'.$content->id); ?>
... depends on your product view.