Semantic plugin
Integrate with the Semantic UI framework
Usage
The following piece of code is the starting point to validate the form made in Semantic UI:
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" />
<link rel="stylesheet" href="/vendors/@form-validation/umd/styles/index.min.css" />
</head>
<body>
<form id="demoForm" method="POST">...</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.min.js"></script>
<script src="/vendors/@form-validation/umd/bundle/popular.min.js"></script>
<script src="/vendors/@form-validation/umd/plugin-semantic/index.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(e) {
FormValidation.formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
semantic: new FormValidation.plugins.Semantic(),
...
},
}
);
});
</script>
</body>
</html>
The sample code above assumes that the FormValidation files are placed inside the vendors
directory. You might need to change the path depending on where you place them on the server.
The next sections list out some examples of various forms made with Semantic UI.
Using the npm packages
If you are using a bundler such as
Webpack,
Rollup,
Parcel or
Vite, etc., to bundle
your application, then it's recommended to use the FormValidation NPM packages.
$ npm install @form-validation/bundle
$ npm install @form-validation/plugin-semantic
- Import and use the Semantic plugin:
import { formValidation } from '@form-validation/bundle/popular';
import { Semantic } from '@form-validation/plugin-semantic';
formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
semantic: new Semantic({
...
}),
...
},
}
);
Horizontal form
Stacked form
In order to add the correct class for error message and the field element when it is a valid or invalid, we need to specify the CSS selector of the field container.
By default, the Semantic plugin will look for the .fields
element. But in the stacked form, evey field will be placed inside a .field
container.
We need to use the rowSelector
option to help the plugin determine the field containers as following:
semantic: new FormValidation.plugins.Semantic({
rowSelector: function(field, ele) {
return '.field';
}
}),
Multiple fields on the same row
Due to the same reason mentioned in the Stacked form section above, the rowSelector
option will be used to determine the field containers.
In the following example, the firstName
and lastName
fields are placed inside .five.field
containers. Meanwhile, the city
, state
and zipcode
fields can be found inside the .four.field
containers.
semantic: new FormValidation.plugins.Semantic({
rowSelector: function(field, ele) {
switch (field) {
case 'firstName':
case 'lastName':
return '.five.field';
case 'city':
case 'state':
case 'zipcode':
return '.four.field';
default:
return '.fields';
}
},
}),
Multiple fields on the same row
Changelog
- Supported Semantic UI v2.3.3