simplemde-markdown-editor
simplemde-markdown-editor copied to clipboard
Required attribute
If I put a 'required' attribute on the textarea, I get an error
An invalid form control with name='MyID' is not focusable."
when I submit the form.
SimpleMDE hides the textarea, so that would be as expected. Instead of a required
attribute, try implementing some form of JS validation that checks to see if SimpleMDE has been filled out.
I had this problem within Chrome too. It seems to prevent SimpleMDE syncing the content to the hidden textarea before the submit event happens. Works fine with the forceSync
option:
var simplemde = new SimpleMDE({
forceSync: true
});
@kraiz This won't work fully either, because if the content is empty, it'll result in the same error shown above. The problem is that the textarea is hidden. A JS approach is necessary.
This still seems like a bit of an oversight to be honest. Instead of requiring users to add extra JS to validate a form, something should be done within the library to alleviate this.
Especially if using a form factory like Symfony or Laravel, where you want this all to be done in code, in one place.
@MatthewBooth there are hundreds of different ways to validate an input and display an error, whether before, during, or after submission. SimpleMDE's job is not to determine how to do that for the developer.
Would it not be possible for SimpleMDE to remove any 'required' attribute from the text area and apply it to the markdown control? It's a while since I looked at this, perhaps it wouldn't work, but it seems like it could be workable.
@chrisveness SimpleMDE would not know what to do if it was left blank, since an error message can be displayed in so many ways. It's up to the developer to not rely on the required
attribute and to instead build their own JS based implementation that matches the rest of their form's validation and error display.
There is a very simple workaround for this problem.
What I'm using mostly is HTML5 validation, so I rely on the browser to know how to display the validation message - and then, if I wanted to, but not necessarily, I could add some JS on top of it, but that's another question.
So, the thing is, SimpleMDE creates its own textarea to keep the widget's value, and hides the original textarea. By simple removing the required attribute from the latter, and adding it to the former (which is displayed, but behind the actual editor box), the HTML5 validation works just fine.
$('#original-text-area').attr('required', false); $('.CodeMirror textarea').attr('required', true);
It's not pretty, and it's just basic browser styles in there, but it works. Maybe you guys should consider adding this behavior by default to SimpleMDE, then let the developer choose what CSS rules they want in order to display the error messages in a more specific way. I hope this helps!
@landart, yes, that's the sort of thing I had in mind.
It could perhaps be generalised along the lines of (roughly)
const originalElement = document.querySelect('#original-text-area');
const mirrorElement = document.querySelect('.CodeMirror textarea');
const validationAttributes = [ 'required', 'pattern', 'min', 'max' ]; // etc
for (const attr of originalElement.attributes) {
if (validationAttributes.includes(attr.name) {
mirrorElement.attr.name = attr.value;
originalElement.attr.name = undefined;
}
}
@WesCossick I think this could make the library much more valuable: there's so many reasons to want to use native browser validation functions, not to try to reinvent the wheel with homegrown JS validation...
Edit: it's a while since I was looking at this, I'm not sure what validation other than required
might be relevant.
I'll look into applying the HTML attributes to the visible textarea.
was having the same issue but i found a way around it, i just added the "novalidate" attribute to the form element and did my custom validation from the framework i was using and it worked
We ran into this in the drupal wrapper module too.
I'd say this is a major issue as ordinary users and integrators end up in a situation where they don not have any clue what happens, it's just "the form does not submit". (So is our downstream issue triaged.)
I looked a bit into this.
- (Once this library is a webcomponent this is trivial to fix. But that is a different issue...)
- Trying a hack to set textarea visibility=hidden does not work out, as the JS validation message then is hidden too.
- As of moving the validation attributes from the original textarea to the codemirror textarea as @chrisveness suggested: Good plan, alas, there is no such thing as a codemirror textarea.
- Adding 'novalidate' to the textarea as @vanderkilu suggested did not do the trick for us, as we have some JS validation in place.
- So we ended up removing 'required' and 'pattern' (min/max are not relevant) attributes from the textarea, and our framework then falls back to server-side validation.
So +1 for doing that (removing textarea 'required' and 'pattern') here in the library.
textarea.removeAttribute('required');
textarea.removeAttribute('pattern');
+1
I'll look into applying the HTML attributes to the visible textarea.
Hi Wes. Great editor first of all!
Did you ever get a chance to do this?