Hi to all,
I'm creating a "registration" plugin, but I have some problems with the form validation.
Here are some pieces of my code:
This is server side plugin (there is also a custom template, but it renders form in the correct way so i will not attach it)
<?php
define('MYSQL_DATE_FORMAT', 'Y-m-d');
define('MYSQL_DATETIME_FORMAT', 'Y-m-d H:i:s');
class registration extends rcube_plugin
{
public $task = 'login';
function init()
{
$rcmail = rcmail::get_instance();
$current_task = $rcmail->task;
$current_action = $rcmail->action;
$this->add_texts('localization/');
$this->include_stylesheet('css/registration.css');
$this->include_script('scripts/script.js');
if($current_task == 'login' && $current_action == 'plugin.add_registration_handler')
{
$this->add_hook('startup', array($this, 'add_registration_handler'));
$this->register_action('plugin.add_registration_handler', array($this, 'add_registration_handler'));
}
else if($current_task == 'login' && $current_action == 'plugin.do_registration_handler')
{
$this->add_hook('startup', array($this, 'do_registration_handler'));
$this->register_action('plugin.do_registration_handler', array($this, 'do_registration_handler'));
}
else if($current_task == 'login' && $current_action == 'plugin.check_email_exists')
{
$this->add_hook('startup', array($this, 'check_email_exists'));
$this->register_action('plugin.check_email_exists', array($this, 'check_email_exists'));
}
}
function add_registration_handler()
{
$this->register_handler('plugin.registrationform', array($this, 'register_form'));
$rcmail = rcmail::get_instance();
$rcmail->output->set_pagetitle($this->gettext('registration'));
$rcmail->output->send('registration.registration_form');
}
function register_form()
{
$input_domain = new html_hiddenfield(array('name' => 'domain', 'id' => 'domain', 'value' => 'cerca.glab.ch'));
....
$input_new_email_label = html::label(array('for' => 'new_email', ), $this->gettext('new_email') . ':');
$input_new_email_field = new html_inputfield(array('name' => 'new_email', 'id' => 'new_email', 'required' => 'required'));
....
$out = '';
$out .= '<table>';
...
$out .= '<tr>';
$out .= '<td class="title">';
$out .= $input_new_email_label;
$out .= '</td>';
$out .= '<td class="input">';
$out .= $input_new_email_field->show();
$out .= '<span class="atcerca">@cerca.it</span>';
$out .= '</td>';
$out .= '</tr>';
...
$out .= $input_domain->show();
return $out;
}
function do_registration_handler()
{
...
$new_email = array_key_exists('new_email',$_POST) ? $_POST['new_email'] : '';
...
$domain = array_key_exists('domain',$_POST) ? $_POST['domain'] : '';
$rcmail = rcmail::get_instance();
$db = $rcmail->db;
...
if($checks_ok)
{
...
if($error)
{
$rcmail->output->show_message('Error1','warning'); <--- THIS DOES NOT WORK
$rcmail->output->redirect(array('_task' => 'login', '_action' => 'plugin.add_registration_handler'));
}
else
{
...
}
}
else
{
$rcmail->output->show_message('registration.contact_system_administrator','warning');
}
}
function format_date_to_mysql_date($date_string, $format)
{
$datetime_object = null;
$datetime_object = DateTime::createFromFormat($format, $date_string);
return $datetime_object->format(MYSQL_DATE_FORMAT);
}
function get_mysql_current_datetime()
{
return date(MYSQL_DATETIME_FORMAT);
}
function check_email_exists()
{
... various checks ...
if($email_exists)
{
$check_result = array
(
'result' => 'fail',
'message' => 'Error, email exists',
);
}
else
{
$check_result = array
(
'result' => 'success',
'message' => '',
);
}
$rcmail->output->command('plugin.check_email_exists_callback', $check_result);
}
}
And this is the javascript code:
function checkEmailExists()
{
rcmail.removeEventListener('plugin.check_email_exists_callback', checkEmailExistsCallback);
rcmail.addEventListener('plugin.check_email_exists_callback', checkEmailExistsCallback);
jQuery('#new_email').keyup
(
function( event )
{
domain = jQuery('#domain').val();
email = jQuery('#new_email').val();
data = { new_email: email , domain: domain};
alert(domain);
alert(email);
rcmail.http_post('plugin.check_email_exists', data);
}
);
}
function checkEmailExistsCallback(data)
{
alert('callback');
var result = data.result;
var message = data.message;
alert(result);
alert(message);
if(result == 'fail')
{
jQuery('#new_email').addClass('error');
}
else
{
jQuery('#new_email').removeClass('error');
}
}
jQuery(document).ready(checkEmailExists);
The problems are essentially two:
1)
While I'm writing the "new_email" i see the POST request with two parameters (domain, new_email) to the server, and i correctly see the callback "alerts".
But after the callback ends the page is redirected to the login page with the "_err=session" error parameter.
This behaviour is very strange for me...I was expecting that the class of the new_email input field would change.
Why is this happening?
2)
After an error checked by do_registration_handler i try to set an error message (the line with "THIS DOES NOT WORK")
But no error is displayed on the page.
I hope my question is clear (or lightly understandable).
If not please ask me what is not clear, i'll try to explain.
Thanks in advance for your help.
Update:
I'm still struggling with this problem, but I found something that could help:
when I do a request like http://mysite.net/?_task=login&_action=plugin.add_registration_handler
all is working fine
when I do a request (ajax using rcmail.http_post) like http://mysite.net/?_task=login&_action=plugin.check_email_exists
I always get a "session error" and RC redirects me to the login page.
As you can see the definition of both actions are the same.
Hope you can help me...
Thanks a lot
I figured it out!
i missed the $rcmail->output->send() after the $rcmail->output->command ...
Great to see you figured it out! Can you mark this post as SOLVED?