Drupal Node comment redirection when validation fail - forms

I've been banging my head on this trying to find a solution, searching all around for something that would work, but I got no chance.
I have a "dashboard" where users have a list of event they took part in where they can rate/comment the event. I'ts basically a custom comment form for a node that is not displaying on the node page itself. The user click on an icon in their dashboard next to the event they want to comment, they get to the form, fill it and it returns them back to the dashboard. The return is adding parameters with a custom submit function and using the redirect function to make sure the user return to the proper tab in their dashboard.
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'comment_node_event_form') {
$form['#submit'][] = 'customcomment_form_submit';
}
}
function customcomment_form_submit($form, &$form_state) {
if($form['#form_id']=='comment_node_event_form'){
$pos = strpos($_SERVER['HTTP_REFERER'], 'qt-dashboard');
if ($pos !== FALSE) {
$form_state['redirect'] = array(
'dashboard',
array(
'query' => array(
'qt-dashboard' => '2',
'qt-dashboard_event' => '2',
),));
}else{
$form_state['redirect'] = array(
'dashboard',
array(
'query' => array(
'qt-dashboard' => '2',
'qt-dashboard_event' => '1',
),));
}
}
}
This portion is working as it should and expected. The problem is when form validation fails, it send the comment form error message and form to refill to the node page instead of staying where it is.
I found that if I set the #action with the link where my comment form is, it does send the fail to the proper page
$form['#action']='/rating_comment/'.$form['#node']->vid.'?destination=dashboard&qt-dashboard=2&qt-dashboard_event=2';
But, doing so break the redirect when successfully submitting the form and it doesn't take the parameter in the redirect..it basically send the user directly to dashboard and scrapes the parameter. Now there might be a better solution for form validation fail to stay on the same page and that is pretty much what I am looking for.
Thanks

Looks like this form isn’t in your module - and you’re altering the other module.
Now, when the validate function gets invoked at the end you can check for failure and if there is failure cancel processing/redirect etc.
$form_state['no_redirect'] = FALSE:
Also, you can use the error function to check for errors and if so cancel the rest. This goes inside validate method.
if (form_get_errors()) { return FALSE ; }
// .. Otherwise, process validation
Check out the following
https://drupal.stackexchange.com/questions/170815/is-it-possible-to-stop-a-webform-form-during-submission
https://drupal.stackexchange.com/questions/5861/how-to-redirect-to-a-page-after-submitting-a-form

Related

Zend Framework 2 - Including a variable in a form action

My login form may be called with a re-direct query and I am wondering if there is a simple way to include this in the subsequent post action.
The use case is for SSO login.
My normal login route is:
/customer/login
and when called from a third party client becomes:
/customer/login?redirectTo=http://www.example.com
My login action:
public function loginAction()
{
$prg = $this->prg();
if ($prg instanceof Response) {
return $prg;
} elseif ($prg === false) {
return new ViewModel(['form' => $this->loginForm]);
}
This loads my view and I currently define my action as so:
$form = $this->form;
$form->setAttribute('action', $this->url());
Now when the action is called, I am losing the redirectTo parameter.
So my question is this, is it possible to update the action to include the re-direct url so that when a user clicks to login, it is posted back to my form?
thanks!
EDIT -
Obviously I can create a redirectTo route in the configs and test on the initial call to the page for the existence of such a route and include this in the form. My question however is whether or not this can be done automagically simply from the viewscript.
To generate query string arguments from the view helper, you need to assign them as the third argument using the query key. Please refer to the ZF2 docs http://framework.zend.com/manual/current/en/modules/zend.view.helpers.url.html
$form->setAttribute('action', $this->url('application', array('action' => 'login'), array('query' => array('redirectTo' => 'http://www.example.com,))));
$form->setAttribute('action', $this->url('login', [
'query' => [
'redirectTo' => $this->params()->fromQuery('redirectTo')
]
]);
Where 'login' is the name of the login route.
See Url View Helper
Well my solution is not as elegant as I hoped it would be. I wanted to avoid using the controller for the query params. As #Stanimir pointed out, the view helpers are in fact, to help with view so my original idea was unfounded.
This is an end to end of what I have put together:
Controller:
$redirect_url = $this->params()->fromQuery('redirectTo',null);
Returns this to view on initial load:
return new ViewModel( ['form' => $this->loginForm , 'redirect_url' => $redirect_url] );
View
$form->setAttribute(
'action',
$this->url(
'customer/login', [] ,
[ 'query'=>
[ 'redirectTo' => $this->redirect_url ]
]
)
);

How to use form validation in Drupal 7

I am trying to edit the checkout form in Drupal Commerce, to require a user to enter their email address twice. When they submit their form, Drupal should check to see if the emails match, and call form_set_error() if they don't. For now, I am just trying to attach a custom validation function to the form, which I can't get to work. (My module is called checkout_confirm_email. This module is only for our own use, so I didn't put much effort into the name).
function checkout_confirm_email_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'commerce_checkout_form_checkout') {
$form['#validate'][] = 'checkout_confirm_email_form_validate';
dprint_r($form['#validate']);
dsm("I printed");
}
}
function checkout_confirm_email_form_validate($form, &$form_state) {
dsm("Never prints...");
}
The dprint_r statment outputs Array ([0] => checkout_confirm_email_form_validate). So the function is part of the form array, but the dsm statement in the validation function never prints.
I've actually been stuck for a while. I've looked up examples, and I can't see what I'm doing wrong. Anyone?
You need to attach the #validate property to the form submit button like this:
$form['submit']['#validate'][] = 'checkout_confirm_email_form_validate'
And it'll work then it's not necessary that my example is identical match to your form tree you should search for the submit button array and apply this example to it
Instead of form_set_error() I would use form_error($form, t('Error message.'));
function checkout_confirm_email_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'commerce_checkout_form_checkout') {
$form['#validate'][] = 'checkout_confirm_email_form_validate';
dpm($form['#validate']);
dsm("I printed");
}
}
function checkout_confirm_email_form_validate(&$form, &$form_state) {
// Not sure the exact email field
if(empty($form['submitted']['mail']['#value'])){
dsm("Should see me now and return to the form for re-submission.");
form_error($form, t('Username or email address already in use.'));
}
}
You could use any validate function here
https://api.drupal.org/api/drupal/includes!form.inc/7
The listed validations would be
date_validate - Validates the date type to prevent invalid dates
(e.g., February 30, 2006).
element_validate_integer -Form element validation handler for
integer elements.
element_validate_integer_positive - Form element validation handler
for integer elements that must be positive
element_validate_number - Form element validation handler for
number elements.
password_confirm_validate - Validates a password_confirm element.
Ex of usage
$form['my_number_field'] = array(
'#type' => 'textfield',
'#title' => t('Number'),
'#default_value' => 0,
'#size' => 20,
'#maxlength' => 128,
'#required' => TRUE,
'#element_validate' => array('element_validate_number')
);
You can use _form_validate().
function my_form_form_validate($form, &$form_state) {
if ((valid_email_address($form_state['values']['field_candid_email'])) === FALSE) {
form_set_error('field_candid_email', t('The email address is not valid.'));
}
if (!(is_numeric($form_state ['values']['field_candid_montant']))) {
form_set_error('field_candid_montant', t('The field value must be numeric.'));
}
}
I changed this line:
$form['submit']['#validate'][] = 'checkout_confirm_email_form_validate'
to this:
$form['actions']['submit']['#validate'][] = 'checkout_confirm_email_form_validate';
And it's works !
Use the following code:
$form['submit']['#validate'][] = 'checkout_confirm_email_form_validate'

Zend form in a popup (fancybox, lightbox....)

I am developping a web application using Zend and I ran out of ideas for a problem I am having. In just a few words, I am trying to have a contact form in a popup (Fancybox, lightbox, colorbox or whatever...). The whole thing works fine, in the sense that it shows up the contact form in the popup and allows to send emails. However, whenever there are errors (unfilled input or filled wrong), I couldn't get those errors to be displayed on the popup (it actually redirects me back to the form in a normal display (view+layout), to show the errors.
It is perhaps possible but I now thought that perhaps I could more easily bring my error message to a new popup (the contact page, filled unproperly, would lead to a error popup page...). I think this alternative could look cool but am having real trouble doing it. Now my real question is : Can we really make a form on a popup, using Facybox (Lighbox or any other actually ... just want my popup) and Zend? Any Guru outhere??
Thanks a lot
here is the code:
the link for instance:
<a class="popLink" href=" <?php echo $this->url(array('module'=>'default', 'controller'=>'contact', 'action'=>'sendmail')).'?ProID='.$this->proProfil->getProID(); ?>">Contact</a>
the action:
public function sendmailAction()
{
$this->_helper->layout()->setLayout('blank');
$request = $this->getRequest();
$proID = $this->_getParam("ProID");
$professionalsList = new Model_DirPro();
$proName = $professionalsList->getProInfo($proID);
$translate = Zend_Registry::get('translate');
Zend_Validate_Abstract::setDefaultTranslator($translate);
Zend_Form::setDefaultTranslator($translate);
$contactform = new Form_ContactForm();
$contactform->setTranslator($translate);
$contactform->setAttrib('id', 'contact');
$this->view->contactform = $contactform;
$this->view->proName = $proName;
if ($request->isPost()){
if ($contactform->isValid($this->_getAllParams())){
$mailSubject = $contactform->getValue('mailsubject');
if ($contactform->mailattcht->isUploaded()) {
$contactform->mailattcht->receive();
//etc....
the form:
class Form_ContactForm extends Zend_Form
{
public function init ()
{
$this->setName("email");
$this->setMethod('post');
$this->addElement('text', 'mailsubject',
array('filters' => array('StringTrim'),
'validators' => array(), 'required' => true, 'label' => 'Subject:'));
$mailattcht = new Zend_Form_Element_File('mailattcht');
$mailattcht->setLabel('Attach File:')->setDestination(APPLICATION_PATH.'/../public/mails');
$mailattcht->addValidator('Count', false, 1);
$mailattcht->addValidator('Size', false, 8000000);
$mailattcht->addValidator('Extension', false,
'jpg,png,gif,ppt,pptx,doc,docx,xls,xslx,pdf');
$this->addElement($mailattcht, 'mailattcht');
$this->addElement('textarea', 'mailbody',
array('filters' => array('StringTrim'),
'validators' => array(), 'required' => true, 'label' => 'Body:'));
$this->addElement('submit', 'send',
array('required' => false, 'ignore' => true, 'label' => 'Send'));
$this->addElement('hidden', 'return', array(
'value' => Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(),
));
$this->setAttrib('enctype', 'multipart/form-data');
}
}
I would suggest implementing AJAX validation. This would allow for the form to be verified before it is submitted. ZendCasts has a good tutorial on how to accomplish this: http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/
Ajax requests are handled via the contextSwitch action helper. You can to specify the various contexts an action needs to handle (xml or json) in the init method of the controller as follows:
public function init()
{
$this->_helper->contextSwitch()
->addActionContext('send-mail', 'json')
->initContext()
;
}
The request url should contain a "format=json" appended to the query string. This will execute the action and send the response in json format. The default behaviour of JSON context is to extract all the public properties of the view and encode them as JSON. Further details can be found here http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
I found a "probably not the prettiest" working solution, it is to indeed use ajax as mentioned in the previous zendcast for validation to stop the real validation (preventdefault), process the data return the result and if everything's ok restart it.

form with no form_id(Drupal 6.x)

I have this form placed inside a block and it is assigned to right region of my site. Form is displayed just fine. But the submit button doesn't work as intended - to call submit function. So, I did some debugging and found an anomaly that there is no essential data - such as form_id and tokens - drupal normally injected to every form. As I can't figure out the root cause of this, I'm here for pointers of friends from here. Here's an excerpt of my code -
function mymodule_block($op = 'list', $delta = '', $edit = array()) {
switch ($op) {
case 'list':
$blocks['quick_search'] = array(
'info' => t('Quick Search'),
);
return $blocks;
case 'view':
switch ($delta) {
case 'quick_search':
$block['subject'] = t('Quick Search');
$block['content'] = drupal_get_form("block_quick_search");
break;
}
return $block;
}
}
function block_quick_search(&$form_state){
$form = array();
.
.
.
$form['quick_search_submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#submit' => array('mymodule_quick_search'),
);
return $form;
}
function mymodule_quick_search($form, &$form_state){
drupal_goto($base_path,"..............");
}
Thanks in advance
there is no essential data - such as form_id and tokens
This is indeed the reason why form submissions are not processed correctly. Check whether drupal_prepare_form is called on your form and whether it adds those items correctly. It is called by drupal_get_form if he form is not posted (and thusly not retrieved from the cache).
If $form['#token'] and $form['form_id'] are added correctly, I suspect something is wrong with translating the form to HTML. Do you use any custom theming for the form?
Try to pass your submit handler to the main form and not on the item submit like that:
$form['#submit'][] = 'mymodule_quick_search';
it should work.

Arbitrary Form Processing with Drupal

I am writing a module for my organization to cache XML feeds to static files to an arbitrary place on our webserver. I am new at Drupal development, and would like to know if I am approaching this the right way.
Basically I:
Expose a url via the menu hook, where a user can enter in a an output directory on the webserver and press the "dump" button and then have PHP go to drupal and get the feed xml. I don't need help with that functionality, because I actually have a prototype working in Python (outside of Drupal)..
Provide a callback for the form where I can do my logic, using the form parameters.
Here's the menu hook:
function ncbi_cache_files_menu() {
$items = array();
$items['admin/content/ncbi_cache_files'] = array(
'title' => 'NCBI Cache File Module',
'description' => 'Cache Guide static content to files',
'page callback' => 'drupal_get_form',
'page arguments' => array( 'ncbi_cache_files_show_submit'),
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
I generate the form in:
function ncbi_cache_files_show_submit() {
$DEFAULT_OUT = 'http://myorg/foo';
$form[ 'ncbi_cache_files' ] = array(
'#type' => 'textfield',
'#title' => t('Output Directory'),
'#description' => t('Where you want the static files to be dumped.
This should be a directory that www has write access to, and
should be accessible from the foo server'),
'#default_value' => t( $DEFAULT_OUT ),
'#size' => strlen( $DEFAULT_OUT ) + 5,
);
$form['dump'] = array(
'#type' => 'submit',
'#value' => 'Dump',
'#submit' => array( 'ncbi_cache_files_dump'),
);
return system_settings_form( $form );
}
Then the functionality is in the callback:
function ncbi_cache_files_dump( $p, $q) {
//dpm( get_defined_vars() );
$outdir = $p['ncbi_cache_files']['#post']['ncbi_cache_files'];
drupal_set_message('outdir: ' . $outdir );
}
The question: Is this a decent way of processing an arbitrary form in Drupal? I not really need to listen for any drupal hooks, because I am basically just doing some URL and file processing.
What are those arguments that I'm getting in the callback ($q)? That's the form array I guess, with the post values? Is this the best way to get the form parameters to work on?
Thanks for any advice.
You can process forms in two stages, validate and submit.
Validate is for when you want to validate some user provided and raise form errors if some user input was invalid (like an invalid url or email address)
Submit which is the one you use is called if a form passes all of its validations, so at that point if you made a proper validation you will know that the data supplied by the user is okay.
Your submit function should look like this:
function ncbi_cache_files_dump(&$form, &$form_state) {
// $form: an array containing the form data
// $form_state: data about the form, like the data inputted in the form etc.
// code...
}
I think you need 2 separate forms here:
for setting the directory (the one you have now);
for making a dump (another form that would use the configured path).
Also it seems logical to publish the previously saved path as the default value in the settings form (instead of a hard-coded path).
And in general you should check the form input data from the second parameter of the submit callback:
function ncbi_cache_files_dump(&$form, &$form_state) {
$outdir = $form_state['values']['ncbi_cache_files'];
// ...
}