Skip to content
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

Required attribute #324

Open
chrisveness opened this issue May 12, 2016 · 14 comments
Open

Required attribute #324

chrisveness opened this issue May 12, 2016 · 14 comments

Comments

@chrisveness
Copy link

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.

@WesCossick
Copy link
Member

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.

@kraiz
Copy link

kraiz commented Jul 25, 2016

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
});

@WesCossick
Copy link
Member

@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.

@MatthewBooth
Copy link

MatthewBooth commented Aug 18, 2016

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.

@WesCossick
Copy link
Member

@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.

@chrisveness
Copy link
Author

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.

@WesCossick
Copy link
Member

@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.

@landart
Copy link

landart commented Oct 20, 2016

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!

@chrisveness
Copy link
Author

chrisveness commented Oct 20, 2016

@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.

@WesCossick
Copy link
Member

I'll look into applying the HTML attributes to the visible textarea.

@WesCossick WesCossick reopened this Jan 12, 2017
@vanderkilu
Copy link

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

@geek-merlin
Copy link

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');

@I-Valchev
Copy link

+1

@jacklinwood
Copy link

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?

dvejmz added a commit to dvejmz/gitea that referenced this issue Oct 24, 2021
Implement a JS, client-side validation workaround for a bug in the upstream
editor library SimpleMDE which breaks HTML5 client-side validation when
a wiki page is submitted.

This allows native, client-side errors to appear if
the text editor contents are empty.

See upstream bugfix report: sparksuite/simplemde-markdown-editor#324

Signed-off-by: David Jimenez <[email protected]>
wxiaoguang pushed a commit to go-gitea/gitea that referenced this issue Oct 27, 2021
* fix: show client-side error if wiki page is empty

Implement a JS, client-side validation workaround for a bug in the upstream
editor library SimpleMDE which breaks HTML5 client-side validation when
a wiki page is submitted.

This allows native, client-side errors to appear if
the text editor contents are empty.

See upstream bugfix report: sparksuite/simplemde-markdown-editor#324

Signed-off-by: David Jimenez <[email protected]>
Co-authored-by: Lauris BH <[email protected]>
Chianina pushed a commit to Chianina/gitea that referenced this issue Mar 28, 2022
* fix: show client-side error if wiki page is empty

Implement a JS, client-side validation workaround for a bug in the upstream
editor library SimpleMDE which breaks HTML5 client-side validation when
a wiki page is submitted.

This allows native, client-side errors to appear if
the text editor contents are empty.

See upstream bugfix report: sparksuite/simplemde-markdown-editor#324

Signed-off-by: David Jimenez <[email protected]>
Co-authored-by: Lauris BH <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

9 participants