简体   繁体   中英

How to remove special characters from an x-editable field before sending data to the server?

I want to strip some special characters from an xeditable text input, and write the sanitized string to the local model, before the model's (stripped) text field is sent to the server.

According to the docs ,

When you submit editable form it performs following steps:

- call child's onbeforesave
- call form's onbeforesave
- write data to local model (e.g. $scope.user)
- call form's onaftersave
- call child's onaftersave

So, for my editable text input, I have a method that strips the special characters, and updates the value on the local model. I've verified that this works; after running onbeforesave, the model has the correctly stripped value. I also know that the returned result of onbeforesave behaves like this:

- string: submit will be cancelled, form will stay opened, string will be shown as error message
- not string: submit will be continued

And I am returning undefined in the onbeforesave function.

Then, in the form's onaftersave method, I send the presumably updated data to the server.

The problem is: the update inside child onbeforesave is getting lost. Neither the local model, nor the server have the updated value.

When I moved the sanitization method to the child's onaftersave, the local model was getting updated successfully, but, as you'd expect, the server didn't get that updated info because the form's onaftersave is called before the child's onaftersave.

If I had to guess, I'd say that the problem is that, even though I'm updating the model inside onbeforesave, that value is being overwritten by the $data (ie the originally unsanitized data from the input text), after my onbeforesave function runs.

So, it seems my only option here is to further modify my form's onaftersave so it strips special characters from my text field, before sending the sanitized, updated data to the server.

Is that accurate? Is there a more elegant way of doing this other than bloating the form's onaftersave function?

I'm not sure if this is the most elegant way, but my working solution is to pass the parameter "this" to my onbeforesave function, where "this" is the scope of one of my child text inputs, which includes the $data attribute. By modifying that $data attribute within onbeforesave, the sanitized value is being passed onto the local model successfully, and then on to the server.

Your approach is most likely wrong.

  1. Shouldn't ever have to strip characters. Why would you? What did that unicode number do to you to strip it out? Whatever problem you're facing has probably much deeper roots.
  2. If you still want to remove characters (hate them so much), strip unwanted characters on the server, not on the client.

Always treat the client with suspicion. Never trust what the client sends you. Do whatever validation/sanitisation work you need to do on the server, never on the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM