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

<ol>
<li><p>Have a separate model per partial form and each model performs its own validation.</p></li>
<li><p>Have a single model, but have it override the <a href="http://www.yiiframework.com/doc/api/1.1/CModel#rules-detail" rel="nofollow">rules()</a> 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 <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/models/EmailSettingsModel.php#L24" rel="nofollow">EmailSettingsModel</a>.</p></li>
<li><p>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.</p></li>
</ol>

CC BY-SA 3.0
2015-02-15T19:26:50.740Z
Thanks for the various approaches Brad.  I'll dig into them all a bit more and experiment!
Ben Parizek
CC BY-SA 3.0
2015-02-15T04:45:43.207Z
2015-02-15T20:59:21.827Z
2015-02-15T20:59:21.827Z
Brad Bell
<p>I have a form that spans two pages, with a required field on both pages and an optional field on both pages:</p>

<pre><code>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]
</code></pre>

<p>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.</p>

<p>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.</p>

<p>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?</p>

<plugin-development><custom-field><models><validation><records>
Can a model or record have conditional requirements?
# Can a model or record have conditional requirements?

(Asked by: username_0 on 2015-02-15)

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?

**Tags:** plugin-development, custom-field, models, validation, records

---

## 1 answers

### Answer by username_1 on 2015-02-15. Score: 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()](http://www.yiiframework.com/doc/api/1.1/CModel#rules-detail) 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](https://github.com/pixelandtonic/Craft-Release/blob/master/app/models/EmailSettingsModel.php#L24).
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.

> **Comments:**
> **username_0**: Thanks for the various approaches Brad.  I'll dig into them all a bit more and experiment!  

---

