Validation errors when saving

拟墨画扇 提交于 2019-12-11 02:08:23

问题


When saving using SaveChanges() using Breeze, I get an error when doing a simple update if I have required fields.

E.g.

I have a table of users with a Name, and Email fields where the Email field is not required.

I can display the list of user names (but do not want to display the emails).

The display works, but save never gets called - I get a validation error in JavaScript:

Not saved due to validation error: 'email' is required

But surely this should be just generating a simple update on the field I have changed and should not be doing anything with email.

Oddly, I don't get an error if I just alter the 1st item in the list.

Any ideas?

The HTML looks like this:

<a href="#" data-bind="click: save">Save</a>
<ul data-bind="foreach: modules">
<li >
    <form>
        <input type="checkbox" data-bind="checked: IsRunning" />
        <input type="text" data-bind="value: ModuleName, css: { done: IsRunning }" />
    </form>
  </li>
 </ul>

and the viewmodel save part is below:

function saveChanges() {
    if (manager.hasChanges()) {
        manager.saveChanges()
            .then(saveSucceeded)
            .fail(saveFailed);
    } else {
        logger.info("Nothing to save");
    };
}
function saveSucceeded(saveResult) {
    logger.success("# of Modules saved = " + saveResult.entities.length);
    logger.log(saveResult);
}

function saveFailed(error) {
    var reason = error.message;
    var detail = error.detail;

    if (reason === "Validation error") {
        handleSaveValidationError(error);
        return;
    }
    if (detail && detail.ExceptionType.indexOf('OptimisticConcurrencyException') !== -1) {
        // Concurrency error 
        reason =
            "Another user, perhaps the server, may have deleted one or all of the todos.";
        manager.rejectChanges(); // DEMO ONLY: discard all pending changes
    }

    logger.error(error,
        "Failed to save changes. " + reason +
        " You may have to restart the app.");
};
function handleSaveValidationError(error) {
    var message = "Not saved due to validation error";
    try { // fish out the first error
        var firstErr = error.entitiesWithErrors[0].entityAspect.getValidationErrors()[0];
        message += ": " + firstErr.errorMessage;
    } catch (e) { /* eat it for now */ } 
    logger.error(message);
}

回答1:


hm... you say that email is not required but the metadata thinks that it is. Can you confirm this by looking at the metadata for this property? The code will look something like this.

var userType = myEntityManager.metadataStore.getEntityType("User");
var emailProp = userType.getProperty("email");
var isRequired = !emailProp.isNullable;

If the metadata thinks the field is required then it was probably set to this on the server. So take a look at your EF model and check that the [Required] attribute hasn't been added to this property. Also, check if this field is nonnullable is the database.

One other item to note, by default breeze will try to validate the entire entity on the server before any save, even those fields that were not touched, even though only the modified fields will actually get updated. This is by design.

However, you can suppress this behavior by setting the entityManager's 'validationOptions' like so:

var vo = new breeze.ValidationOptions({ 
    validateOnSave: false,
    validateOnQuery: false, // not needed here but shown for completeness
    validateOnAttach: true  // not needed here but shown for completeness
});
myEntityManager.setProperties({ validationOptions: vo });


来源:https://stackoverflow.com/questions/13990886/validation-errors-when-saving

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!