CodeIgniter loading a view - php

A function in the controller User loads the signup form, eg:
public function signup()
{
$this->load->view('form_view');
}
So the signup form url will be like root/user/signup (ignore index.php)
The form_view has a form, which is submitted to another method in the same controller, lets say process(), eg.
<? echo form_open('user/process') ?>
Once the form is submitted, it goes to the user/process, which contains the validation code. If there is any validation error, It would load the form_view again and pass the error data to display on the form.
if ( $this -> form_validation -> run() === FALSE )
{
//$this -> load -> vars( $data );
$this -> load -> view( 'form_view' );
}
Everything works fine. But now since the same form_view is loaded from the user/process function, the url of the form will change from:
root/user/signup
to
root/user/process
Because the form was submitted to the url user/process and the form_view is being called from the process(). Is it possible to somehow redirect or maintain the original form url(root/user/signup) when the form_view is loaded from the process function in case of errors?
Thanks.

Using the same method to for and validation you can try something like
if( count($this->input->post()) > 0 )
{ // do validation
if ($this->form_validation->run())
{
// initilize and
redirect('logged');
}
}
$this->load->view( 'form_view' );
you always get the same method, as you asked

I've done this a different way. I actually have one controller to do all this.
if ( $this -> form_validation -> run() === TRUE )
{
code
code
redirect('ANOTHER_URL_WITH_VALIDATION_CODE_DISPLAY');
}
$this -> load -> view( 'form_view' );
Sorry about that, I meant to say one controller NOT one method. If you're just looking for form validation, then throw it all into one controller. Have CI's built-in form_validation function do the validation for you. IF you require backend validation (i.e. username check), you can create a custom validation rule called a "callback" and have the form validate against that as well.
Ideally your controller will check all fields, if it passes, it should redirect to onward to the next page w/ any information you want to display (i.e. "Thank's for registering!"). If it fails the check, it should load the view page again; you'll need to repopulate the fields obviously.

See my response here for a detailed explanation of how/why the Codeigniter form validation should be called:
Question about how redirects should work

Related

Symfony2 - Redirect after form processing in form type

I'm trying to figure out how to work with dynamic forms/form types in Symfony 2. I've managed so far, but what I don't know and can't find is how to redirect after the POST_SUBMIT event.
Do I do it inside the controller or does it happen directly in the form type class? What happens atm is the page reloads, but the data in the page is not updated. Plus I would like to redirect to another page.
Thanks in advance.
From the doc :
if ($form->isValid()) {
// perform some action, such as saving the task to the database
return $this->redirect($this->generateUrl('task_success'));
}
the isValid() method will return true only when a POST request has been performed and when the data sent is valid.
If you want to redirect after POST request even in the case the form is not valid, you can do this :
public function newAction(Request $request)
{
// ...
if ($request->isMethod('POST')) {
if ($form->isValid()) {
// perform some action...
}
return $this->redirect($this->generateUrl('page_after_post_request'));
}
// ...
}

Handling contact form submissions Codeigniter

I've inherited a website built using Codeigniter (v2.1.4). The client has asked for a change, and I'm not sure of the best way to achieve it.
I have the following method in the Main controller that powers a new vans page.
public function new_vans($slug = null){
$this->load->view('inc/header_view');
if($slug === NULL){
//If no slug is provided, show all new vans
$this->load->view('new_vans_view');
}else{
//If there is a slug, just show the selected van, or redirect if nothing returned
$data['new_van'] = $this->Database->getSingle('new_vans', array('slug' => $slug));
if(!empty($data['new_van'])){
$this->load->view('new_van_details_view',$data);
}else{
redirect('/new-vans');
}
}
$this->load->view('inc/footer_view');
}
The client has asked for a contact form to be added to a couple of pages including this one, and my question is, should I create a new method that just handles the contact form submissions? If so, how would I handle sending validation errors back to the page? The contact forms will all have the same fields, so I would guess creating a new method is the way to go?
Partial Views(forms)
Partial views are good for forms, they can be re-used
like your client has requested.
Returning views as data
There is a third optional parameter lets you change the behavior
of the function so that it returns data as a
string rather than sending it to your browser.
This can be useful if you want to process the data in some way.
If you set the parameter to true (boolean) it will return data.
The default behavior is false, which sends it to your browser.
Remember to assign it to a variable if you want the data returned:
$string = $this->load->view('myfile', '', true);
Master layouts
To create a Master layout so you can wrap your views
create a new file inside your views directory
views/master/layout.php
<body>
<?php $this->load->view($view); ?>
</body>
Controller
class someController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->template = 'master/layout';
}
public function index()
{
return $this->load->view($this->template, array(
'view' => 'somecontrollerview',
'contact_form' => $this->load->view('partials/forms/contact', array(), true)
));
}
}
somecontrollerview
Echo out the contact form(string)
<?php echo $contact_form; ?>
Contact Controller
Create a new Controller to handle your form validation
The client has asked for a contact form to be added to a couple of pages including this one, and my question is, should I create a new method that just handles the contact form submissions?
create a new controller and new methods
If so, how would I handle sending validation errors back to the page?
look through the codeigniter documentation for form validation. basically if they have an error you are going to show them a view with the form again. it does not matter which page they came "from".
The contact forms will all have the same fields, so I would guess creating a new method is the way to go?
you need to validate the form fields, hopefully capture the contact info to a database, send an email confirmation to the customer, and send an email to the sales person unless its being done directly from the database, and then show a view with a thank you.
each one of those steps is a separate method.
optionally you can show the email address on the thank you page saying 'we have sent you a copy to the email address: something#gmail.com -- that way if the customer messed up the email address they can go back and correct it.

Laravel: Redirect within custom class?

I'm not sure this is the "proper" way of doing things, but the logic is working. I have my own class in my Laravel setup, which I use in a controller. Within my controller I call a function in my custom class, however I'd like to Redirect the user if something happens within that function.
After chatting on IRC, I'm told you can't do a Redirect within your own class, you have to "return the redirect response object from the controller".
Not entirely sure what this means, but I guess you have to do the redirects from the controller instead.
Code (simplified, it is working):
Controller method:
// Validate the incoming user
$v = new SteamValidation( $steam64Id );
// Check whether they're the first user
$v->checkFirstTimeUser();
This goes away to my SteamValidation class (app/Acme/Steam/SteamValidation.php and namespaced), and it does a check:
public function checkFirstTimeUser() {
// Is there any users?
if( \User::count() == 0 ) {
$user_data = [
// Data
];
// Create the new user
$newUser = \User::create( $user_data );
// Log that user in
\Auth::login($newUser);
// Redirect to specific page
return \Redirect::route('settings');
}
return;
}
Now if the count is over 0, then it just returns back to the controller and I happily carry on. However, if it's a new user and I try to do a redirect (return \Redirect::route('settings');) I get a blank page!
So my questions are:
Why am I unable to redirect from here?
What would be the correct way to return a response back to the controller, and then do the redirect?
The reason you can't redirect from your nested method is that simply calling Redirect::route() does not fire a redirect off. Your controller's method will return something Laravel then looks at to decide what to do - if it'a a View it'll display it, if it's a Redirect it'll do a redirect. In your nested method, you can return what you want but as long at the controller it's not passing that down the line then it's no good for you.
Also you probably shouldn't return a Redirect in a helper function anyway. If your validate function has a boolean response (yes all good, no something's bad) then you can simply return true/false and then pick that up in the controller to do the redirect:
// Validate the incoming user
$v = new SteamValidation( $steam64Id );
// Check whether they're the first user
if ($v->isFirstTimeUser()) { // note I renamed this method, see below
return \Redirect::route('settings');
}
However, while we're taking the responsibility of redirecting away from your validation method, you should also take the responsibility of creating the user away:
// SteamValidation
public function isFirstTimeUser() {
return (\User::count() == 0);
}
// Controller
// Validate the incoming user
$v = new SteamValidation( $steam64Id );
// Check whether they're the first user
if ($v->isFirstTimeUser()) {
// you may even wish to extract this user creation code out to something like a repository if you wanna go for it
$user_data = [
// Data
];
// Create the new user
$newUser = \User::create( $user_data );
// Log that user in
\Auth::login($newUser);
// Redirect to specific page
return \Redirect::route('settings');
}

CakePHP form validation before sending post to the requested controller function

My validation is working as it stands, but I want to display the validation error prior to the search controller. I understand this might not be possible within the CakePHP framework.
I have a model plan.php. And in the plans_controller.php, I have a function called search().
My form calls search() as expected (because there is no search model):
echo $this->Form->create('Plan', array('action' => 'search'));
As it stands, when I submit my search, the errors are displayed and the url changes to .../search, so no results are displayed ("There are 0 results for that search criteria", but the correct validation errors are displayed below required form fields.
I do not want the .../search url to be displayed. I want the form to "halt" and just display the validation errors w/out changing the url to the search function.
I am calling the search form within an element because the search form displays on several different pages.
To sum this up: The search form should validate w/out changing the url path to the controller action name of the search. Of course, the validation is done IN the search() and plan.php model, so I just don't know how to work around this and wondering if its even possible.
You can use the validates() method of the model to check whether it validates and then redirect back.
Assuming your model is called Plan, this would be your controller
$errors = array();
if (!$this->Plan->validates($this->data)) {
//errors occured
$errors = $this->Plan->invalidFields();
$this->Session->save('Plan.errors', $errors);
$this->redirect('/plans');
}
And in your view.
if ($this->Session->check('Plan.errors')) {
$errors = $this->Session->read('Plan.errors');
$this->Session->delete('Plan.errors'); //don't want it again
}
In both cases, make sure Session helper/component is actually assigned to your view and controller
OK. Cracked out a working solution w/ implode, but now my errors are only displayed in the default layout and no longer under the form fields where that belong.. So now I need to know how to get the errors back below the form fields..
Working code:
...else {
$errors = $this->Plan->invalidFields();
$error_messages = implode(' ',$errors);
$this->Session->setFlash($error_messages);
$this->redirect('/');
}...

PHP advice please. (Kohana experience a plus)

I've been working with PHP for about a year, but I do it as a hobby. I dont have anybody I can go to as a teacher or a mentor to give me advice on what I may be doing completely wrong, or what I could do better. I've done quite a few different things within that year, so I wouldnt consider myself a complete noob.
Anyways, I have just started using a framework (Kohana), and there really arent that many tutorials out there, so I'm not entirely sure if I'm doing things in a good way.
I have a few code snippets that I would like to post to get some feedback pertaining to what I just said.
For Starters
User Controller
class User_Controller extends Template_Controller{
public function register()
{
// logged in users cant register
if($this->logged_in)
{
url::redirect('user/profile');
}
// initially show an empty form
$form = $errors = array
(
'username' => '',
'email' => '',
'password' => '',
'gender' => '',
'dob_month' => '',
'dob_day' => '',
'dob_year' => '',
'date_of_birth' => '',
'captcha' => '',
'registration' => ''
);
// check for a form submission
if($this->input->post('register'))
{
// get the form
$post = $this->input->post();
// prepare the data for validation
$post['date_of_birth'] = "{$post['dob_year']}-{$post['dob_month']}-{$post['dob_day']}";
// create a new user
$user = ORM::factory('user');
// validate and register the user.
if($user->register($post, TRUE))
{
// SEND EMAIL
// login using the collected data
if(Auth::instance()->login($post->username, $post->password, TRUE))
{
// redirect the user to the profile page
//url::redirect("user/profile/{$user->id}");
}
}
// get validation errors and repopulate the form
$form = arr::overwrite($form, $post->as_array());
$errors = arr::overwrite($errors, $post->errors('registration_errors'));
}
// template variables
$this->template->title = 'Sign Up';
$this->template->body = new View('layout_1');
// layout variables
$this->template->body->left = new View('user/registration_form');
$this->template->body->right = 'Right Side Content';
// registration form variables
$this->template->body->left->form = $form;
$this->template->body->left->errors = $errors;
$this->template->body->left->captcha = new Captcha('register');
}
}
Register Function within User_Model
class User_Model extends ORM{
public function register(array& $user, $save = FALSE)
{
$user = new Validation($user);
// logged in users cant register
if(Auth::instance()->logged_in())
{
$user->add_error('registration', 'logged_in');
return FALSE;
}
// trim everything
$user->pre_filter('trim')
// everything is required
->add_rules('*', 'required')
// username must be 5 - 30 alphanumeric characters and available
->add_rules('username', 'length[5,30]', 'valid::alpha_numeric', array($this, 'username_available'))
// email must be valid format and available
->add_rules('email', 'valid::email', array($this, 'email_available'))
// password must be 5 - 15 characters and alpha dash
->add_rules('password', 'length[5,15]', 'valid::alpha_dash')
// gender must be either male or female. capitalize first letter
->add_rules('gender', array($this, 'valid_gender'))
->post_filter('ucfirst', 'gender')
// dob must be a valid date, and user must be old enough.
->add_callbacks('date_of_birth', array($this, 'check_dob'))
// captcha must be entered correctly.
->add_rules('captcha', 'Captcha::valid');
// add the registration date
$this->registration_date = date::unix2mysql(); // helper function transforms the current unix to mysql datetime format
// validate the information. an ORM function.
$result = parent::validate($user, $save);
// was the user info valid?
if($result === TRUE)
{
// was the user saved?
if($save === TRUE)
{
// add a login role
$this->add(ORM::factory('role', 'login'));
$this->save();
}
}
else
{
$user->add_error('registration', 'failed');
}
return $result;
}
}
Mostly all my models follow the same format when validating info.
I have some other things I would appreciate feedback on as well, but I dont want to overwhelm anybody.
Thanks a lot for your time
EDIT: I'm sorry, I should've posted both the user controller and model. I've been reading alot about how models should be fat, and controllers should be skinny. Thats why I created a register function in the model to validate the info instead of doing so within the controller. The register function takes an array, but turns that array into a validation object so that I can retrieve the user input, and the errors. I've seen a few tutorials on Kohana where it was done this way.
First, I would not put the register() method into the User model. A model should be a representation of the object in the database and generally only contains your "CRUD" methods (create, retrieve, update, delete), getter and setter methods, and maybe some static helper methods related to the model. By putting your register() method into the model, you're making the model do presentation logic that should really be done by a User controller, since this is a response to a user action. A controller handles user actions, validates those user actions, then updates the model if the validation is successful.
In your example, the user is attempting to create a new account. He fills out a form and clicks submit. The form's POST action should point to a controller's method, like /user/register, and that method will use the Validation library to validate the form data sent by the user. Only if that data validates successfully should you create a User model instance, set the properties of that model to what the user input, and then use the model's save() method to save to the database. If the validation fails, you report the error to the user and you don't create a User model at all since you don't have a valid data set to create a model with yet.
Next, you are checking to see if the user is logged in. Again, this should be in the controller, not the model. Besides that, the user should not be able to get to this register process in the first place if he is already logged in. The controller method that creates the user registration form view should check to see if the user's logged in, and if he is, then he should be redirected to another page. Even if the user is playing tricks and manages to submit the form (maybe he logged in via another window while having the form open in an old window), your register method should check for that first and not create a $user Validation object yet.
I can see in your code that there are some confusing items based on your model set up. For example, you're passing the $user array into the method, which I presume is the form data. But you're using the "pass by reference" operator (&) which is unnecessary in PHP5 since all objects are now passed by reference. But after that you're recasting $user as a Validation object. Are you using the $user Validation object elsewhere and require it to be passed by reference? If so, that's another flaw in the logic as all of this processing needs to be in the controller and the $_POST values can be used directly in the controller instead of having to pass around a Validation object.
Later on, you're validating the user information with parent::validate($user, $save). Why is the validate() method being called on parent as a static method? If this is a model, it should be extending Kohana's core Model class, and "parent" references the Model class. Is your model extending the Validation class? Also, why are you passing in the $user Validation object to the validation() method? Doing that is required if you need to do recursion (to validate elements again after making changes from previous filters), but it looks like you're not doing anything to require recursion. You should be calling validate() on the $user Validation object:
$user->validate();
without any arguments. The validation errors will become part of the $user object, so you can check for errors using
$user->errors();
Finally, while Kohana allows you to use method chaining, I would not use one long chain to set up the rules and other items for the validation. It's confusing and may cause debugging to be difficult. Put each of those on its own line and perform each directly on the $user object.
I dont know Kohanna so im not sure what the lay of the land is on their MVC separation but typically i would make register an action on a controller. The main thing i disagee with in your code is that the Model is coupled to the Authentication system internally. The authentication check should be made outside the class and the control flow decision should be made outside as well, OR the result of the authentication check should be passed in to the Model for use in its internal operation.
Typically i might do something like the following pseudo code:
// in my controller class for User or whatever
public function registerAction()
{
// get the form data from the request if its POST, ortherwise a blank array
$userData = $this->getRequest('user', array(), 'POST');
// create a user
$user = new User($userData);
if(Auth::instance()->logged_in())
{
// we are logged in add an error to the user object for use by the view
$user->getValidator()->add_error('registration', 'logged_in');
}
elseif($user->is_valid())
{
// user data is valid, set the view with the success message
$user->save();
$this->setView('register_success');
}
/**
* render the designated view, by default this would be the one containing the
* registration form which displays errors if they exist - however if we success-
* fully registered then the view with the success message we set above will be
* displayed.
*/
$this->render();
}

Categories