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

#include <WObject>

namespace Wt {

class WString;

/*! \class WValidator WValidator WValidator
 *  \brief A WValidator is used to validate user input according to
 *         pre-defined rules.
 *
 * A WValidator can be associated with a WFormWidget, using
 * WFormWidget::setValidator.
 *
 * This WValidator only checks that mandatory fields are not empty.
 * This class is reimplemented in WIntValidator, WDoubleValidator,
 * and WRegExpValidator, which perform checks on the input as well.
 *
 * If these validators are not suitable, you can inherit from this class,
 * and provide a suitable implementation to validate() and fixup().
 *
 * \sa WFormWidget
 * \sa WValidationStatus
 */
class WT_API WValidator : public WObject
{
public:
  /*! \brief The state in which validated input can exist.
   */
  enum State { Invalid,	     //!< The input is invalid.
	       InvalidEmpty, //!< The input is invalid (emtpy and mandatory).
	       Valid	     //!< The input is valid.
  };

  /*! \brief Create a new validator.
   *
   * Indicate whether input is mandatory.
   *
   * \sa setMandatory(bool)
   */
  WValidator(bool mandatory = false);

  /*! \brief Set if input is mandatory
   *
   * When an input is not mandatory, then an empty field is always
   * valid.
   */
  void setMandatory(bool how);

  /*! \brief Returns if input is mandatory.
   */
  bool isMandatory() const { return mandatory_; }

  /*! \brief This function attempts to change input to be valid according to
   *         the validator's rules.
   *
   * In general the function needs not to change the input into a
   * valid input. The default implementation does nothing. But it may
   * help the user in getting its input right.
   */
  virtual void fixup(WString& input) const;

  /*! \brief Evaluate the validness of the given input.
   *
   * This function returns the current state of the input.
   *
   * The function can change both input and pos (the cursor position)
   * if required.
   */
  virtual State validate(WString& input, int& pos) const;

private:
  bool mandatory_;
};

}

#endif // WVALIDATOR_H_
