Unable to make Field mandatory

Dunny774
Kilo Explorer

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 = "";
            
      
        })
    }

4 REPLIES 4

Bert_c1
Kilo Patron

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.

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) 

Bert_c1
Kilo Patron

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.

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.