angular-ui / ui-tinymce

AngularUI wrapper for TinyMCE
MIT License
488 stars 370 forks source link

maxlength/minlength attribute broke 2-way databinding #145

Open rolandocc opened 9 years ago

rolandocc commented 9 years ago

I had this code:

<textarea ui-tinymce="{trusted: true}" id="tinymceEmailCuerpo"
   aria-label="Cuerpo"
  maxlength="100"
   ng-model="plantillaCorreo.entidad.Cuerpo"></textarea>

This cause the model var "plantillaCorreo.entidad.Cuerpo" becomes undefined, also printing {{plantillaCorreo.entidad.Cuerpo}} displays nothing and of course the 2-way databinding stops working.

I was trying to figure it out the reason without success.

andresmatasuarez commented 9 years ago

+1. Had the same problem, ended up removing ng-maxlength directive and implementing a custom one that does exactly the same. Not cool.

deeg commented 9 years ago

Please update issue with a Plunker or Fiddle. I am happy to look into fixing it.

deeg commented 9 years ago

Adding Plunker for good measure.

It is still an issue with the latest release (v.0.0.10). I will continue looking into fixing it.

lommez commented 8 years ago

Is there a workaround for using ng-maxlength or maxlength?

andresmatasuarez commented 8 years ago

I am using the following directive as a workaround, without specifying the maxlength attribute. I know it's not perfect, I didn't have the time to delve deeper into Angular formatters and its edge cases, but I think it do the work.

Usage

<textarea ui-tinymce="..." plain-text-max-length="100" />

Directive. Basically, it performs a maxlength validation on the text with its HTML tags ROUGHLY stripped.

'use strict'

app = angular.module 'dashboard'

stripHtmlTags = v -> String(v).replace /<[^>]+>/gm, ''

app.directive 'plainTextMaxLength', ($filter) ->
  restrict: 'A'
  require: 'ngModel'

  link: (scope, element, attributes, ngModel) ->

    validPlainTextLength = (v) ->
      return true if !v
      stripHtmlTags(v).length <= maxLength

    maxLength = undefined

    scope.$watch attributes.plainTextMaxLength, (newValue, oldValue) ->
      if maxLength != newValue
        maxLength = newValue
        ngModel.$validate()

    ngModel.$validators['plainTextMaxLength'] = (modelValue, viewValue) ->
      if viewValue.$$unwrapTrustedValue
        validPlainTextLength viewValue.$$unwrapTrustedValue()
      else
        validPlainTextLength viewValue
rodneyjoyce commented 8 years ago

I can confirm that this bug is still an issue (Cannot use Min/Max lengths in angular with TinyMCe

sameerinfodb commented 8 years ago

@andresmatasuarez can you share the plunker link with your solution because have tried solution but plain text max length directive was getting strip out of markup.

andresmatasuarez commented 7 years ago

@sameerinfodb sorry for the late response, but here is the plunkr with my custom directive to make length validation work: http://plnkr.co/edit/oAZcHZAmCXYOTckJCPcs?p=preview

naXa777 commented 6 years ago

Here's a related discussion on Stack Overflow.