diff --git a/lib/WebGUI/ProfileField.pm b/lib/WebGUI/ProfileField.pm index 5faf0897c..ce0cccafd 100644 --- a/lib/WebGUI/ProfileField.pm +++ b/lib/WebGUI/ProfileField.pm @@ -104,12 +104,7 @@ sub create { ### Check data # Check if the field already exists - my $fieldNameExists - = $session->db->quickScalar( - "select count(*) from userProfileField where fieldName=?", - [$fieldName] - ); - return undef if $fieldNameExists; + return undef if $class->exists($session,$fieldName); return undef if $class->isReservedFieldName($fieldName); ### Data okay, create the field @@ -155,6 +150,24 @@ sub delete { $db->deleteRow("userProfileField","fieldName",$self->getId); } +#------------------------------------------------------------------- + +=head2 exists ( session, fieldName ) + +Class method that returns true if a field with the given name already +exists. The first argument is a WebGUI::Session object. C is +the field name to check + +=cut + +sub exists { + my ( $class, $session, $fieldName ) = @_; + + return 1 if $session->db->quickScalar( + "SELECT COUNT(*) FROM userProfileField WHERE fieldName=?", + [$fieldName] + ); +} #------------------------------------------------------------------- @@ -717,12 +730,7 @@ sub rename { ### Check data # Make sure the field doesn't exist - my $fieldNameExists - = $self->session->db->quickScalar( - "SELECT COUNT(*) FROM userProfileField WHERE fieldName=?", - [$newName] - ); - return 0 if ($fieldNameExists); + return 0 if $self->exists($session, $newName); # Rename the userProfileData column my $fieldClass = $self->getFormControlClass; diff --git a/t/ProfileField.t b/t/ProfileField.t index 0b20789bc..f1b0e0f77 100644 --- a/t/ProfileField.t +++ b/t/ProfileField.t @@ -33,7 +33,7 @@ WebGUI::Test->usersToDelete($newUser); #---------------------------------------------------------------------------- # Tests -plan tests => 20; # Increment this number for each test you create +plan tests => 22; # Increment this number for each test you create #---------------------------------------------------------------------------- # Test the creation of ProfileField @@ -102,6 +102,15 @@ is($newProfileField->get('fieldType'), 'HTMLArea', 'test field updated to HTMLAr $fieldSpec = $session->db->quickHashRef('describe userProfileData testField'); is (lc $fieldSpec->{Type}, $htmlFieldType, 'database updated along with profile field object'); +########################################################### +# +# exists +# +########################################################### + +ok( WebGUI::ProfileField->exists($session,"firstName"), "firstName field exists" ); +ok( !WebGUI::ProfileField->exists($session, time), "random field does not exist" ); + #---------------------------------------------------------------------------- # Cleanup END {