UA-17470720-3

Jump to content


Photo
- - - - -

Control when doneButton is enabled

core-data view-controllers actions data-definitions

  • Please log in to reply
7 replies to this topic

#1 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 11 December 2013 - 08:43 AM

Hello,

I am trying to fine-tune my CoreData entity validation.  I have a new object that is presented in an STV detail view, with 6 properties that need to all have values filled in before the object can be saved (by pressing the generated UIBarButtonItem "Done" in this case).  I was able to accomplish that by marking those propertyDef's with required = TRUE.  I now need to add additional validation.  The 6 properties are grouped into 2 sections of 3 properties:

 

 

Section A

  Property 1

  Property 2

  Property 3

 

Section B

  Property 4

  Property 5

  Property 6

 

All properties are defined in CoreData as int16.  I need to prevent the "Done" button from being enabled if the sum of either section is over 100.  I tinkered with using cellActions.valueIsValid but I don't think this will work because if I return FALSE from this block, while it will prevent the "Done" button from being enabled, it will also prevent the value from committing to the bound object.  Normally that would be fine, but since I have 3 cells that are basically "linked" together, it could cause the object to NEVER be valid.  Basically I'm looking for a .entityIsValid or .objectIsValid equivalent so I can evaluate the presented object as a whole, rather than the individual properties.

 

Anyone have ideas on how to accomplish this?  I'm thinking I may need to subclass SCTableViewController and handle the doneButton.

 

Thanks,

Aaron



#2 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 12 December 2013 - 09:32 AM

I was able to get close enough by using the doneButtonTapped action on the tableViewController.  It doesn't disable the "Done" button, but it does prevent the object from saving and the detailViewController from dismissing, and allows me to alert the user that they need to fix their form.  SCArrayOfObjectsAttributes is where I set the block.

 

Thanks,

Aaron



#3 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 12 December 2013 - 03:22 PM

i do things like this

 

if its not valid then change the cells background colour so it stands out

Def.cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        if(cell.valueIsValid)
            cell.backgroundColor = [UIColor clearColor];
        else
            cell.backgroundColor = CELL_BACKGROUND_ERROR;
    };

and you can test the valid 

cellActions.valueIsValid = ^BOOL(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        BOOL valid = FALSE;
 
        if([cell isKindOfClass:[SCTextFieldCell class]])
        {
            SCTextFieldCell *textFieldCell = (SCTextFieldCell *)cell;
 
            // Make sure the password field is at least 8 characters long
            if([textFieldCell.textField.text length] >= 8)
                valid = TRUE;
        }
 
        return valid;
    };

if returns false the DONE button is disabled for you

 

hope that helps


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#4 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 13 December 2013 - 04:08 AM

Hey Ozie,

Thank you for posting that.  Unfortunately, as I mentioned, that does not work for me.  That is a great method for evaluating each property on its own, but I need to evaluate the object as a whole.  

 

If you return FALSE from cellActions.valueIsValid, the value is not committed, which obviously makes sense from a individual property validation perspective, but is much more difficult to validate the object in that case.

 

I still think it would be nice to have an objectIsValid block that is called after a valueChanged that would allow us to much more easily test multiple properties and conditions based on the object as a whole, rather than individual properties, one at a time.



#5 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 13 December 2013 - 06:55 AM

Greetings Program!

 

I have a grid of order lines and when when a row is selected I save the values of the object into a dictionary to revert back to if/when the user wants to revert back to the original values later on.

 

You could probably do the same thing. On cell selection, save the object values to a dictionary first and let the values be committed during editing. Then when you catch the done button action, you could do your check on the object and if it fails validation, revert the object back to the original values from the dictionary.

 

I found but have not tried using the NSManagedObject changedValues or committedValuesForKeys to enumerate through the list and revert back to the values using it.

 

Hope that helps!

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 15 December 2013 - 09:56 AM

Hi Aaron,

 

Have you had a look at the valueIsValid cell action? This action is specifically designed for custom user validations.

 

Please let me know if you need more help.



#7 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 15 December 2013 - 12:51 PM

Hey Tarek

Thanks for the response on a Sunday. :)

 

I have indeed tried valueIsValid.  Unfortunately it doesn't work in my case because, in addition to disabling the "Done" button, it seems to ALSO prevent STV from committing the value to the boundObject.  As I mentioned, I need to evaluate whether the user can submit the object or not based on the value of 3 cells combined, not just one.  It is perfect for validating individual values but unless I am missing something, does not work as well when the object's validity is dependent upon other properties as well.  I take valueIsValid to be more for property-level validation (correct me if I'm wrong), which then determines whether the value is actually committed to the boundObject or not, rather than OBJECT validation, which would determine whether or not the "Done" button should be enabled.

 

As I mentioned I was able to achieve an effect close to what I am shooting for by providing a doneButtonTapped action and checking whether or not the object is valid there.  This does not enable or disable the "Done" button, but in my case it does prevent the user from saving a potentially invalid object.

 

Thanks,
Aaron



#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 15 December 2013 - 02:07 PM

Hi Aaron,

 

You can still validate the values of all three fields in valueIsValid, and only return TRUE when your criteria has been met. There is nothing that prevents you from accessing all three fields in valueIsValid, unless I am missing something here.







Also tagged with one or more of these keywords: core-data, view-controllers, actions, data-definitions

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users