Skip to content

[Validation] Add ability to add a rule to a field after initializing validation #4267

Closed
@jemarlin

Description

We had need in our project to conditionally add form validation based on workflow steps. We added this method to form module.add:

validation: function ( $field, rule ) {

  var
    identifier = $field.attr( 'id' ) || $field.attr( 'name' ) || $field.attr( 'data-' + metadata.validate ),
    validator
  ;

  if ( !validation || !$field.length) {
      return false;
  }

  validator = validation[identifier];
  if ( !validator ) { // no rules exist for field, create an empty rule set
      validator = validation[identifier] = { 'identifier': identifier, 'rules': [] };
  }

  validator.rules.push( rule ); // add the rule

  return true;

  },

Which can be called like so $form.form( 'add validation', $field, { 'type': 'empty', 'prompt': 'This field must contain a value' } );

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions