// This may look like C code, but it's really -*- C++ -*-
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#ifndef WLENGTHVALIDATOR_H_
#define WLENGTHVALIDATOR_H_

#include <limits>

#include <Wt/WValidator>

namespace Wt {

/*! \class WLengthValidator Wt/WLengthValidator Wt/WLengthValidator
 *  \brief A validator that checks the string length of user input.
 *
 * This validator checks whether user input is within the specified range
 * of accepted string lengths.
 *
 * If you only want to limit the length on a line edit, you may also
 * use WLineEdit::setMaxLength().
 */
class WT_API WLengthValidator : public WValidator
{
public:
  /*! \brief Create a length validator that accepts input of any length.
   */
  WLengthValidator(WObject *parent = 0);

  /*! \brief Create a length validator that accepts input within a length range.
   */
  WLengthValidator(int minLength, int maxLength, WObject *parent = 0);

  /*! \brief Set the minimum length.
   *
   * The default value is 0.
   */
  void setMinimumLength(int minimum);

  /*! \brief Return the minimum length.
   *
   * \sa setMinimumLength(int)
   */
  int minimumLength() const { return minLength_; }

  /*! \brief Set the maximum length.
   *
   * The default value is std::numeric_limits<int>::max()
   */
  void setMaximumLength(int maximum);

  /*! \brief Returns the maximum length.
   *
   * \sa setMaximumLength(int)
   */
  int maximumLength() const { return maxLength_; }

  /*! \brief Validate the given input.
   *
   * The input is considered valid only when it is blank for a non-mandatory
   * field, or has a length within the valid range.
   */
  virtual State validate(WString& input, int& pos) const;

  virtual void createExtConfig(std::ostream& config) const;

  /*! \brief Set message to display when the input is too short.
   *
   * Depending on whether maximumLength() is a real bound, the default
   * message is "The input must have a length between {1} and {2}
   * characters" or " "The input must be at least {1} characters".
   */
  void setInvalidTooShortText(const WString& text);

  /*! \brief Returns the message displayed when the input is too short.
   *
   * \sa setInvalidTooShortText(const WString&)
   */
  WString invalidTooShortText() const;

  /*! \brief Set message to display when the input is too long.
   *
   * Depending on whether minimumLength() is different from zero, the
   * default message is "The input must have a length between {1} and
   * {2} characters" or " "The input must be no more than {2}
   * characters".
   */
  void setInvalidTooLongText(const WString& text);

  /*! \brief Returns the message displayed when the input is too long.
   *
   * \sa setInvalidTooLongText(const WString&)
   */
  WString invalidTooLongText() const;

protected:
  std::string javaScriptValidate(const std::string& jsRef) const;

private:
  int minLength_;
  int maxLength_;

  WString tooLongText_;
  WString tooShortText_;
};

}

#endif // WLENGTHVALIDATOR_H_
