3

I have a form that spans two pages, with a required field on both pages and an optional field on both pages:

Form A - Page 1
- Question 1 (required)
- Question 2 (optional)

[User submits partial form to database]

Form A - Page 2
- Question 3 (required)
- Question 4 (optional)

[User submits partial form to database]

The Form is creating an ElementType in Craft and the questions are Custom Fields. The user who creates the Form can arrange any number of custom fields required or optional on any number of tabs.

How do I think about validation for this workflow? When I create a Model to validate the forms being submitted, required fields should only trigger validation errors when the page they are on is submitted.

I'm happy to do more digging myself on this one if you can help point me in the right direction, but I'm not sure how to frame what I'd like to do. Does anyone have a better framework of how to think about how to validate conditionally required fields in the context of Models, Records, Element Types, and Custom Fields?

3

I can think of a couple of options and I'll just throw them all out there.

  1. Have a separate model per partial form and each model performs its own validation.

  2. Have a single model, but have it override the rules() method and you can conditionally set what validations methods you want depending on the step you're on. Presumably, you'd want to pass in a step number either through the querystring or through POST. You can see an example of how we're doing some conditional validation using this method for email settings in EmailSettingsModel.

  3. Don't worry about any of Craft's/Yii's automagic validation classes and just have the controller manually do its own validation per form and return errors as necessary.

  • Thanks for the various approaches Brad. I'll dig into them all a bit more and experiment! – Ben Parizek Feb 15 '15 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.