ENHANCEMENT clientside validation messages sourced from customisable serverside code. Several small bugfixes also

This commit is contained in:
Shea Dawson 2013-02-20 17:15:28 +11:00 committed by Will Rossiter
parent b1bc0c713b
commit ca638e367a
3 changed files with 36 additions and 19 deletions

View File

@ -242,13 +242,25 @@ class CommentingController extends Controller {
$member = Member::currentUser();
$fields = new FieldList(
new TextField("Name", _t('CommentInterface.YOURNAME', 'Your name')),
new EmailField("Email", _t('CommentingController.EMAILADDRESS', "Your email address (will not be published)")),
new TextField("URL", _t('CommentingController.WEBSITEURL', "Your website URL")),
new TextareaField("Comment", _t('CommentingController.COMMENTS', "Comments")),
new HiddenField("ParentID"),
new HiddenField("ReturnURL"),
new HiddenField("BaseClass")
TextField::create("Name", _t('CommentInterface.YOURNAME', 'Your name'))
->setCustomValidationMessage(_t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name'))
->setAttribute('data-message-required', _t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name')),
EmailField::create("Email", _t('CommentingController.EMAILADDRESS', "Your email address (will not be published)"))
->setCustomValidationMessage(_t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'))
->setAttribute('data-message-required', _t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'))
->setAttribute('data-message-email', _t('CommentInterface.EMAILADDRESS_MESSAGE_EMAIL', 'Please enter a valid email address')),
TextField::create("URL", _t('CommentingController.WEBSITEURL', "Your website URL"))
->setAttribute('data-message-url', _t('CommentInterface.COMMENT_MESSAGE_URL', 'Please enter a valid URL')),
TextareaField::create("Comment", _t('CommentingController.COMMENTS', "Comments"))
->setCustomValidationMessage(_t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'))
->setAttribute('data-message-required', _t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment')),
HiddenField::create("ParentID"),
HiddenField::create("ReturnURL"),
HiddenField::create("BaseClass")
);
// save actions

View File

@ -97,7 +97,8 @@ class CommentsExtension extends DataExtension {
* @see docs/en/Extending
*/
public function CommentsForm() {
if (Commenting::has_commenting($this->owner->ClassName) && Commenting::get_config_value($this->owner->ClassName, 'use_ajax_commenting')) {
if (Commenting::has_commenting($this->ownerBaseClass) && Commenting::get_config_value($this->ownerBaseClass, 'use_ajax_commenting')) {
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/lib/jquery.form.js');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/jquery.validate.pack.js');
Requirements::javascript('comments/javascript/CommentsInterface.js');

View File

@ -22,6 +22,12 @@
}, 200);
},
showErrors: function(errorMap, errorList) {
this.defaultShowErrors();
// hack to add the extra classes we need to the validation message elements
form.find('span.error').addClass('message required');
},
errorElement: "span",
errorClass: "error",
@ -42,17 +48,17 @@
},
messages: {
Name : {
required : 'Plaese enter your name'
required : form.find('[name="Name"]').data('message-required')
},
Email : {
required : 'Plaese enter your email address',
email : 'Plaese enter a valid email address'
required : form.find('[name="Email"]').data('message-required'),
email : form.find('[name="Email"]').data('message-email')
},
Comment: {
required : 'Plaese enter your comment'
required : form.find('[name="Comment"]').data('message-required')
},
URL: {
url : 'Please enter a valid URL'
url : form.find('[name="Comment"]').data('message-url')
}
}
});
@ -64,12 +70,11 @@
*/
form.submit(function (e) {
// trigger validation, if there are errors add the error classes to the elements
// trigger validation
if(!form.validate().valid()){
form.find('span.error').addClass('message required');
return false;
}
// submit the form
$(this).ajaxSubmit(function(response) {
noCommentsYet.hide();
@ -89,10 +94,9 @@
}
commentsList.prepend(newComment.fadeIn());
$(this).resetForm();
});
$(this).resetForm();
return false;
});