To verify username and email availability, follow these steps:
Using https://github.com/posabsolute/jQuery-Validation-Engine
edit the validationsEngines-en.js file for AJAX calls, one for the email message will look like this:
"ajaxEmailAvailable": { "url": "/route_to_send_the_parameters", // you may want to pass extra data on the ajax call "alertTextOk": "* This email is available", "alertText": "* This email is already taken", "alertTextLoad": "* Validating, please wait" },
Make sure you configure the route.rb file according to the route you want to use, the default action with the call is the HTTP GET request.
Then configure the correct action in your controller to process the request (I included an assistant in the Application Controller so that the input value can be sanitized before the request in the database:
CONTROLLER, PLEASE REQUEST
def username_availability scrubb = help.sanitize(params[:fieldValue], :tags => '') user = User.find_by_email(scrubb) if user.blank? render :json => ["INPUT_ID", true , ""] else render :json => ["INPUT_ID", false , "This email is already taken"] end end
If you are not sure of the correct INPUT ID, monitor the server logs for the parameters passed during the call and make a simple copy-paste. By default, the query passes the INPUT ID and INPUT VALUE identifiers.
To access this assistant, add the following:
CONTROLLER APPLICATIONS
def help Helper.instance end class Helper include Singleton include ActionView::Helpers::TextHelper end
Now in the form itself, your input should look like this:
<%= c.text_field :email, "data-validation-engine"=>"validate[required, custom[email], ajax[ajaxEmailAvailable]]" %>
For proper functionality, always put an AJAX call as your final check.
Remember to include jquery.js, jquery.validationEngine-en.js, jquery.validationEngine.js and validationEngine.jquery.css at the beginning of the document [in this js order] and call
<script type="text/javascript">$(function() {$("#FORM_ID").validationEngine();});</script>
If you want to do this for the username, simply edit the above.