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

[Form Validation][Enhancement] Please add 'validate field(field)' and 'reset field(field)' behaviors #5253

Closed
kmd1970 opened this issue Apr 7, 2017 · 1 comment
Milestone

Comments

@kmd1970
Copy link

kmd1970 commented Apr 7, 2017

We really need a behavior to trigger validation (check rules, show error or success) for individual form fields:

$('.ui.form').form('validate field', 'first-name')

Edit Looks like this (hidden?) behavior already exists, but has to be called like this:

<label>First Name</label>
<input placeholder="First Name" name="first-name" class="firstname" type="text">
$('.ui.form').form(
'validate field',
{
  identifier:'first-name',
  rules: [
      {
          type   : 'empty',
          prompt : 'Please enter your name'
      }
  ]
},'name',true);

this shortcut also works

$('.ui.form').form(
  'validate field',
  $('.ui.form').form('get validation', $('[name="first-name"]') ),
  'name',
  true
);

form.js line 853 (docs issue?)

field: function(field, fieldName, showErrors)

Also, need a behavior to reset a field that currently has an error and message showing.

$('.ui.form').form('reset field', 'first-name')
or
$('.ui.form').form('reset', 'first-name') (will only reset specified field in this case)

This can be done very easily:

form.js line 201

reset: function(field) {
         // pass in a field identifier or leave blank, then use filter()
         field = (field !== undefined) ? field : '';
         field = ('[name="' + field +'"]');
          $field.filter(field)
            .each(function () {
              var
                $field       = $(this),
                $element     = $field.parent(),
                $fieldGroup  = $field.closest($group),
                $prompt      = $fieldGroup.find(selector.prompt),
                defaultValue = $field.data(metadata.defaultValue),
                isCheckbox   = $element.is(selector.uiCheckbox),
                isDropdown   = $element.is(selector.uiDropdown),
                isErrored    = $fieldGroup.hasClass(className.error)
              ;

More
Two more behaviors add rule (adds a new rule to a field) and remove rule (removes an existing rule from a field, example empty. This can be used to dynamically toggle required fields)

@kmd1970 kmd1970 changed the title [Form][Enhancement] Please add 'validate field(field)' and 'reset field(field)' behaviors [Form Validation][Enhancement] Please add 'validate field(field)' and 'reset field(field)' behaviors Apr 7, 2017
@jlukic jlukic added this to the 2.2.11 milestone Jul 11, 2017
@jlukic
Copy link
Member

jlukic commented Jul 11, 2017

After looking through you can just call

$('.ui.form').form('validate field', 'someField'); 

This will validate the field with the existing ruleset.

I'm also adding in next version add rule add field remove field, remove rule

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

No branches or pull requests

3 participants