IPhone Forms Where Do You Put Your Verification?

Simple question, I am currently using Core Data. I have a form that is responsible for inserting a new item. If there is no category or name, the save button should be disabled. I am currently doing this simple if statement in my controller, but is there any good validation practice in iOS development?

As with rails or any PHP MVC framework, should there be any checks in the models, will this be the same for Core data models?

Thank.

EDIT

What I am currently doing, I am checking with textFieldEditingChanged: if both text boxes are not empty to enable the save button. When they are not empty and the user clicks Save, I create my new Core data object and then save it. What would you suggest according to your decision?

When the field is changed by the method

- (IBAction)textFieldEditingChanged:(UITextField *)textField
{
    saveButton.enabled = [self validatesRequiredFields];

    if (textField == descField)
    {
        itemDesc = descField.text;
    }
    else if (textField == personField)
    {
        personName = personField.text;
    }
    else if (textField == valueField)
    {
        itemValue = valueField.text;
    }
}

      

Verification method

- (BOOL)validatesRequiredFields
{
    if (category != nil && personField.text.length != 0)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

      

When the save button is pressed

- (IBAction)saveButtonPressed
{
    item = [Item createEntity];
    item.type = itemType;
    item.desc = itemDesc;
    item.value = itemValue;
    item.imageFilename = itemImageFilename;
    item.category = category;
    item.addedDate = itemDueDate;

    Person *p = [Person personWithName:personName];

    item.person = p;

    if (dueDateField)
    {
        item.dueDate = itemDueDate;
    }

    [delegate itemAddSaveButtonPressed:item];
}

      

EDIT 2

What am I currently using

- (BOOL)isValid
{
    BOOL valid;
    NSError *error;

    item.type = itemType;
    item.desc = itemDesc;
    item.value = itemValue;
    item.imageFilename = itemImageFilename;
    item.category = category;
    item.addedDate = itemDueDate;

    if (dueDateField)
    {
        item.dueDate = itemDueDate;
    }

    if (personName.length > 0)
    {
        item.person = [Person personWithName:personName];   
    }
    else
    {
        item.person = nil;
    }

    if ([item validateForInsert:&error])
        valid = YES;
    else 
        valid = NO;

    return valid;
}

      

+3


source to share


4 answers


It is important to distinguish between validating the managed entity that will be received from the form and validating the data entered into the form. Core Data automatically checks the managed objects (objects) that you add to your context. However, your question seems to be about validating the data entered into the form, perhaps before the managed object is even created.

As you described, the state of the "Save" button depends on the presence of a name in one of the fields. Obviously the view manager needs to be involved to some extent here, since the model objects don't know anything about views. One way to deal with this is to simply let the view controller do its own validation, as you are doing now. This isn't too bad for simple cases, and it's an obvious route if you've implemented your view controller so that the managed object isn't created until the user closes the Save button.



Another way to do this is to force the view controller to create a new managed object when its view is first displayed and then copy any changes to the user interface to the managed object. If you do, you can use the NSManagedObject -validateForInsert:

and / or methods -validateForUpdate:

to decide if the data in the managed object is valid and you can set the Save button state based on the result. This approach means that all validation rules for the managed object will be validated, and changing the rules for the object does not also require updating the validation code in the view controller.

+3


source


A good practice is to subclass or extend (via categories) the generated managed objects where you can add additional functionality, including data validation.



+2


source


I think you got it right that you should work like Rails.

The easiest way to perform validation is to insert it directly into the data model description. Then, at runtime, you can use validation methods to validate the object and enable / disable buttons (like validateForUpdate :).

If the validation parameters within the data model do not directly suit your needs, you probably need to subclass NSManagedObject and do some spot validation on that class, cf. The Validation Methods parameter is here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html

To make it short, in terms of architecture:

  • model objects need to know if they are valid or not (via Core Data model description or via NSManagedObject subclass) View Controllers
  • have to ask the models if they are valid or not if they want to give some feedback to the user.

Hope it helps.

+2


source


IOS development should follow MVC principles.

-2


source







All Articles