Unable to make Field mandatory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2022 08:12 AM
Hi all,
I for some reason unable to spot why I cannot get a field to act as mandatory using the following code found from another community question:
https://community.servicenow.com/community?id=community_question&sys_id=4909309adb9668103daa1ea668961991
When using the solution above, it doesn't allow the form to proceed regardless if the title box is completed or not.
HTML
<div class = "test">
<input type="text" name = "title" ng-model="c.data.title" required/>
<button class = "btn btn-success" ng-click="c.uiAction('submit')">Submit</button>
</div>
Client
function() {
var c = this;
c.uiAction = function(action) {
if(!c.state){
spUtil.addErrorMessage("Please fill the required fields.");
return;
}
}
c.data.action = action;
c.server.update().then(function() {
c.data.action = undefined;
c.data.title = "" ;
c.data.chgreq = "";
c.data.chgscen = "";
c.data.chgout = "";
})
}
- Labels:
-
Service Portal Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2022 08:37 AM
There is the "Mandatory" field on the sys_dictionary table. That will make a table field mandatory. But that is platform wide. Seems you may only want to do that in certain circumstances.
From your Community link, that deals with a service portal widget. The solution there does not look like what you post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2022 04:56 AM
Hi,
I've attempted to adapt the code, but I cannot seem to figure out where I'm going wrong or what I need to change. I believe it has something to do with the if(!c.state)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2022 01:11 PM
Again, it seems you are not looking that the "Accepted Solution and Top Rated Answer" on that Community post. I can only comment on the Table feature that allows you to make a field mandatory. Others may provide suggestions if you state where that code you posted is used/present.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 02:17 AM
I added the required add the end of the button:
<input type="text" name = "title" ng-model="c.data.title" required/>
Then in the client script I added this just after the uiaction function is triggered:
if(!c.state){
spUtil.addErrorMessage("Please fill the required fields.");
return;
The only alteration is I changed the "$scope.state" into "c.state" as I am not using $scope in this widget. After adding this piece it stops the widget from proceeding entirely where the text box is completed or not.