def listen_input(self, parser, tag, attrs): get_attr = parser.get_attr name = get_attr(attrs, 'name') if not name: # @@: should warn if you try to validate unnamed fields return v = compound.All(validators.Identity()) add_to_end = None # for checkboxes, we must set if_missing = False if tag.lower() == "input": type_attr = get_attr(attrs, "type").lower().strip() if type_attr == "submit": v.validators.append(validators.Bool()) elif type_attr == "checkbox": v.validators.append(validators.Wrapper(to_python = force_list)) elif type_attr == "file": add_to_end = validators.FieldStorageUploadConverter() message = get_attr(attrs, 'form:message') required = to_bool(get_attr(attrs, 'form:required', 'false')) if required: v.validators.append( validators.NotEmpty( messages=get_messages(validators.NotEmpty, message))) else: v.validators[0].if_missing = False if add_to_end: v.validators.append(add_to_end) v_type = get_attr(attrs, 'form:validate', None) if v_type: pos = v_type.find(':') if pos != -1: # @@: should parse args args = (v_type[pos+1:],) v_type = v_type[:pos] else: args = () v_type = v_type.lower() v_class = self.validators.get(v_type) if not v_class: raise ValueError("Invalid validation type: %r" % v_type) kw_args={'messages': get_messages(v_class, message)} v_inst = v_class( *args, **kw_args) v.validators.append(v_inst) self._schema.add_field(name, v)
def __init__(self, main_form, type, name, label=None, required=False, nuci_path=None, nuci_preproc=None, validators=None, hint="", multifield=False, **kwargs): """ :param main_form: parent form of this field :type main_form: ForisForm :param type: type of field :param name: field name (rendered also as HTML name attribute) :param label: display name of field :param required: True if field is mandatory :param nuci_path: path in Nuci get response :param nuci_preproc: function to process raw YinElement instance, returns field value :type nuci_preproc: callable :param validators: validator or list of validators :type validators: validator or list :param hint: short descriptive text explaining the purpose of the field :param multifield: whether multiple values can be returned :param kwargs: passed to Input constructor """ super(Field, self).__init__(name) # self.type = type self.name = name self.nuci_path = nuci_path self.nuci_preproc = nuci_preproc if validators and not isinstance(validators, list): validators = [validators] self.validators = validators or [] if not all( map(lambda x: isinstance(x, validators_module.Validator), self.validators)): raise TypeError( "Argument 'validators' must be Validator instance or list of them." ) self._kwargs = kwargs self.required = required if self.required: self.validators.append(validators_module.NotEmpty()) self._kwargs["required"] = self.required self._kwargs["description"] = label self.requirements = {} self.hint = hint self.multifield = multifield default = kwargs.pop("default", [] if self.multifield else None) if issubclass(self.type, Checkbox): self._kwargs["value"] = "1" # we need a non-empty value here self._kwargs["checked"] = False if default == "0" else bool( default) # set defaults for main form self._main_form = main_form self._main_form.defaults.setdefault(name, default) # cache for rendered field - remove after finishing TODO #2793 self.__field_cache = None
def test_empty(self): try: validators.NotEmpty('', ['node']) self.assertTrue(False) except ValueError as ex: self.assertEqual('node: Cannot be empty', ex.message)
def test_not_empty(self): value = 'abc' actual = validators.NotEmpty(value, []) self.assertEqual(value, actual)
def test_none(self): try: validators.NotEmpty(None, []) self.assertTrue(False) except ValueError as ex: self.assertEqual('Cannot be null', ex.message)