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

Attribute required or validate-if-empty without value or value="false" not handled correctly #1262

Open
Species8372 opened this issue Aug 10, 2018 · 1 comment

Comments

@Species8372
Copy link

Hello,

we are using Parsley for a JSF-UI, so we do not have the possibility to conditionally add or remove pass-through-attributes in the UI-Tags. It is only possible to set the parsley-attributes to true or false (or nothing). However parsley treats an available "data-parsley-required" or "data-parsley-validate-if-empty" always as true, not considering its value (if there is a value).

<p:selectOneMenu  required="#{reqJSF}" requiredMessage="#{cc.attrs.requiredMessage}" 
                  pt:data-parsley-required="#{reqParsley}"  
				  pt:data-parsley-validate-if-empty="#{validateParsley}"  >  
                            //...
</p:selectOneMenu>

We fixed this by slightly changing the following two methods.


    _isRequired: function _isRequired() {
      if ('undefined' === typeof this.constraintsByName.required || !this.constraintsByName.required) return false;

      return false !== this.constraintsByName.required.requirements;
    },

	
	 needsValidation: function needsValidation(value) {
      if ('undefined' === typeof value) value = this.getValue();

      // If a field is empty and not required, it is valid
      // Except if `data-parsley-validate-if-empty` explicitely added, useful for some custom validators
      if (!value.length && !this._isRequired() && ('undefined' === typeof this.options.validateIfEmpty || !this.options.validateIfEmpty)) return false;

      return true;
    },

The change now considers a value for the two attributes if a value is given.
It would be nice considering this changes for a future release.

@marcandre
Copy link
Collaborator

Thanks for the bug report. I understand that it can be surprising that foo="false" is interpreted the same as foo="true".

The problem is that it's the spec for boolean HTML attributes, e.g. required. So changing this is a bit tricky...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants