Domanda

Problema:

Ho una forma in TurboGears 2 che ha un campo di testo per un elenco di e-mail. C'è un modo semplice utilizzando ToscaWidgets o FormEncode a validatori forma a catena per Set e-mail o dovrò scrivere il mio validatore per questo?

È stato utile?

Soluzione

Penso che dovrebbe essere più simile alla seguente. Ha il vantaggio di cercare ogni e-mail invece di fermarsi al primo valido. Sarà anche aggiungere gli errori allo Stato così si potrebbe dire quali avevano fallito.

from formencode import FancyValidator, Invalid
from formencode.validators import Email

class EmailList(FancyValidator):
    """ Takes a delimited (default is comma) string and returns a list of validated e-mails
        Set the delimiter by passing delimiter="A_DELIMITER" to the constructor.
        Also takes all arguments a FancyValidator does.  
        The e-mails will always be stripped of whitespace.
    """
    def _to_python(self, value, state):
        try:
            values = str(value).split(self.delimiter)
        except AttributeError:
            values = str(value).split(',')
        validator = formencode.ForEach(validators.Email())
        validator.to_python(values, state)
        return [value.strip() for value in values]

Altri suggerimenti

http://formencode.org/Validator.html

Un altro validatore notevole è formencode.compound.All - questo è un validatore composto - che è, si tratta di un validatore che prende validatori come input. Schemi sono un esempio; In questo caso tutti prende una lista di validatori e si applica ciascuno di loro a turno. formencode.compound.Any è il suo complimento, che utilizza il primo validatore che passa nella sua lista.

Quello che volevo era un validatore ho potuto solo bastone in un campo come la corda e Int validatori. Ho trovato che non c'era modo di farlo a meno che non ho creato il mio validatore. Sto postando qui per completezza, e in modo che altri possano beneficiare.

from formencode import FancyValidator, Invalid
from formencode.validators import Email

class EmailList(FancyValidator):
    """ Takes a delimited (default is comma) string and returns a list of validated e-mails
        Set the delimiter by passing delimiter="A_DELIMITER" to the constructor.
        Also takes all arguments a FancyValidator does.  
        The e-mails will always be stripped of whitespace.
    """
    def _to_python(self, value, state):
        try:
            values = str(value).split(self.delimiter)
        except AttributeError:
            values = str(value).split(',')
        returnValues = []
        emailValidator = Email()
        for value in values:
            returnValues.append( emailValidator._to_python(value.strip(), state) )
        return values

FormEncode validatori - Tubo e Wrapper, è potrebbe:

from formencode import validators, compound


compound.Pipe(validators.Wrapper(to_python=lambda v: v.split(',')),
              validators.Email())
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top