Skip to content

arillo/silverstripe-jqueryformvalidator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JQueryValidation

Adds jquery.validation functionality to silverstripe 3.0+ forms. Visit http://jqueryvalidation.org/ for more information.

Usage

To make it work, make sure your page has jquery included. For sake of backward compartibilty for old browsers you also might load json2.js. Both files can be found in modulefolder/javascript/libs.

Start up validation on a form like this:

$form = new Form(
	$this,
	'Form',
	$fields,
	new FieldList(
		new FormAction(
			'FormHandler',
			'Submit'
		)
	),
	RequiredFields::create(....)
);

JQueryValidation::create($form, $config)

Expects a form and an optional config array, which overrides values from the base config. Take a look into JQueryValidation::$base_config for available key / value pairs.

JQueryValidation::create(
	$form,
	array(
		'validator' => array(
			'errorElement' => 'div'
		)
		...
		..
		.
	)
);

JQueryValidation->generate()

For automated creation use:

// This will inspect all form fields and add their validation methods.
// It also will add required checks provided by the form's RequiredFields.
JQueryValidation::create($form)->generate();

// It is also possible to provide custom error messages and behaviour through passing a config array like this:
JQueryValidation::create($form)->generate(array(
	'messages' => array(
		'CheckboxField_DATA' => array(
			'required' => 'Custom message'
		)
	)
));
// Expected hooks are messages, rules, groups.

JQueryValidation->custom()

If you want to provide your own validation file you can use this:

JQueryValidation::create($form)->custom('path/to/your.js');

// It is also possible to provide information about which additional js files should be loaded, like this:
JQueryValidation::create($form)->custom(
	'path/to/your.js',
	array(
		'additionalMethods',	// load additional-methods.min.js
		'metadata',				// load jquery.metadata.js
		'moment',				// load moment.min.js
		'date'					// load date.js
	)
);

Supported form fields

  • CheckboxField
  • ConfirmedPasswordField
  • DropdownField
  • DateField
  • DatetimeField
  • EmailField
  • PasswordField
  • OptionsetField
  • NumericField
  • UploadField
  • TimeField
  • TextareaField
  • TextField

Validation types jquery.validator ships

Standard:

  • required – Makes the element required.
  • remote – Requests a resource to check the element for validity.
  • minlength – Makes the element require a given minimum length.
  • maxlength – Makes the element require a given maxmimum length.
  • rangelength – Makes the element require a given value range.
  • min – Makes the element require a given minimum.
  • max – Makes the element require a given maximum.
  • range – Makes the element require a given value range.
  • email – Makes the element require a valid email
  • url – Makes the element require a valid url
  • date – Makes the element require a date.
  • dateISO – Makes the element require an ISO date.
  • number – Makes the element require a decimal number.
  • digits – Makes the element require digits only.
  • creditcard – Makes the element require a credit card number.
  • equalTo – Requires the element to be the same as another one

Additional methods:

  • accept – Makes a file upload accept only specified mime-types.
  • extension – Makes the element require a certain file extension.
  • phoneUS – Validate for valid US phone number.

See http://jqueryvalidation.org/documentation/ for more info.

Some of this methods are automatically invoked if JQueryValidation->generate() is used. Extra functionality can be added by adding them into $custom parameter. The following example creates a field which validates for an url:

// adding a simple textfield to the field list
$fields->push(
	TextField::create(
		'URL',
		'Your website'
	)
);
...
..
.
// later, when starting the validation you can add url validtition to this field like this:
JQueryValidation::create($form)
	->generate(
		array(
			'messages' => array(
				'URL' => array(
					'url' => 'Please enter a valid url.' // add error message
				)
			),
			'rules' => array(
				'URL' => array(
					'url' => true // add url validation
				)
			)
		)
);

About

jquery validator for Silverstripe CMS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published