Created
April 11, 2025 21:03
-
-
Save Megafry/1e26e4e563f8ce36e1cd4f467d6c89ee to your computer and use it in GitHub Desktop.
CraftCMS form to update single via Front-End User login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "layouts/default" %} | |
{% block main %} | |
<div class="container"> | |
<div class="content"> | |
<h1>Status page</h1> | |
{% for type, message in craft.app.session.getAllFlashes() %} | |
{{ tag('div', { | |
class: [ | |
'flash-message', | |
'flash-message--' ~ type | |
], | |
role: 'alert', | |
aria: { | |
live: 'assertive', | |
atomic: 'true' | |
}, | |
text: message | |
})}} | |
{% endfor %} | |
<form class="form" method="post" accept-charset="UTF-8"> | |
<fieldset> | |
<legend class="h6 visually-hidden">Seaside Resort Status Update</legend> | |
{{ csrfInput() }} | |
{{ actionInput('lifeguard/lifeguard/update') }} | |
<div class="field"> | |
<label class="label" for="temperature">Current Temperature (°C):</label> | |
{{ input('number', 'fields[temperature]', status.temperature, { | |
id: 'temperature', | |
required: true, | |
class: [ | |
'input', | |
'input--number' | |
] | |
}) }} | |
</div> | |
<div class="field"> | |
<label class="label" for="waterTemperature">Water Temperature (°C):</label> | |
{{ input('number', 'fields[waterTemperature]', status.waterTemperature, { | |
id: 'waterTemperature', | |
required: true, | |
class: [ | |
'input', | |
'input--number' | |
] | |
}) }} | |
</div> | |
<div class="field"> | |
<label>Resort Status:</label> | |
<div class="radio-group"> | |
{% for option in status.resortStatus.options %} | |
<div class="radio-option"> | |
{{ input('radio', 'fields[resortStatus]', option.value, { | |
id: option.value | id, | |
required: true, | |
checked: option.selected, | |
class: [ | |
'input', | |
'input--radio' | |
] | |
}) }} | |
{{ tag('label', { | |
class: 'label', | |
for: option.value | id, | |
text: option.label | |
})}} | |
</div> | |
{% endfor %} | |
</div> | |
</div> | |
<div class="field"> | |
<label class="label" for="notes">Additional Notes:</label> | |
{{ tag('textarea', { | |
class: 'input input--textarea', | |
id: 'notes', | |
name: 'fields[notes]', | |
rows: 3, | |
text: status.notes | |
})}} | |
</div> | |
<button type="submit" class="button">Update Resort Status</button> | |
</fieldset> | |
</class="label"form> | |
</div> | |
</div> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\lifeguard\controllers; | |
use Craft; | |
use craft\elements\Entry; | |
use craft\web\Controller; | |
use yii\web\Response; | |
/** | |
* Lifeguard controller | |
*/ | |
class LifeguardController extends Controller | |
{ | |
public $defaultAction = 'index'; | |
protected array|int|bool $allowAnonymous = self::ALLOW_ANONYMOUS_NEVER; | |
/** | |
* index | |
*/ | |
public function actionIndex(): Response | |
{ | |
$this->requireLogin(); | |
$status = Entry::find() | |
->section('status') | |
->one(); | |
$variables = [ | |
'status' => $status | |
]; | |
return $this->renderTemplate('_lifeguard/index', $variables); | |
} | |
/** | |
* update action | |
* | |
* @return Response | |
*/ | |
public function actionUpdate(): Response | |
{ | |
$this->requirePostRequest(); | |
$this->requireLogin(); | |
$request = $this->request; | |
$fieldValuesFromRequest = $request->getBodyParam('fields'); | |
$status = Entry::find() | |
->section('status') | |
->one(); | |
$status->setFieldValues($fieldValuesFromRequest); | |
if (!Craft::$app->getElements()->saveElement($status)) { | |
Craft::$app->getSession()->setError(Craft::t('site', 'error while saving contact infos.')); | |
} | |
Craft::$app->getSession()->setNotice(Craft::t('site', 'Information has been saved.')); | |
return $this->redirectToPostedUrl(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'lifeguard-admin' => 'lifeguard/lifeguard' | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment