Adding custom form validation to a FLEXIcontent Field

  • Published by
    George Papadakis
  • Last modified
    22 July 2015
  • Up to date
    Yes
  • Profile concerned
    Developer
  • Concerns
    Plugins
  • Since Version
    2.0
  • Voting
    Average rating
    1 vote
    • 1
    • 2
    • 3
    • 4
    • 5
  • Favourites
    313 Adding custom form validation to a FLEXIcontent Field /documentation/tutorials-english/71-fields-usages-tips-joomla-plugin/313-adding-custom-form-validation-to-a-flexicontent-field.html


Developers may want to add a custom validation to their custom developed field.

This should be possible without hacking our validation.js script file that contains the validation form field handlers.

1. Adding a custom class name to a form field (input or select form fields) e.g. validate-myvalidatename

2. Goto component parameters (or in content type parameters if you want to have different validation per content type) and inside ITEM FORM Tab (in component parameters) or inside ITEM FORM Slider (in content type parameters) , find parameter for custom javascript and add:

jQuery(document).ready(function() {
	document.formvalidator.setHandler('myvalidatename',
		function (value) {
			// do so tests
			some_true_false_result = ...;
			return some_true_false_result;
		}
	);
});


Or you can enter same code inside the OnDisplayField(...) function of your custom field by using this PHP code:

static $js_validation_added = false;
if ( !$js_validation_added ) {
	$js = "
	jQuery(document).ready(function() {
		document.formvalidator.setHandler('myvalidatename',
			function (value) {
				// do so tests
				some_true_false_result = ...;
				return some_true_false_result;
			}
		);
	});
	";
	JFactory::getDocument()->addScriptDeclaration($js);
	$js_validation_added = true;
}