I'm building a small application that will basically display 3 forms... the first one is optional, the second not and the third not. I'd like each form to be a separate url(controller). While reading the documentation for the CodeIgniter form_validation, it appears that the form can only submit to it's self to validate. If that's the case, the forms would keep showing on the same page... he's basically what I have... and commented in what i'd like to do...
class Index extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
//load front page that contains first form...
$content['title'] = 'Home';
$content['view'] = 'pages/index';
$this->load->view('template/default',$content);
}
function step_two()
{
//recieve information from front page. validate form. If validation is
//successful continue to step_two(url) if it fails redirect
//to front page with error...
$this->form_validation->set_rules('serial', 'Serial Number', 'required');
if ($this->form_validation->run() == FALSE)
{
//return to front page...
}else{
//do necessary work and load step_two view
}
}
}
?>
This is a snippet from my idea. But what I'm noticing is that you can't have form validation unless the form submits to it's self. Any ideas? Should I validate the form then just redirect to the new url/function?
Thanks guys...
this is how you do it
Controller -
public function step1()
{
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform1');
}
else
{
//do something to post data
redirect('/controller/step2');
}
}
public function step2()
{
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform2');
}
else
{
//do something to post data
redirect('/controller/step3');
}
}
so answer to your question is yes, you keep these in the same method and redirect on successful validation.
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 have the following method in my controller:
(shortened version but all the key pieces are here...)
class Widget extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('widget_model');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function assign()
{
//logic to display form.
if ($this->input->method() == "get" ) {
$data['save_status'] = '';
$data['title'] = 'Assign Widget';
$data['main_content'] = "assign";
$this->load->view('includes/template',$data);
}
else
{
//logic to handle POST
...
$data['save_status'] = 'Sucessfully saved to db';
$data['title'] = 'Assign Widget';
$data['main_content'] = "assign";
$this->load->view('includes/template',$data);
}
}
Everything works, except I don't know what the proper way to clear the form data is... because when I press F5 thinking I'm refreshing my page, it's actually resubmitting the data to the database.
Sorry, i'm sure this is a noob question.
Thanks.
EDIT 1
for now, I added a redirect at the end of post logic like this;
redirect(base_url()."index.php/thesamecontroller/thesamemethod");
and that takes the user to the same page, but without the form data being available.
Is this the best way to handle it?
I have this admin panel where I use different data-toggle tabs.
In CI(3), if I go with redirect('user/dashboard#new'); , it redirects me to correct section of view but not with form_validation errors.
And if I try $this->dashboard('user/dashboard#new'); it renders the errors but leads me to the wrong section of page (not at #new).
I have just started developing with CI and looking for some help from seniors.
Thanks in advance.
Controller (user)
public function dashboard() {
if($this->session->userdata('is_logged_in')){
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
}else{
$data['session_error']='Either the session has expired or you have tried to access this page directly';
$this->load->view('../../templates/vacations/headfoot/header-login');
$this->load->view('../../templates/vacations/users/session-error', $data);
$this->load->view('../../templates/vacations/headfoot/footer-login');
}}
Form Validation
if($this->form_validation->run() == FALSE)
{
$this->dashboard('user/dashboard#new');
} else {
$this->load->model('model_users');
if($query = $this->model_users->insert_property_details())
{
redirect('user/dashboard#new');
} else {
redirect('user/dashboard#new');
}}}
$this->dashboard('user/dashboard#new'); just runs/calls the method within the current page. 'user/dashboard#new' does nothing because the method is not written to accept arguments anyway:
public function dashboard(/* arguments would be here normally */) { ... }
Redirecting right after running validation won't work because you will lose the validation errors when you load a new page.
You need to save the errors somewhere, such as to session data, then redirect to dashboard, and then load the errors from saved location and display them on the dashboard view.
Here's an example using session data.
Form method:
if($this->form_validation->run() == FALSE)
{
$this->session->set_userdata('validation_errors', validation_errors());
$this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
redirect('user/dashboard#new');
}
else { ... }
Dashboard method:
public function dashboard()
{
if($this->session->userdata('is_logged_in')){
$data['validation_errors'] = $this->session->userdata('validation_errors');
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
} else { ... }
}
Getting error array from form validation class (for comments below):
class MY_Form_validation extends CI_Form_validation {
public function error_array()
{
return $this->_error_array;
}
}
I was trying to redirect after signin in yii framework but its not working i want to redirect to customer/dashboard after signin here's the code i am working
public function actionLogin() {
if (Yii::app()->user->getId() !== null) {
$url = Yii::app()->createUrl('customer/dashboard');
$this->redirect($url);
// $this->redirect(Yii::app()->createUrl('customer/dashboard'));
}
$this->layout = "main";
$model = new LoginForm;
$this->title = "Login";
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if (isset($_POST['LoginForm'])) {
$model->attributes = $_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if ($model->validate() && $model->login()) {
// we are now redirecting to the home page after login instead (as
// in update note above). IF the user is not applying for a job.
$url = Yii::app()->createUrl('customer/dashboard');
$this->redirect($url);
// $this->redirect(Yii::app()->createUrl('customer/dashboard'));
} else {
$_SESSION['pageName'] = "/UserEvents/LoginError";
}
}
// display the login form
$this->render('login', array('model' => $model));
}
IN that i am using actionLogin function to login
regards
Change the code Yii::app()->createUrl('customer/dashboard'); to Yii::app()->urlManager->createUrl('customer/dashboard'). Also you can pass an array to redirect() function and Yii will take care of generating correct url $this->redirect(['customer/dashboard']);
If you want to use createUrl function then follow below pattern
$this->redirect(Yii::app()->createUrl('/modulename/controllername/action'));
And If you want to redirect without createUrl then use below one
$this->redirect(array('controllername/action'));
Use this code for redirect.
if (Yii::app()->user->getId() !== null) {
$this->redirect(array("customer/dashboard"));
}
I am trying to create a new page that will bring up a form for them to enter a name for a group. It will then create the group and add them as a member to it. I have a similar sign up that I have copied and changed to fit the needs of this, but when I try to access it it gives me a 404 message.
I am accessing the controller (groups.php):
<?php
class Login extends CI_Controller {
function index() {
$this->load->view('includes/header');
$this->load->view('group_view');
$this->load->view('includes/footer');
$this->createGroup();
}
function createGroup() {
$this->load->library('form_validation');
// validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
if ($this->form_validation->run() == FALSE) { //didnt validate
$this->load->view('includes/header');
$this->load->view('group_view');
$this->load->view('includes/footer');
} else {
$this->load->model('model_groups');
if($query = $this->model_groups->createGroup()) {
//$data['account_created'] = 'Your account has been created.<br/><br/>You may now sign in.';
$this->load->view('includes/header');
$this->load->view('home_view');
$this->load->view('includes/footer');
} else {
$this->load->view('includes/header');
$this->load->view('group_view');
$this->load->view('includes/footer');
}
}
}
}
?>
I can post the model and view if necessary but Im pretty sure the problem is in the controller as that is what I am trying to load. Please let me know as soon as possible what my problem is! Thanks guys!
You should change your class name Login to Group