elclanrs / jq-idealforms-old

The ultimate framework for building and validating responsive HTML5 forms.
665 stars 95 forks source link

Server support #31

Closed nilskp closed 12 years ago

nilskp commented 12 years ago

I didn't see a way to invoke errors on external validation, such as server side validation. This would be nice.

elclanrs commented 12 years ago

What do you mean? Can you elaborate a bit more?

nilskp commented 12 years ago

I want to submit a form, and on an error response from server, be able to highlight the fields in error. It doesn't look like I can do that today. It seems I can only do custom regex validation and form errors only pop up once I've entered the field. That wouldn't work for server validated fields.

elclanrs commented 12 years ago

Ideal Forms is all about live validation or "on the spot" as I like to call it. You can do an AJAX request within a custom filter. If the filter returns false then it fails validation and it shows your error; if it returns true then it passes. The filter will run the request "on the spot" and inform you through the error message.

var myFilter = {

  regex: function( input, value ) {
    var isValid = false
    $.post(
      'script.php',
      { value: value },
      function( data ) {
        // make sure to receive JSON from PHP with json_encode
        isValid = $.parseJSON(data)
      }
    )
    return isValid
  },

  error: 'Something went wrong...'

}

// Add filter to Ideal Forms
$.extend( $.idealforms.filters, myFilter )

If what you want to do is double check the values from client side then when a value is not correct you can just reset the field and then focus the field to show the error.

$myform.reset(['foo'])
$('input[name=foo]').trigger('focus')