dpgaspar/Flask-AppBuilder

Prohibition on entering invalid characters

AleksSpace opened this issue · 3 comments

Good afternoon, I'm trying to make sure that forbidden characters don't get into the input field. But I don't understand how to do it. I either get an exception that I registered in the validator, or a message appears that forbidden characters have been entered, but the form is still sent.

Environment

Flask-Appbuilder version: 1.10.0
Python 3.6.14

my table:

class BaseColumn(AuditMixinNullable, ImportMixin):
    """Interface for column"""

    __tablename__ = None  # {connector_name}_column

    id = Column(Integer, primary_key=True)
    column_name = Column(String(255), nullable=False)
    verbose_name = Column(String(1024))
    is_active = Column(Boolean, default=True)
    type = Column(String(32))
    groupby = Column(Boolean, default=False)
    count_distinct = Column(Boolean, default=False)
    sum = Column(Boolean, default=False)
    avg = Column(Boolean, default=False)
    max = Column(Boolean, default=False)
    min = Column(Boolean, default=False)
    filterable = Column(Boolean, default=False)
    description = Column(Text)
    is_dttm = None

my validator:

@validates("column_name")
    def validate_column_name(self, key, name):
        match = re.search(r'\W', name)
        if match:
            raise ValueError(f"Запрещенный символ: {match[0]}")
        return name

my func edit:

@log_this
    @expose('/edit/<pk>', methods=['GET', 'POST'])
    @has_access
    def edit(self, pk):
        pk = self._deserialize_pk_if_composite(pk)
        widgets = self._edit(pk)

        if not widgets:
            return self.post_edit_redirect()
        else:
            return self.render_template(self.edit_template,
                                        title=self.edit_title,
                                        widgets=widgets,
                                        related_views=self._related_views)

Please help me to make sure that when entering forbidden characters, a message appears that forbidden characters are entered and changes are not saved.

Hi,

Take a look at: https://flask-appbuilder.readthedocs.io/en/latest/advanced.html#forms-custom-validation-rules

Or you can declare your own WTForms and set it on your model view (no need to override the edit method).
https://github.com/dpgaspar/Flask-AppBuilder/blob/master/flask_appbuilder/baseviews.py#L833

Good afternoon. That's what it says in the documentation:

class MyView(ModelView):
    datamodel = SQLAInterface(MyModel)
    validators_columns = {
        'my_field1':[EqualTo('my_field2', message=gettext('fields must match'))]
    }

This is what I do:

class BaseColumnModelView(ModelView):
    datamodel = SQLAInterface(BaseColumn)
    validators_columns = {
        'column_name':[EqualTo(r'\W', message=__('fields must match'))]
    }

I get an error:
sqlalchemy.orm.exc.UnmappedClassError: Class 'superset.connectors.base.models.BaseColumn' is not mapped
Tell me, please, what am I doing wrong?

EqualTo compares the values of two fields.
You have to use the "Regexp" validator from "wtforms.validators" !