def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True): """ Returns a list of oldforms.FormField instances for this field. It calculates the choices at runtime, not at compile time. name_prefix is a prefix to prepend to the "field_name" argument. rel is a boolean specifying whether this field is in a related context. """ field_objs, params = self.prepare_field_objs_and_params(manipulator, name_prefix) # Add the "unique" validator(s). for field_name_list in opts.unique_together: if field_name_list[0] == self.name: params['validator_list'].append(getattr(manipulator, 'isUnique%s' % '_'.join(field_name_list))) # Add the "unique for..." validator(s). if self.unique_for_date: params['validator_list'].append(getattr(manipulator, 'isUnique%sFor%s' % (self.name, self.unique_for_date))) if self.unique_for_month: params['validator_list'].append(getattr(manipulator, 'isUnique%sFor%s' % (self.name, self.unique_for_month))) if self.unique_for_year: params['validator_list'].append(getattr(manipulator, 'isUnique%sFor%s' % (self.name, self.unique_for_year))) if self.unique and not rel: params['validator_list'].append(curry(manipulator_validator_unique, self, opts, manipulator)) # Only add is_required=True if the field cannot be blank. Primary keys # are a special case, and fields in a related context should set this # as False, because they'll be caught by a separate validator -- # RequiredIfOtherFieldGiven. params['is_required'] = not self.blank and not self.primary_key and not rel # BooleanFields (CheckboxFields) are a special case. They don't take # is_required. if isinstance(self, BooleanField): del params['is_required'] # If this field is in a related context, check whether any other fields # in the related object have core=True. If so, add a validator -- # RequiredIfOtherFieldsGiven -- to this FormField. if rel and not self.blank and not isinstance(self, AutoField) and not isinstance(self, FileField): # First, get the core fields, if any. core_field_names = [] for f in opts.fields: if f.core and f != self: core_field_names.extend(f.get_manipulator_field_names(name_prefix)) # Now, if there are any, add the validator to this FormField. if core_field_names: params['validator_list'].append(validators.RequiredIfOtherFieldsGiven(core_field_names, ugettext_lazy("This field is required."))) # Finally, add the field_names. field_names = self.get_manipulator_field_names(name_prefix) return [man(field_name=field_names[i], **params) for i, man in enumerate(field_objs)]
def get_validator_list(rating_num): if rating_num <= num_rating_choices: return [ validators.RequiredIfOtherFieldsGiven( [ 'rating%d' % i for i in range(1, 9) if i != rating_num ], _("This rating is required because you've entered at least one other rating." )) ] else: return []
def __call__(self, field_data, all_data): if not all_data.get(self.other_file_field_name, False): c = validators.RequiredIfOtherFieldsGiven( self.other_field_names, gettext_lazy("This field is required.")) c(field_data, all_data)