If form validation fails - forms

I want to redirect people to a certain page if the form validations fails. However, I can’t quite figure out how.
If i redirect people at the REDIRECT HERE comment below, it also redirects when it loads up the form and causes a endless loop.
public function create() {
$this->form_validation->set_rules('email_adress', 'E-mail', 'required|valid_email|is_unique[users.email_adress]');
if ($this->form_validation->run() !== FALSE) {
// PASSED
}
else {
// REDIRECT HERE
}
$this->load->view('user_register_view');
}
How can I achieve this?

You would redirect like so:
redirect('insert_URI_here');
Or if you are passing it to another method in the same controller you could just do :
$this->method_name();
However, if you just drop that in your else statement, user_register_view will not load because $this->form_validation->run() will return false either on validation error or non submission of the form.
What you will have to do is add another check to look for validation errors. If validation has failed and there are no validation errors, then your form hasn't been submitted.
So you could do something like this:
public function create() {
$this->form_validation->set_rules('email_adress', 'E-mail', 'required|valid_email|is_unique[users.email_adress]');
if(($this->form_validation->run() == FALSE) && ($this->form_validation->error_string() == ''));
//form not submitted yet
$this->load->view('user_register_view');
else if ($this->form_validation->run()) {
// PASSED
} else {
//Validation errors
redirect('insert_URI_here');
}
}
You will have to play around with it. I think $this->form_validation->error_string() should return an empty string if not submitted, but it might be a null value or a false (sorry can't remember off the top of my head).

Related

Form redirect for confirmation

Form redirect for confirmation can be currently managed using one of these two options:
1/ Flash message: using flashbag on the form page or another page like this:
$this->addFlash('success', 'Thank you');
return $this->redirectToRoute('confirmation_page');
2/ Confirmation page: using a dedicated confirmation like this:
return $this->redirectToRoute('confirmation_page');
BUT using option 2 makes the confirmation_page directly accessible from the browser without having submitted the form before. I am currently using flashbag mechanism to fix it by adding a $this->addFlash('success', true); before the redirection in the form and then checking the flashbag content in the confirmation page so that the route is accessible only once after being successfully redirected from the form.
Is there any best practice or more appropriate way to manage it?
/**
* #Route("/confirmation", methods="GET", name="confirmation_page")
*/
public function confirmation(): Response
{
$flashbag = $this->get('session')->getFlashBag();
$success = $flashbag->get("success");
if (!$success) {
return $this->redirectToRoute('app_home');
}
return $this->render('templates/confirmation.html.twig');
}
Flash Message is designed to display messages. Instead, use sessions in your application.
When submitting the confirmation form, create a variable in the session before the redirect
$this->requestStack->getSession()->set('verifyed',true);
return $this->redirectToRoute('confirmation_page');
Use the created variable in your method
public function confirmation(): Response
{
if (!$this->requestStack->getSession()->get('verifyed')) {
return $this->redirectToRoute('app_home');
}
return $this->render('templates/confirmation.html.twig');
}
Don't forget to inject the RequestStack into your controller
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

yii2 binds redirect to each link after first request

public function actionDone($id)
{
if ($model = $this->findModel($id)) {
$model["status"] = 3;
if ($model->save()) {
return $this->redirect(['test/index']);
}
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
It works only for the first time for each link. After that its just redirects to the 'test/index' without doing anything. Seems like browser (or smth else) remember, that if we open, for example, page site.com/?r=test/done&id=2 it should redirect to 'test/index' anyway.
Why is that? How can I fix it?
I even tried put die(); in the beginning of the method - anyway it redirects to 'test/index' until I use different link with another ID.
Thanks!

Implementing Pass-Through with Grails Filters

Please note: Although I'm using the Grails Webflow plugin here, I am pretty sure this is just a generic question that any battle-weary Grails veteran could answer.
Grails 2.4.4 here. I have a need for a Grails Filter that inspects all traffic coming into a particular controller. If the app is in a particular state, I need to redirect traffic to another controller/action. Else, I need the filter to act as a no-op/pass-through filter (meaning it does nothing; just allows the request to pass through it and on to its intended destination):
package filters
class MyAppFilters {
def filters = {
fizzFilter(controller: 'fizz', action: '*') {
before = {
if(AppStateHolder.checkState() == AppState.Blue) {
redirect(controller: 'auth', action: 'unauthorized')
return false
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
???
}
}
}
}
}
I have everything working perfectly for when the app state is AppState.Blue (so, the if-condition). The problem is that when app state isn't "blue", and that else executes, I keep getting infinite redirect errors. I believe Grails Webflow is complicating things, but I can't really fix anything with how it has been implemented.
I've also tried returning true/false from inside the else like so:
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
return false // Also tried 'true'
}
But this produces the same infinite redirect errors. It looks like I need to do some kind of redirect/render/etc. inside this else or webflow will cause problems.
So I'm looking for a way to tell Grails to redirect to whatever was the controller/action off the request. Something like:
} else {
// Allow request to continue on to its intended controller/action target ('no-op'/pass-through)
redirect(controller: req.controller, action: req.action)
return false
}
Is this possible, if so, how (specifically)?

Drupal form error redirect

I need help for this problem:
In my Drupal 7 site i need to perform a redirect to a certain page if the login form is invalid, only in this occasion, and only for login form.
What is the best way to do this? what function is called?
Thanks in advance.
You have to use hook_form_alter and add or alter the #validate options. Prefer the second method:
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_login') {
$form['#validate'][] = 'MYMODULE_user_login_form_validate';
}
}
function MYMODULE_user_login_form_validate($form, &$form_state) {
// Do some validation here. Better take the same codes from the validation functions below.
if (...) {
drupal_goto('CUSTOM_PATH');
}
}
You can find the validation functions inside ROOT/modules/user/user.module file line 2113:
function user_login_default_validators() {
return array('user_login_name_validate', 'user_login_authenticate_validate', 'user_login_final_validate');
}
Similar questions:
(6.x, although it is the same) Drupal Modify Login System
https://drupal.stackexchange.com/questions/22952/custom-validation-for-a-form
using hook_user_login method you can achieve the same,
/**
* Implements hook_user_login();
*/
function MODULE_NAME_user_login(&$edit, $account) {
$url = 'PAGE_TO_BE REDIRECTED';
if (isset($user->uid) && $user->uid > 0) {
drupal_goto($url);
}
}

Symfony form values missing

I was writing a simple login form, everything works fine (validation etc.) but I can't get the values, there's my code:
public function executeIndex(sfWebRequest $request)
{
$this->getUser()->clearCredentials();
$this->getUser()->setAuthenticated(false);
$this->form = new LoginForm();
if ($request->isMethod('post') && $request->hasParameter('login')) {
$this->form->bind($request->getParameter('login'));
if ($this->form->isValid()) {
$this->getUser()->setAuthenticated(true);
$this->getUser()->addCredential('user');
$this->login = $this->form->getValue('login');
}
}
}
$this->login is NULL. Now I checked almost everything, the form is valid, isBound() is true, count() returns 3, I can see the values in my request:
parameterHolder:
action: index
login: { login: foo, password: foo, _csrf_token: 53ebddee1883d7e3d6575d6fb1707a15 }
module: login
BUT getValues() returns NULL, getValue('login') etc. returns NULL as well. How can it be?
And no, I don't want to use sfGuard-Plugins ;)
What about trying something like this
$form['value_name']->getValue()
Is it still NULL?
Also is it possible that you created a custom post validator?
Callback validation must return values back to caller:
return $values;