Skip to content

Trouble with validity using default values. #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
isaac-livingston opened this issue Dec 27, 2015 · 2 comments
Open

Trouble with validity using default values. #121

isaac-livingston opened this issue Dec 27, 2015 · 2 comments

Comments

@isaac-livingston
Copy link

Having a bit of trouble when the following conditions are present:

  1. Have a pre-filled object; for example, if one were to make an 'Edit' form
  2. Push the default object into the select list.
  3. Set the ng-model field to the ID of the default object.
<!-- the mark-up -->
<selectize ng-model="base.serialnumber[data.linkfield]" 
                   name="{{data.linkfield}}" 
                   config="inputConfig"  
                   options="cfg.sOpts"
                   ng-pattern="cfg.rx" 
                   ng-required="cfg.required"></selectize>
/*  the configuration */
scope.inputConfig = {
                create: true,
                valueField: attribs.valuefield,
                labelField: attribs.labelfield,
                searchField: attribs.searchfield,
                placeholder: attribs.placeholder,
                inputClass: 'form-control selectize-input',
                onInitialize: function (selectize) {
                    // receives the selectize object as an argument
                    selobj = selectize;
                },
                maxItems: 1,
                load: debounce(function (query, callback) {
                    if (!query.length) return callback();
                    if (query.length < attribs.minlen) return callback();
                    this.clearOptions();
                    scope.cfg.queryFunction(query);
                }, 400, true)
            };
/* a bit of json as an example */

{
    "Employee":
        {"Id":4,"Name":"EMPLOYEE NAME"},
    "Id":20088,
    "SerialNumber":"123456",
    "DateOfOrigination":"2015-12-12",
    "EmployeeId":4
}

At this point things look great. The options include the item that is required and the control shows the appropriate text.

The trouble sets in when adding ng-required='true'. Even though the control has been pre-populated, it will be in-valid until the user removes the pre-populated item and chooses the default option from the options list.

@isaac-livingston
Copy link
Author

Here is what I did to fix for now...

var setSelectizeValue = function() {
      validate();

        selectize.$control.toggleClass('ng-valid', modelCtrl.$valid);
        selectize.$control.toggleClass('ng-invalid', modelCtrl.$invalid);
        selectize.$control.toggleClass('ng-dirty', modelCtrl.$dirty);
        selectize.$control.toggleClass('ng-pristine', modelCtrl.$pristine);

        if (!angular.equals(selectize.items, scope.ngModel)) {
            selectize.setValue(scope.ngModel, true);
            /* added the following line */
            if (!!scope.ngModel) { modelCtrl.$setValidity('required', true); };
        }


      }

@arturgspb
Copy link

#116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants