Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actionMyControllerAction()
- {
- $this->requirePostRequest();
- // Build a model of the data you are submitting
- $model = new MyModel();
- $model->someOptionalAttribute = Craft::$app->getRequest()->getBodyParam('someOptionalAttribute');
- // The `getRequiredBodyParam` method can help validate required fields
- $model->someRequiredAttribute = Craft::$app->getRequest()->getRequiredBodyParam('someRequiredAttribute');
- // After you prepare the model, hand things off to a service
- // to validate and/or save things to the database
- // The "saveThisModel" method can run any additional
- // validation you need to run, add errors to the model,
- // and return `false` if validation fails
- if (!Plugin::getInstance()->myService->saveThisModel($model)) {
- Craft::$app->getSession()->setError(Craft::t('my-plugin', 'Unable to save item.'));
- // This is where you name the thing you are returning to your template with errors.
- // In this example, the 'myModel' variable would be available to your submitted template
- Craft::$app->getUrlManager()->setRouteParams([
- 'myModel' => $model
- ]);
- return null;
- }
- Craft::$app->getSession()->setNotice(Craft::t('my-plugin', 'Item saved.'));
- // Redirects to wherever the submitted 'redirect' input points to
- return $this->redirectToPostedUrl();
- }
- {{ dump(myModel.getErrors()) }}
- {{ dump(myModel.getError('someOptionalAttribute')) }}
- {{ forms.textField({
- label: "Title"|t('app'),
- siteId: category.siteId,
- id: 'title',
- name: 'title',
- value: category.title,
- errors: category.getErrors('title'),
- required: true,
- maxlength: 255
- }) }}
- if (!Craft::$app->getElements()->saveElement($category)) {
- // ...
- Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save category.'));
- // Send the category back to the template
- Craft::$app->getUrlManager()->setRouteParams([
- 'category' => $category
- ]);
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement