1

I'm referring to Hero guide passing the object to empty all the fields in the model like this.

this.form.reset({
  "firstName": "",
  "lastName": "bzz",
  "reporter": "" 
});

The problem is that it only sets the fields that are non-empty, i.e. bzz in the sample above. I've tried with setValue(...) but that gave the same effect. Goolearching the issue gave rather nothing other than references to the Hero examples.

I also tried to use the following. Same effect as well.

this.form.get("firstName").patchValue("");

What am I missing?

4
  • 1
    can you explain more your problem ? Commented Nov 3, 2017 at 12:11
  • Ahem... Not sure. I'm sending in the object and it resets the form setting the fields' values as specified but only if the value specified isn't an empty string. Then it doesn't set jack and the old value that's supposed to be cleaned up, stays. Commented Nov 3, 2017 at 12:14
  • 2
    Can't reproduce: plnkr.co/edit/N9vjdZMdFuj5Td8M5bLG?p=preview. Post a complete minimal example reproducing the problem, as I just did. Commented Nov 3, 2017 at 12:22
  • @JBNizet I can't neither. I'm using custom validators with custom components. It might be something there. I'll keep digging. Or simply reload the page creating the impression that I've solved the problem, hehe. Commented Nov 3, 2017 at 15:23

2 Answers 2

2

Don't pass an object, and It will set all the form controls to null:

this.form.reset();
Sign up to request clarification or add additional context in comments.

6 Comments

Two problems. One - the default values need to be set one way or another. Two - only going this.form.reset() produces a bunch of validation errors (I have custom components with custom validations). Also, awesome nick. A bit offensive to the weak-herted but to me it's beautiful. I'd love to see the recruited who asks for your SO profile, hehe. :)
I don't understand your problem then. You want to clear the form (like clicking on a reset button or after submitting), don't you?
Yes, that's what I want to do. The problem is that due to the custom components I've designed, there's something wrong and I get errors. I didn't realize until now.
@DonkeyBanana, it's probably because your custom validators are expecting a certain value and since the reset() method sets their value to null, it throws an error. For example let's say your validator does some sort of validation on the length of a string: control.value.length. When it gets set to null, it'll throw an error because you can't access properties or call functions on null or undefined. Simply add this at the beginning of your validator function to make sure it actually has a value: if(!control.value) return null;
I agree. On the other hand, setting an init object with actual values in the reset(blob) method seemed to generate errors too. I've tried setting blob to so many things but there are form arrays and groups and all kinds of junk, so it got complicated rather quickly. I gave up and went for a reload of the page. Ugly but effective, hehe. I'll give it another whack later, if I find time, though.
|
0
this.form.reset({
  firstName: {value: '1', disabled: true}
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.