hyperactiveform 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,13 +6,13 @@
6
6
  <title>
7
7
  File: README
8
8
 
9
- &mdash; Documentation by YARD 0.9.37
9
+ &mdash; Documentation by YARD 0.9.45
10
10
 
11
11
  </title>
12
12
 
13
- <link rel="stylesheet" href="css/style.css" type="text/css" />
13
+ <link rel="stylesheet" href="css/style.css" type="text/css">
14
14
 
15
- <link rel="stylesheet" href="css/common.css" type="text/css" />
15
+ <link rel="stylesheet" href="css/common.css" type="text/css">
16
16
 
17
17
  <script type="text/javascript">
18
18
  pathId = "README";
@@ -27,6 +27,8 @@
27
27
 
28
28
  </head>
29
29
  <body>
30
+ <div id="main_progress" aria-hidden="true"></div>
31
+
30
32
  <div class="nav_wrap">
31
33
  <iframe id="nav" src="file_list.html?1"></iframe>
32
34
  <div id="resizer"></div>
@@ -57,49 +59,29 @@
57
59
  <div class="clear"></div>
58
60
  </div>
59
61
 
60
- <div id="content"><div id='filecontents'>
61
- <h1 id="label-E2-9C-A8+HyperActiveForm+-E2-9C-A8">✨ HyperActiveForm ✨</h1>
62
-
62
+ <div id="content"><div id='filecontents'><h1 id="__HyperActiveForm__">✨ HyperActiveForm ✨</h1>
63
63
  <p>HyperActiveForm is a simple form object implementation for Rails.</p>
64
-
65
64
  <p>Form objects are objects that encapsulate form logic and validations, they allow to extract the business logic out of the controller and models into specialized objects.</p>
66
-
67
- <p>HyperActiveForm’s form objects mimic the ActiveModel API, so they work out of the box with Rails’ form helpers, and allow you to use the ActiveModel validations you already know.</p>
68
-
65
+ <p>HyperActiveForm's form objects mimic the ActiveModel API, so they work out of the box with Rails' form helpers, and allow you to use the ActiveModel validations you already know.</p>
69
66
  <p>This allows you to only keep strictly necessary validations in the model, and have business logic validations in the form object. This is especially useful when you want different validations to be applied depending on the context.</p>
70
-
71
- <h2 id="label-Installation">Installation</h2>
72
-
73
- <p>Add this line to your application’s Gemfile:</p>
74
-
67
+ <h2 id="Installation">Installation</h2>
68
+ <p>Add this line to your application's Gemfile:</p>
75
69
  <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>hyperactiveform</span><span class='tstring_end'>&#39;</span></span>
76
70
  </code></pre>
77
-
78
71
  <p>And then execute:</p>
79
-
80
72
  <pre class="code ruby"><code class="ruby">$ bundle install
81
73
  </code></pre>
82
-
83
74
  <p>Run the install generator:</p>
84
-
85
75
  <pre class="code ruby"><code class="ruby">$ rails generate hyper_active_form:install
86
76
  </code></pre>
87
-
88
77
  <p>this will create an <code>ApplicationForm</code> class in your app/forms directory. You can use it as a base class for your form objects.</p>
89
-
90
- <h2 id="label-Generators">Generators</h2>
91
-
78
+ <h2 id="Generators">Generators</h2>
92
79
  <p>You can <a href="https://guides.rubyonrails.org/configuring.html#configuring-generators">generate</a> a form and its tests with the following command:</p>
93
-
94
80
  <pre class="code ruby"><code class="ruby">$ rails generate form FooBar
95
81
  </code></pre>
96
-
97
82
  <p>This will create the <code>FooBarForm</code></p>
98
-
99
- <h2 id="label-Usage">Usage</h2>
100
-
83
+ <h2 id="Usage">Usage</h2>
101
84
  <p>Here is an example of an <code>HyperActiveForm</code> form object:</p>
102
-
103
85
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
104
86
  <span class='comment'># proxy_for is used to delegate the model name to the class, and some methods to the object
105
87
  </span> <span class='comment'># this helps use `form_with` in views without having to specify the url
@@ -133,9 +115,7 @@
133
115
  <span class='kw'>end</span>
134
116
  <span class='kw'>end</span>
135
117
  </code></pre>
136
-
137
118
  <p>The controller would look like this:</p>
138
-
139
119
  <pre class="code ruby"><code class="ruby">class UsersController &lt; ApplicationController
140
120
 
141
121
  def edit
@@ -151,10 +131,8 @@
151
131
  end
152
132
  end
153
133
  </code></pre>
154
-
155
134
  <p>And the view would look like this:</p>
156
-
157
- <pre class="code ruby"><code class="ruby">&lt;%= form_with(model: @form) do |f| %&gt;
135
+ <pre class="code erb"><code class="erb">&lt;%= form_with(model: @form) do |f| %&gt;
158
136
  &lt;%= f.text_field :first_name %&gt;
159
137
  &lt;%= f.text_field :last_name %&gt;
160
138
  &lt;%= f.date_field :birth_date %&gt;
@@ -162,28 +140,18 @@
162
140
  &lt;%= f.submit %&gt;
163
141
  &lt;% end %&gt;
164
142
  </code></pre>
165
-
166
- <h3 id="label-Understanding+proxy_for">Understanding <code>proxy_for</code></h3>
167
-
143
+ <h3 id="Understanding__proxy_for_">Understanding <code>proxy_for</code></h3>
168
144
  <p><code>HyperActiveForm</code> mimics a model object, you can use <code>proxy_for</code> to tell it which class and object to delegate to.</p>
169
-
170
145
  <p>When using <code>form_for</code> or <code>form_with</code>, Rails will choose the URL and method based on the object, according to the persisted state of the object and its model name.</p>
171
-
172
146
  <p>The first argument of <code>proxy_for</code> is the class of the object, and the second argument is the name of the instance variable that holds the object.</p>
173
-
174
147
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
175
148
  <span class='id identifier rubyid_proxy_for'>proxy_for</span> <span class='const'>User</span><span class='comma'>,</span> <span class='symbol'>:@user</span> <span class='comment'># Will delegate to @user
176
149
  </span><span class='kw'>end</span>
177
150
  </code></pre>
178
-
179
- <p>If you pass an url and method yourself, you don’t need to use <code>proxy_for</code>.</p>
180
-
181
- <h3 id="label-Understanding+setup">Understanding <code>setup</code></h3>
182
-
151
+ <p>If you pass an url and method yourself, you don't need to use <code>proxy_for</code>.</p>
152
+ <h3 id="Understanding__setup_">Understanding <code>setup</code></h3>
183
153
  <p><code>setup</code> is called just after the form is initialized, and is used to pre-fill the form with data from the object.</p>
184
-
185
154
  <p><code>setup</code> will receive the same arguments as the initializer, so you can use it to pass any data you need to the form.</p>
186
-
187
155
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
188
156
  <span class='kw'>def</span> <span class='id identifier rubyid_setup'>setup</span><span class='lparen'>(</span><span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
189
157
  <span class='ivar'>@user</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span>
@@ -193,23 +161,19 @@
193
161
  <span class='kw'>end</span>
194
162
  <span class='kw'>end</span>
195
163
  </code></pre>
196
-
197
- <h3 id="label-Understanding+perform">Understanding <code>perform</code></h3>
198
-
164
+ <h3 id="Understanding__perform_">Understanding <code>perform</code></h3>
199
165
  <p>When using <code>submit</code> or <code>submit!</code>, <code>HyperActiveForm</code> will first assign the form attributes to the object, then perform the validations, then call <code>perform</code> on the object if the form is valid.</p>
200
-
201
166
  <p>The <code>perform</code> method is where you should do the actual form logic, like updating the object or creating a new one.</p>
202
-
203
167
  <p>If the return value of <code>perform</code> is not truthy, <code>HyperActiveForm</code> will consider the form encountered an error and <code>submit</code> will return <code>false</code>, or <code>submit!</code> will raise a <code>HyperActiveForm::FormDidNotSubmitError</code>.</p>
204
-
168
+ <p>By default, <code>submit</code> and <code>assign_form_attributes</code> overwrite attributes that are missing from the params with their default value or <code>nil</code>. Pass <code>overwrite_missing: false</code> with an explicit params hash to preserve their current values:</p>
169
+ <pre class="code ruby"><code class="ruby"><span class='ivar'>@form</span><span class='period'>.</span><span class='id identifier rubyid_submit'>submit</span><span class='lparen'>(</span><span class='lbrace'>{</span> <span class='label'>first_name:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>John</span><span class='tstring_end'>&quot;</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>overwrite_missing:</span> <span class='kw'>false</span><span class='rparen'>)</span>
170
+ <span class='ivar'>@form</span><span class='period'>.</span><span class='id identifier rubyid_assign_form_attributes'>assign_form_attributes</span><span class='lparen'>(</span><span class='lbrace'>{</span> <span class='label'>first_name:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>John</span><span class='tstring_end'>&quot;</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>overwrite_missing:</span> <span class='kw'>false</span><span class='rparen'>)</span>
171
+ </code></pre>
172
+ <p>Explicitly provided values, including <code>nil</code>, are still assigned. <code>submit!</code> accepts the same option.</p>
205
173
  <p>At any point during the form processing, you can raise <code>HyperActiveForm::CancelForm</code> to cancel the form submission, this is the same as returning <code>false</code>.</p>
206
-
207
- <h2 id="label-Understanding+add_errors_from">Understanding add_errors_from</h2>
208
-
174
+ <h2 id="Understanding_add_errors_from">Understanding add_errors_from</h2>
209
175
  <p><code>HyperActiveForm</code> provides a method to add errors from a model and apply them fo the form.</p>
210
-
211
176
  <p>This is useful when the underlying model has validations that are not set up in the form object, and you want them to be applied to the form.</p>
212
-
213
177
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'>&lt;</span> <span class='const'>ApplicationRecord</span>
214
178
  <span class='id identifier rubyid_validates'>validates</span> <span class='symbol'>:first_name</span><span class='comma'>,</span> <span class='label'>presence:</span> <span class='kw'>true</span>
215
179
  <span class='kw'>end</span>
@@ -228,17 +192,11 @@
228
192
  <span class='kw'>end</span>
229
193
  <span class='kw'>end</span>
230
194
  </code></pre>
231
-
232
- <h3 id="label-Not+all+forms+map+to+a+single+model">Not all forms map to a single model</h3>
233
-
234
- <p>The power of <code>HyperActiveForm</code> is that you can use it to create forms that don’t map to a single model.</p>
235
-
195
+ <h3 id="Not_all_forms_map_to_a_single_model">Not all forms map to a single model</h3>
196
+ <p>The power of <code>HyperActiveForm</code> is that you can use it to create forms that don't map to a single model.</p>
236
197
  <p>Some forms can be used to create several models at once. Doing so without form objects can be tedious especially with nested attributes.</p>
237
-
238
198
  <p>Some forms dont map to any model at all, like a simple contact form that only sends an email and saves nothing in the database, or a sign in form that would only validate the credentials and return the instance of the connected user.</p>
239
-
240
199
  <p>One great example of such forms are search forms. You can use a form object to encapsulate the search logic :</p>
241
-
242
200
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>UserSearchForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
243
201
  <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:name</span>
244
202
  <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:email</span>
@@ -263,9 +221,7 @@
263
221
  <span class='kw'>end</span>
264
222
  <span class='kw'>end</span>
265
223
  </code></pre>
266
-
267
224
  <p>And in the controller:</p>
268
-
269
225
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>UsersController</span> <span class='op'>&lt;</span> <span class='const'>ApplicationController</span>
270
226
  <span class='kw'>def</span> <span class='id identifier rubyid_index'>index</span>
271
227
  <span class='ivar'>@form</span> <span class='op'>=</span> <span class='const'>UserSearchForm</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
@@ -274,13 +230,9 @@
274
230
  <span class='kw'>end</span>
275
231
  <span class='kw'>end</span>
276
232
  </code></pre>
277
-
278
- <h2 id="label-Callbacks">Callbacks</h2>
279
-
233
+ <h2 id="Callbacks">Callbacks</h2>
280
234
  <p>HyperActiveForm provides callbacks for <code>assign_form_attributes</code> and <code>submit</code>.</p>
281
-
282
235
  <p>You can use these callbacks to run code before or after assigning the form attributes or before or after submitting the form.</p>
283
-
284
236
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
285
237
  <span class='comment'># ...
286
238
  </span>
@@ -295,15 +247,14 @@
295
247
  <span class='comment'># Do something before assigning the form attributes
296
248
  </span> <span class='kw'>end</span>
297
249
  <span class='kw'>end</span>
298
- </code></pre>
299
- </div></div>
250
+ </code></pre></div></div>
300
251
 
301
252
  <div id="footer">
302
- Generated on Mon Jan 13 17:26:04 2025 by
253
+ Generated on Tue Aug 25 18:22:24 2026 by
303
254
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
304
- 0.9.37 (ruby-3.3.1).
255
+ 0.9.45 (ruby-4.0.6).
305
256
  </div>
306
257
 
307
258
  </div>
308
259
  </body>
309
- </html>
260
+ </html>
data/docs/file_list.html CHANGED
@@ -2,11 +2,11 @@
2
2
  <html >
3
3
  <head>
4
4
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
5
- <meta charset="utf-8" />
5
+ <meta charset="utf-8">
6
6
 
7
- <link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" />
7
+ <link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen">
8
8
 
9
- <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
9
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen">
10
10
 
11
11
 
12
12
 
@@ -16,7 +16,7 @@
16
16
 
17
17
 
18
18
  <title>File List</title>
19
- <base id="base_target" target="_parent" />
19
+ <base id="base_target" target="_parent">
20
20
  </head>
21
21
  <body>
22
22
  <div id="content">
@@ -40,7 +40,7 @@
40
40
 
41
41
  <div id="search">
42
42
  <label for="search-class">Search:</label>
43
- <input id="search-class" type="text" />
43
+ <input id="search-class" type="text">
44
44
  </div>
45
45
  </div>
46
46
 
data/docs/frames.html CHANGED
@@ -2,7 +2,7 @@
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="utf-8">
5
- <title>Documentation by YARD 0.9.37</title>
5
+ <title>Documentation by YARD 0.9.45</title>
6
6
  </head>
7
7
  <script type="text/javascript">
8
8
  var mainUrl = 'index.html';
data/docs/index.html CHANGED
@@ -6,13 +6,13 @@
6
6
  <title>
7
7
  File: README
8
8
 
9
- &mdash; Documentation by YARD 0.9.37
9
+ &mdash; Documentation by YARD 0.9.45
10
10
 
11
11
  </title>
12
12
 
13
- <link rel="stylesheet" href="css/style.css" type="text/css" />
13
+ <link rel="stylesheet" href="css/style.css" type="text/css">
14
14
 
15
- <link rel="stylesheet" href="css/common.css" type="text/css" />
15
+ <link rel="stylesheet" href="css/common.css" type="text/css">
16
16
 
17
17
  <script type="text/javascript">
18
18
  pathId = "README";
@@ -27,6 +27,8 @@
27
27
 
28
28
  </head>
29
29
  <body>
30
+ <div id="main_progress" aria-hidden="true"></div>
31
+
30
32
  <div class="nav_wrap">
31
33
  <iframe id="nav" src="class_list.html?1"></iframe>
32
34
  <div id="resizer"></div>
@@ -57,49 +59,29 @@
57
59
  <div class="clear"></div>
58
60
  </div>
59
61
 
60
- <div id="content"><div id='filecontents'>
61
- <h1 id="label-E2-9C-A8+HyperActiveForm+-E2-9C-A8">✨ HyperActiveForm ✨</h1>
62
-
62
+ <div id="content"><div id='filecontents'><h1 id="__HyperActiveForm__">✨ HyperActiveForm ✨</h1>
63
63
  <p>HyperActiveForm is a simple form object implementation for Rails.</p>
64
-
65
64
  <p>Form objects are objects that encapsulate form logic and validations, they allow to extract the business logic out of the controller and models into specialized objects.</p>
66
-
67
- <p>HyperActiveForm’s form objects mimic the ActiveModel API, so they work out of the box with Rails’ form helpers, and allow you to use the ActiveModel validations you already know.</p>
68
-
65
+ <p>HyperActiveForm's form objects mimic the ActiveModel API, so they work out of the box with Rails' form helpers, and allow you to use the ActiveModel validations you already know.</p>
69
66
  <p>This allows you to only keep strictly necessary validations in the model, and have business logic validations in the form object. This is especially useful when you want different validations to be applied depending on the context.</p>
70
-
71
- <h2 id="label-Installation">Installation</h2>
72
-
73
- <p>Add this line to your application’s Gemfile:</p>
74
-
67
+ <h2 id="Installation">Installation</h2>
68
+ <p>Add this line to your application's Gemfile:</p>
75
69
  <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>hyperactiveform</span><span class='tstring_end'>&#39;</span></span>
76
70
  </code></pre>
77
-
78
71
  <p>And then execute:</p>
79
-
80
72
  <pre class="code ruby"><code class="ruby">$ bundle install
81
73
  </code></pre>
82
-
83
74
  <p>Run the install generator:</p>
84
-
85
75
  <pre class="code ruby"><code class="ruby">$ rails generate hyper_active_form:install
86
76
  </code></pre>
87
-
88
77
  <p>this will create an <code>ApplicationForm</code> class in your app/forms directory. You can use it as a base class for your form objects.</p>
89
-
90
- <h2 id="label-Generators">Generators</h2>
91
-
78
+ <h2 id="Generators">Generators</h2>
92
79
  <p>You can <a href="https://guides.rubyonrails.org/configuring.html#configuring-generators">generate</a> a form and its tests with the following command:</p>
93
-
94
80
  <pre class="code ruby"><code class="ruby">$ rails generate form FooBar
95
81
  </code></pre>
96
-
97
82
  <p>This will create the <code>FooBarForm</code></p>
98
-
99
- <h2 id="label-Usage">Usage</h2>
100
-
83
+ <h2 id="Usage">Usage</h2>
101
84
  <p>Here is an example of an <code>HyperActiveForm</code> form object:</p>
102
-
103
85
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
104
86
  <span class='comment'># proxy_for is used to delegate the model name to the class, and some methods to the object
105
87
  </span> <span class='comment'># this helps use `form_with` in views without having to specify the url
@@ -133,9 +115,7 @@
133
115
  <span class='kw'>end</span>
134
116
  <span class='kw'>end</span>
135
117
  </code></pre>
136
-
137
118
  <p>The controller would look like this:</p>
138
-
139
119
  <pre class="code ruby"><code class="ruby">class UsersController &lt; ApplicationController
140
120
 
141
121
  def edit
@@ -151,10 +131,8 @@
151
131
  end
152
132
  end
153
133
  </code></pre>
154
-
155
134
  <p>And the view would look like this:</p>
156
-
157
- <pre class="code ruby"><code class="ruby">&lt;%= form_with(model: @form) do |f| %&gt;
135
+ <pre class="code erb"><code class="erb">&lt;%= form_with(model: @form) do |f| %&gt;
158
136
  &lt;%= f.text_field :first_name %&gt;
159
137
  &lt;%= f.text_field :last_name %&gt;
160
138
  &lt;%= f.date_field :birth_date %&gt;
@@ -162,28 +140,18 @@
162
140
  &lt;%= f.submit %&gt;
163
141
  &lt;% end %&gt;
164
142
  </code></pre>
165
-
166
- <h3 id="label-Understanding+proxy_for">Understanding <code>proxy_for</code></h3>
167
-
143
+ <h3 id="Understanding__proxy_for_">Understanding <code>proxy_for</code></h3>
168
144
  <p><code>HyperActiveForm</code> mimics a model object, you can use <code>proxy_for</code> to tell it which class and object to delegate to.</p>
169
-
170
145
  <p>When using <code>form_for</code> or <code>form_with</code>, Rails will choose the URL and method based on the object, according to the persisted state of the object and its model name.</p>
171
-
172
146
  <p>The first argument of <code>proxy_for</code> is the class of the object, and the second argument is the name of the instance variable that holds the object.</p>
173
-
174
147
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
175
148
  <span class='id identifier rubyid_proxy_for'>proxy_for</span> <span class='const'>User</span><span class='comma'>,</span> <span class='symbol'>:@user</span> <span class='comment'># Will delegate to @user
176
149
  </span><span class='kw'>end</span>
177
150
  </code></pre>
178
-
179
- <p>If you pass an url and method yourself, you don’t need to use <code>proxy_for</code>.</p>
180
-
181
- <h3 id="label-Understanding+setup">Understanding <code>setup</code></h3>
182
-
151
+ <p>If you pass an url and method yourself, you don't need to use <code>proxy_for</code>.</p>
152
+ <h3 id="Understanding__setup_">Understanding <code>setup</code></h3>
183
153
  <p><code>setup</code> is called just after the form is initialized, and is used to pre-fill the form with data from the object.</p>
184
-
185
154
  <p><code>setup</code> will receive the same arguments as the initializer, so you can use it to pass any data you need to the form.</p>
186
-
187
155
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
188
156
  <span class='kw'>def</span> <span class='id identifier rubyid_setup'>setup</span><span class='lparen'>(</span><span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
189
157
  <span class='ivar'>@user</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span>
@@ -193,23 +161,19 @@
193
161
  <span class='kw'>end</span>
194
162
  <span class='kw'>end</span>
195
163
  </code></pre>
196
-
197
- <h3 id="label-Understanding+perform">Understanding <code>perform</code></h3>
198
-
164
+ <h3 id="Understanding__perform_">Understanding <code>perform</code></h3>
199
165
  <p>When using <code>submit</code> or <code>submit!</code>, <code>HyperActiveForm</code> will first assign the form attributes to the object, then perform the validations, then call <code>perform</code> on the object if the form is valid.</p>
200
-
201
166
  <p>The <code>perform</code> method is where you should do the actual form logic, like updating the object or creating a new one.</p>
202
-
203
167
  <p>If the return value of <code>perform</code> is not truthy, <code>HyperActiveForm</code> will consider the form encountered an error and <code>submit</code> will return <code>false</code>, or <code>submit!</code> will raise a <code>HyperActiveForm::FormDidNotSubmitError</code>.</p>
204
-
168
+ <p>By default, <code>submit</code> and <code>assign_form_attributes</code> overwrite attributes that are missing from the params with their default value or <code>nil</code>. Pass <code>overwrite_missing: false</code> with an explicit params hash to preserve their current values:</p>
169
+ <pre class="code ruby"><code class="ruby"><span class='ivar'>@form</span><span class='period'>.</span><span class='id identifier rubyid_submit'>submit</span><span class='lparen'>(</span><span class='lbrace'>{</span> <span class='label'>first_name:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>John</span><span class='tstring_end'>&quot;</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>overwrite_missing:</span> <span class='kw'>false</span><span class='rparen'>)</span>
170
+ <span class='ivar'>@form</span><span class='period'>.</span><span class='id identifier rubyid_assign_form_attributes'>assign_form_attributes</span><span class='lparen'>(</span><span class='lbrace'>{</span> <span class='label'>first_name:</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>John</span><span class='tstring_end'>&quot;</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>overwrite_missing:</span> <span class='kw'>false</span><span class='rparen'>)</span>
171
+ </code></pre>
172
+ <p>Explicitly provided values, including <code>nil</code>, are still assigned. <code>submit!</code> accepts the same option.</p>
205
173
  <p>At any point during the form processing, you can raise <code>HyperActiveForm::CancelForm</code> to cancel the form submission, this is the same as returning <code>false</code>.</p>
206
-
207
- <h2 id="label-Understanding+add_errors_from">Understanding add_errors_from</h2>
208
-
174
+ <h2 id="Understanding_add_errors_from">Understanding add_errors_from</h2>
209
175
  <p><code>HyperActiveForm</code> provides a method to add errors from a model and apply them fo the form.</p>
210
-
211
176
  <p>This is useful when the underlying model has validations that are not set up in the form object, and you want them to be applied to the form.</p>
212
-
213
177
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'>&lt;</span> <span class='const'>ApplicationRecord</span>
214
178
  <span class='id identifier rubyid_validates'>validates</span> <span class='symbol'>:first_name</span><span class='comma'>,</span> <span class='label'>presence:</span> <span class='kw'>true</span>
215
179
  <span class='kw'>end</span>
@@ -228,17 +192,11 @@
228
192
  <span class='kw'>end</span>
229
193
  <span class='kw'>end</span>
230
194
  </code></pre>
231
-
232
- <h3 id="label-Not+all+forms+map+to+a+single+model">Not all forms map to a single model</h3>
233
-
234
- <p>The power of <code>HyperActiveForm</code> is that you can use it to create forms that don’t map to a single model.</p>
235
-
195
+ <h3 id="Not_all_forms_map_to_a_single_model">Not all forms map to a single model</h3>
196
+ <p>The power of <code>HyperActiveForm</code> is that you can use it to create forms that don't map to a single model.</p>
236
197
  <p>Some forms can be used to create several models at once. Doing so without form objects can be tedious especially with nested attributes.</p>
237
-
238
198
  <p>Some forms dont map to any model at all, like a simple contact form that only sends an email and saves nothing in the database, or a sign in form that would only validate the credentials and return the instance of the connected user.</p>
239
-
240
199
  <p>One great example of such forms are search forms. You can use a form object to encapsulate the search logic :</p>
241
-
242
200
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>UserSearchForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
243
201
  <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:name</span>
244
202
  <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:email</span>
@@ -263,9 +221,7 @@
263
221
  <span class='kw'>end</span>
264
222
  <span class='kw'>end</span>
265
223
  </code></pre>
266
-
267
224
  <p>And in the controller:</p>
268
-
269
225
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>UsersController</span> <span class='op'>&lt;</span> <span class='const'>ApplicationController</span>
270
226
  <span class='kw'>def</span> <span class='id identifier rubyid_index'>index</span>
271
227
  <span class='ivar'>@form</span> <span class='op'>=</span> <span class='const'>UserSearchForm</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
@@ -274,13 +230,9 @@
274
230
  <span class='kw'>end</span>
275
231
  <span class='kw'>end</span>
276
232
  </code></pre>
277
-
278
- <h2 id="label-Callbacks">Callbacks</h2>
279
-
233
+ <h2 id="Callbacks">Callbacks</h2>
280
234
  <p>HyperActiveForm provides callbacks for <code>assign_form_attributes</code> and <code>submit</code>.</p>
281
-
282
235
  <p>You can use these callbacks to run code before or after assigning the form attributes or before or after submitting the form.</p>
283
-
284
236
  <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
285
237
  <span class='comment'># ...
286
238
  </span>
@@ -295,15 +247,14 @@
295
247
  <span class='comment'># Do something before assigning the form attributes
296
248
  </span> <span class='kw'>end</span>
297
249
  <span class='kw'>end</span>
298
- </code></pre>
299
- </div></div>
250
+ </code></pre></div></div>
300
251
 
301
252
  <div id="footer">
302
- Generated on Mon Jan 13 17:26:04 2025 by
253
+ Generated on Tue Aug 25 18:22:24 2026 by
303
254
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
304
- 0.9.37 (ruby-3.3.1).
255
+ 0.9.45 (ruby-4.0.6).
305
256
  </div>
306
257
 
307
258
  </div>
308
259
  </body>
309
- </html>
260
+ </html>