Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

For #342 - trying to make sense of the validation section in common p…

…roblems
  • Loading branch information...
commit f7cc62bc5329b490f6ddf5ba8f86bd19491489b8 1 parent 9fc1642
@addyosmani authored
View
BIN  backbone-fundamentals.epub
Binary file not shown
View
40 backbone-fundamentals.md
@@ -5592,14 +5592,6 @@ This means that it can be a challenge to determine which specific fields are bei
**Solution**
-The most optimal solution to this problem probably isn't to stick validation in your model attributes. Instead, have a function specifically designed for validating that particular form. There are many good JavaScript form validation libraries out there. If you want to stick it on your model, just make it a class function:
-
-```javascript
-User.validate = function(formElement) {
- //...
-};
-```
-
To illustrate this problem better, let us look at a typical registration form use case that:
* Validates form fields using the blur event
@@ -5639,7 +5631,7 @@ HTML:
</html>
```
-Some simple validation that could be written using the current Backbone `validate` method to work with this form could be implemented using something like:
+Basic validation that could be written using the current Backbone `validate` method to work with this form could be implemented using something like:
```javascript
@@ -5652,7 +5644,7 @@ validate: function(attrs) {
}
```
-Unfortunately, this method would trigger a first name error each time any of the fields were blurred and only an error message next to the first name field would be presented.
+Unfortunately, this method would trigger a `firstname` error each time any of the fields were blurred and only an error message next to the first name field would be presented.
One potential solution to the problem is to validate all fields and return all of the errors:
@@ -5668,7 +5660,7 @@ validate: function(attrs) {
}
```
-This can be adapted into a complete solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:
+This can be adapted into a solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:
```javascript
@@ -5708,11 +5700,12 @@ $(function($) {
```
-
-This works great as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A [demo](http://jsbin.com/afetez/2/edit) of the above by [@braddunbar](http://github.com/braddunbar) is also available.
+This works fine as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A [demo](http://jsbin.com/afetez/2/edit) of the above by [@braddunbar](http://github.com/braddunbar) is also available.
Unfortunately, this solution does perform validation on all fields every time, even though we are only displaying errors for the field that has changed. If we have multiple client-side validation methods, we may not want to have to call each validation method on every attribute every time, so this solution might not be ideal for everyone.
+##### Backbone.validateAll
+
A potentially better alternative to the above is to use [@gfranko](http://github.com/@franko)'s [Backbone.validateAll](https://github.com/gfranko/Backbone.validateAll) plugin, specifically created to validate specific Model properties (or form fields) without worrying about the validation of any other Model properties (or form fields).
Here is how we would setup a partial User Model and validate method using this plugin for our use case:
@@ -5804,16 +5797,14 @@ user.set({ 'firstname': 'Greg' }, {validate: true, validateAll: false});
```
-That's it!
-
-The Backbone.validateAll logic doesn't override the default Backbone logic by default and so it's perfectly capable of being used for scenarios where you might care more about field-validation [performance](http://jsperf.com/backbone-validateall) as well as those where you don't. Both solutions presented in this section should work fine however.
+That's it. The Backbone.validateAll logic doesn't override the default Backbone logic by default and so it's perfectly capable of being used for scenarios where you might care more about field-validation [performance](http://jsperf.com/backbone-validateall) as well as those where you don't. Both solutions presented in this section should work fine however.
##### Backbone.Validation
As we've seen, the `validate` method Backbone offers is `undefined` by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.
-A helpful plugin for Backbone called [Backbone.Validation](https://github.com/thedersen/backbone.validation) attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the `validate` method behind the scenes.
+Another helpful plugin for Backbone called [Backbone.Validation](https://github.com/thedersen/backbone.validation) attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the `validate` method behind the scenes.
One of the useful methods this plugin includes is (pseudo) live validation via a `preValidate` method. This can be used to check on key-press if the input for a model is valid without changing the model itself. You can run any validators for a model attribute by calling the `preValidate` method, passing it the name of the attribute as well as the value you would like validated.
@@ -5824,6 +5815,21 @@ One of the useful methods this plugin includes is (pseudo) live validation via a
var errorMsg = myModel.preValidate('attribute', 'value');
```
+##### Form-specific validation classes
+
+That said, the most optimal solution to this problem may not be to stick validation in your model attributes. Instead, you could have a function specifically designed for validating a specific form and there are many good JavaScript form validation libraries out there that can help with this.
+
+If you want to stick it on your model, you can also make it a class function:
+
+```javascript
+User.validate = function(formElement) {
+ //...
+};
+```
+
+For more information on validation plugins available for Backbone, see the [Backbone wiki](https://github.com/documentcloud/backbone/wiki/Extensions%2C-Plugins%2C-Resources#model).
+
+
#### Avoiding Conflicts With Multiple Backbone Versions
**Problem**
View
28 backbone-fundamentals.rtf
@@ -4346,10 +4346,6 @@ documentation
{\pard \ql \f0 \sa180 \li0 \fi0 By default, when we define a custom {\f1 validate} method, Backbone passes all of a model\u8217's attributes through this validation each time, regardless of which model attributes are being set.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 This means that it can be a challenge to determine which specific fields are being set or validated without being concerned about the others that aren\u8217't being set at the same time.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 {\b Solution}\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 The most optimal solution to this problem probably isn\u8217't to stick validation in your model attributes. Instead, have a function specifically designed for validating that particular form. There are many good JavaScript form validation libraries out there. If you want to stick it on your model, just make it a class function:\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 \f1 User.validate = function(formElement) \{\line
- //...\line
-\};\par}
{\pard \ql \f0 \sa180 \li0 \fi0 To illustrate this problem better, let us look at a typical registration form use case that:\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Validates form fields using the blur event\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Validates each field regardless of whether other model attributes (i.e., other form data) are valid or not.\sa180\par}
@@ -4381,7 +4377,7 @@ documentation
</form>\line
</body>\line
</html>\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 Some simple validation that could be written using the current Backbone {\f1 validate} method to work with this form could be implemented using something like:\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 Basic validation that could be written using the current Backbone {\f1 validate} method to work with this form could be implemented using something like:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 validate: function(attrs) \{\line
\line
if(!attrs.firstname) return 'first name is empty';\line
@@ -4389,7 +4385,7 @@ documentation
if(!attrs.email) return 'email is empty';\line
\line
\}\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 Unfortunately, this method would trigger a first name error each time any of the fields were blurred and only an error message next to the first name field would be presented.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 Unfortunately, this method would trigger a {\f1 firstname} error each time any of the fields were blurred and only an error message next to the first name field would be presented.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 One potential solution to the problem is to validate all fields and return all of the errors:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 validate: function(attrs) \{\line
var errors = \{\};\line
@@ -4400,7 +4396,7 @@ documentation
\line
if (!_.isEmpty(errors)) return errors;\line
\}\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 This can be adapted into a complete solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 This can be adapted into a solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 \line
$(function($) \{\line
\line
@@ -4435,7 +4431,7 @@ $(function($) \{\line
\});\line
\line
\});\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 This works great as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A {\field{\*\fldinst{HYPERLINK "http://jsbin.com/afetez/2/edit"}}{\fldrslt{\ul
+{\pard \ql \f0 \sa180 \li0 \fi0 This works fine as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A {\field{\*\fldinst{HYPERLINK "http://jsbin.com/afetez/2/edit"}}{\fldrslt{\ul
demo
}}}
of the above by {\field{\*\fldinst{HYPERLINK "http://github.com/braddunbar"}}{\fldrslt{\ul
@@ -4443,6 +4439,7 @@ demo
}}}
is also available.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 Unfortunately, this solution does perform validation on all fields every time, even though we are only displaying errors for the field that has changed. If we have multiple client-side validation methods, we may not want to have to call each validation method on every attribute every time, so this solution might not be ideal for everyone.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs20 Backbone.validateAll\par}
{\pard \ql \f0 \sa180 \li0 \fi0 A potentially better alternative to the above is to use {\field{\*\fldinst{HYPERLINK "http://github.com/@franko"}}{\fldrslt{\ul
@gfranko
}}}
@@ -4527,14 +4524,13 @@ var User = Backbone.Model.extend(\{\line
{\pard \ql \f0 \sa180 \li0 \fi0 It\u8217's fairly straight-forward to use as well. We can simply define a new Model instance and then set the data on our model using the {\f1 validateAll} option to use the behavior defined by the plugin:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 var user = new User();\line
user.set(\{ 'firstname': 'Greg' \}, \{validate: true, validateAll: false\});\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 That\u8217's it!\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 The Backbone.validateAll logic doesn\u8217't override the default Backbone logic by default and so it\u8217's perfectly capable of being used for scenarios where you might care more about field-validation {\field{\*\fldinst{HYPERLINK "http://jsperf.com/backbone-validateall"}}{\fldrslt{\ul
+{\pard \ql \f0 \sa180 \li0 \fi0 That\u8217's it. The Backbone.validateAll logic doesn\u8217't override the default Backbone logic by default and so it\u8217's perfectly capable of being used for scenarios where you might care more about field-validation {\field{\*\fldinst{HYPERLINK "http://jsperf.com/backbone-validateall"}}{\fldrslt{\ul
performance
}}}
as well as those where you don\u8217't. Both solutions presented in this section should work fine however.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs20 Backbone.Validation\par}
{\pard \ql \f0 \sa180 \li0 \fi0 As we\u8217've seen, the {\f1 validate} method Backbone offers is {\f1 undefined} by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 A helpful plugin for Backbone called {\field{\*\fldinst{HYPERLINK "https://github.com/thedersen/backbone.validation"}}{\fldrslt{\ul
+{\pard \ql \f0 \sa180 \li0 \fi0 Another helpful plugin for Backbone called {\field{\*\fldinst{HYPERLINK "https://github.com/thedersen/backbone.validation"}}{\fldrslt{\ul
Backbone.Validation
}}}
attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the {\f1 validate} method behind the scenes.\par}
@@ -4543,6 +4539,16 @@ Backbone.Validation
// if not, it returns a falsy value\line
\line
var errorMsg = myModel.preValidate('attribute', 'value');\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs20 Form-specific validation classes\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 That said, the most optimal solution to this problem may not be to stick validation in your model attributes. Instead, you could have a function specifically designed for validating a specific form and there are many good JavaScript form validation libraries out there that can help with this.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 If you want to stick it on your model, you can also make it a class function:\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 \f1 User.validate = function(formElement) \{\line
+ //...\line
+\};\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 For more information on validation plugins available for Backbone, see the {\field{\*\fldinst{HYPERLINK "https://github.com/documentcloud/backbone/wiki/Extensions%2C-Plugins%2C-Resources#model"}}{\fldrslt{\ul
+Backbone wiki
+}}}
+.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs24 Avoiding Conflicts With Multiple Backbone Versions\par}
{\pard \ql \f0 \sa180 \li0 \fi0 {\b Problem}\par}
{\pard \ql \f0 \sa180 \li0 \fi0 In instances out of your control, you may have to work around having more than one version of Backbone in the same page. How do you work around this without causing conflicts?\par}
View
40 chapters/07-common-problems.md
@@ -408,14 +408,6 @@ This means that it can be a challenge to determine which specific fields are bei
**Solution**
-The most optimal solution to this problem probably isn't to stick validation in your model attributes. Instead, have a function specifically designed for validating that particular form. There are many good JavaScript form validation libraries out there. If you want to stick it on your model, just make it a class function:
-
-```javascript
-User.validate = function(formElement) {
- //...
-};
-```
-
To illustrate this problem better, let us look at a typical registration form use case that:
* Validates form fields using the blur event
@@ -455,7 +447,7 @@ HTML:
</html>
```
-Some simple validation that could be written using the current Backbone `validate` method to work with this form could be implemented using something like:
+Basic validation that could be written using the current Backbone `validate` method to work with this form could be implemented using something like:
```javascript
@@ -468,7 +460,7 @@ validate: function(attrs) {
}
```
-Unfortunately, this method would trigger a first name error each time any of the fields were blurred and only an error message next to the first name field would be presented.
+Unfortunately, this method would trigger a `firstname` error each time any of the fields were blurred and only an error message next to the first name field would be presented.
One potential solution to the problem is to validate all fields and return all of the errors:
@@ -484,7 +476,7 @@ validate: function(attrs) {
}
```
-This can be adapted into a complete solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:
+This can be adapted into a solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:
```javascript
@@ -524,11 +516,12 @@ $(function($) {
```
-
-This works great as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A [demo](http://jsbin.com/afetez/2/edit) of the above by [@braddunbar](http://github.com/braddunbar) is also available.
+This works fine as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A [demo](http://jsbin.com/afetez/2/edit) of the above by [@braddunbar](http://github.com/braddunbar) is also available.
Unfortunately, this solution does perform validation on all fields every time, even though we are only displaying errors for the field that has changed. If we have multiple client-side validation methods, we may not want to have to call each validation method on every attribute every time, so this solution might not be ideal for everyone.
+##### Backbone.validateAll
+
A potentially better alternative to the above is to use [@gfranko](http://github.com/@franko)'s [Backbone.validateAll](https://github.com/gfranko/Backbone.validateAll) plugin, specifically created to validate specific Model properties (or form fields) without worrying about the validation of any other Model properties (or form fields).
Here is how we would setup a partial User Model and validate method using this plugin for our use case:
@@ -620,16 +613,14 @@ user.set({ 'firstname': 'Greg' }, {validate: true, validateAll: false});
```
-That's it!
-
-The Backbone.validateAll logic doesn't override the default Backbone logic by default and so it's perfectly capable of being used for scenarios where you might care more about field-validation [performance](http://jsperf.com/backbone-validateall) as well as those where you don't. Both solutions presented in this section should work fine however.
+That's it. The Backbone.validateAll logic doesn't override the default Backbone logic by default and so it's perfectly capable of being used for scenarios where you might care more about field-validation [performance](http://jsperf.com/backbone-validateall) as well as those where you don't. Both solutions presented in this section should work fine however.
##### Backbone.Validation
As we've seen, the `validate` method Backbone offers is `undefined` by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.
-A helpful plugin for Backbone called [Backbone.Validation](https://github.com/thedersen/backbone.validation) attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the `validate` method behind the scenes.
+Another helpful plugin for Backbone called [Backbone.Validation](https://github.com/thedersen/backbone.validation) attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the `validate` method behind the scenes.
One of the useful methods this plugin includes is (pseudo) live validation via a `preValidate` method. This can be used to check on key-press if the input for a model is valid without changing the model itself. You can run any validators for a model attribute by calling the `preValidate` method, passing it the name of the attribute as well as the value you would like validated.
@@ -640,6 +631,21 @@ One of the useful methods this plugin includes is (pseudo) live validation via a
var errorMsg = myModel.preValidate('attribute', 'value');
```
+##### Form-specific validation classes
+
+That said, the most optimal solution to this problem may not be to stick validation in your model attributes. Instead, you could have a function specifically designed for validating a specific form and there are many good JavaScript form validation libraries out there that can help with this.
+
+If you want to stick it on your model, you can also make it a class function:
+
+```javascript
+User.validate = function(formElement) {
+ //...
+};
+```
+
+For more information on validation plugins available for Backbone, see the [Backbone wiki](https://github.com/documentcloud/backbone/wiki/Extensions%2C-Plugins%2C-Resources#model).
+
+
#### Avoiding Conflicts With Multiple Backbone Versions
**Problem**
View
27 index.html
@@ -185,7 +185,9 @@
<li><a href="#rendering-view-hierarchies">Rendering View Hierarchies</a></li>
<li><a href="#working-with-nested-models-or-collections">Working With Nested Models Or Collections</a></li>
<li><a href="#better-model-property-validation">Better Model Property Validation</a><ul>
+<li><a href="#backbone.validateall">Backbone.validateAll</a></li>
<li><a href="#backbone.validation">Backbone.Validation</a></li>
+<li><a href="#form-specific-validation-classes">Form-specific validation classes</a></li>
</ul></li>
<li><a href="#avoiding-conflicts-with-multiple-backbone-versions">Avoiding Conflicts With Multiple Backbone Versions</a></li>
<li><a href="#building-model-and-view-hierarchies">Building Model And View Hierarchies</a></li>
@@ -4631,10 +4633,6 @@ <h4 id="better-model-property-validation"><a href="#TOC">Better Model Property V
<p>By default, when we define a custom <code>validate</code> method, Backbone passes all of a model’s attributes through this validation each time, regardless of which model attributes are being set.</p>
<p>This means that it can be a challenge to determine which specific fields are being set or validated without being concerned about the others that aren’t being set at the same time.</p>
<p><strong>Solution</strong></p>
-<p>The most optimal solution to this problem probably isn’t to stick validation in your model attributes. Instead, have a function specifically designed for validating that particular form. There are many good JavaScript form validation libraries out there. If you want to stick it on your model, just make it a class function:</p>
-<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">User</span>.<span class="fu">validate</span> = <span class="kw">function</span>(formElement) {
- <span class="co">//...</span>
-};</code></pre>
<p>To illustrate this problem better, let us look at a typical registration form use case that:</p>
<ul>
<li>Validates form fields using the blur event</li>
@@ -4668,7 +4666,7 @@ <h4 id="better-model-property-validation"><a href="#TOC">Better Model Property V
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
-<p>Some simple validation that could be written using the current Backbone <code>validate</code> method to work with this form could be implemented using something like:</p>
+<p>Basic validation that could be written using the current Backbone <code>validate</code> method to work with this form could be implemented using something like:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="dt">validate</span>: <span class="kw">function</span>(attrs) {
<span class="kw">if</span>(!<span class="kw">attrs</span>.<span class="fu">firstname</span>) <span class="kw">return</span> <span class="ch">&#39;first name is empty&#39;</span>;
@@ -4676,7 +4674,7 @@ <h4 id="better-model-property-validation"><a href="#TOC">Better Model Property V
<span class="kw">if</span>(!<span class="kw">attrs</span>.<span class="fu">email</span>) <span class="kw">return</span> <span class="ch">&#39;email is empty&#39;</span>;
}</code></pre>
-<p>Unfortunately, this method would trigger a first name error each time any of the fields were blurred and only an error message next to the first name field would be presented.</p>
+<p>Unfortunately, this method would trigger a <code>firstname</code> error each time any of the fields were blurred and only an error message next to the first name field would be presented.</p>
<p>One potential solution to the problem is to validate all fields and return all of the errors:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="dt">validate</span>: <span class="kw">function</span>(attrs) {
<span class="kw">var</span> errors = {};
@@ -4687,7 +4685,7 @@ <h4 id="better-model-property-validation"><a href="#TOC">Better Model Property V
<span class="kw">if</span> (!<span class="kw">_</span>.<span class="fu">isEmpty</span>(errors)) <span class="kw">return</span> errors;
}</code></pre>
-<p>This can be adapted into a complete solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:</p>
+<p>This can be adapted into a solution that defines a Field model for each input in our form and works within the parameters of our use case as follows:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript">
$(<span class="kw">function</span>($) {
@@ -4722,8 +4720,9 @@ <h4 id="better-model-property-validation"><a href="#TOC">Better Model Property V
});
});</code></pre>
-<p>This works great as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A <a href="http://jsbin.com/afetez/2/edit">demo</a> of the above by <a href="http://github.com/braddunbar">@braddunbar</a> is also available.</p>
+<p>This works fine as the solution checks the validation for each attribute individually and sets the message for the correct blurred field. A <a href="http://jsbin.com/afetez/2/edit">demo</a> of the above by <a href="http://github.com/braddunbar">@braddunbar</a> is also available.</p>
<p>Unfortunately, this solution does perform validation on all fields every time, even though we are only displaying errors for the field that has changed. If we have multiple client-side validation methods, we may not want to have to call each validation method on every attribute every time, so this solution might not be ideal for everyone.</p>
+<h5 id="backbone.validateall"><a href="#TOC">Backbone.validateAll</a></h5>
<p>A potentially better alternative to the above is to use <a href="http://github.com/@franko">@gfranko</a>’s <a href="https://github.com/gfranko/Backbone.validateAll">Backbone.validateAll</a> plugin, specifically created to validate specific Model properties (or form fields) without worrying about the validation of any other Model properties (or form fields).</p>
<p>Here is how we would setup a partial User Model and validate method using this plugin for our use case:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript">
@@ -4802,16 +4801,22 @@ <h4 id="better-model-property-validation"><a href="#TOC">Better Model Property V
<p>It’s fairly straight-forward to use as well. We can simply define a new Model instance and then set the data on our model using the <code>validateAll</code> option to use the behavior defined by the plugin:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> user = <span class="kw">new</span> User();
<span class="kw">user</span>.<span class="fu">set</span>({ <span class="ch">&#39;firstname&#39;</span>: <span class="ch">&#39;Greg&#39;</span> }, {<span class="dt">validate</span>: <span class="kw">true</span>, <span class="dt">validateAll</span>: <span class="kw">false</span>});</code></pre>
-<p>That’s it!</p>
-<p>The Backbone.validateAll logic doesn’t override the default Backbone logic by default and so it’s perfectly capable of being used for scenarios where you might care more about field-validation <a href="http://jsperf.com/backbone-validateall">performance</a> as well as those where you don’t. Both solutions presented in this section should work fine however.</p>
+<p>That’s it. The Backbone.validateAll logic doesn’t override the default Backbone logic by default and so it’s perfectly capable of being used for scenarios where you might care more about field-validation <a href="http://jsperf.com/backbone-validateall">performance</a> as well as those where you don’t. Both solutions presented in this section should work fine however.</p>
<h5 id="backbone.validation"><a href="#TOC">Backbone.Validation</a></h5>
<p>As we’ve seen, the <code>validate</code> method Backbone offers is <code>undefined</code> by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.</p>
-<p>A helpful plugin for Backbone called <a href="https://github.com/thedersen/backbone.validation">Backbone.Validation</a> attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the <code>validate</code> method behind the scenes.</p>
+<p>Another helpful plugin for Backbone called <a href="https://github.com/thedersen/backbone.validation">Backbone.Validation</a> attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the <code>validate</code> method behind the scenes.</p>
<p>One of the useful methods this plugin includes is (pseudo) live validation via a <code>preValidate</code> method. This can be used to check on key-press if the input for a model is valid without changing the model itself. You can run any validators for a model attribute by calling the <code>preValidate</code> method, passing it the name of the attribute as well as the value you would like validated.</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="co">// If the value of the attribute is invalid, a truthy error message is returned</span>
<span class="co">// if not, it returns a falsy value</span>
<span class="kw">var</span> errorMsg = <span class="kw">myModel</span>.<span class="fu">preValidate</span>(<span class="ch">&#39;attribute&#39;</span>, <span class="ch">&#39;value&#39;</span>);</code></pre>
+<h5 id="form-specific-validation-classes"><a href="#TOC">Form-specific validation classes</a></h5>
+<p>That said, the most optimal solution to this problem may not be to stick validation in your model attributes. Instead, you could have a function specifically designed for validating a specific form and there are many good JavaScript form validation libraries out there that can help with this.</p>
+<p>If you want to stick it on your model, you can also make it a class function:</p>
+<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">User</span>.<span class="fu">validate</span> = <span class="kw">function</span>(formElement) {
+ <span class="co">//...</span>
+};</code></pre>
+<p>For more information on validation plugins available for Backbone, see the <a href="https://github.com/documentcloud/backbone/wiki/Extensions%2C-Plugins%2C-Resources#model">Backbone wiki</a>.</p>
<h4 id="avoiding-conflicts-with-multiple-backbone-versions"><a href="#TOC">Avoiding Conflicts With Multiple Backbone Versions</a></h4>
<p><strong>Problem</strong></p>
<p>In instances out of your control, you may have to work around having more than one version of Backbone in the same page. How do you work around this without causing conflicts?</p>
Please sign in to comment.
Something went wrong with that request. Please try again.