Skip to content

Commit

Permalink
Add required and type=email to forms
Browse files Browse the repository at this point in the history
This gives the browser more information to do validation (though #359
must be fixed to actually make this effective) and allows HTMLForm to
validate these fields automatically (so this allows removing most
`$form->validateXXXField()` calls).
  • Loading branch information
matthijskooijman committed Dec 7, 2020
1 parent 9b16a07 commit 692e358
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 49 deletions.
20 changes: 6 additions & 14 deletions system/datatypes/festival.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ protected function createSettingsForm(array $values=[]) {
<table>
<tr>
<td><label for="[[field-name-title]]">[[title]]</label></td>
<td><input id="[[field-name-title]]" name="[[field-name-title]]"/></td>
<td><input required id="[[field-name-title]]" name="[[field-name-title]]"/></td>
</tr>
</table>
EOF;
Expand Down Expand Up @@ -337,8 +337,6 @@ protected function settingsSaveAction(HyphaRequest $request) {

// create form
$form = $this->createSettingsForm($request->getPostData());

$form->validateRequiredField(self::FIELD_NAME_TITLE);
$form->validate();

// process form if it was posted
Expand Down Expand Up @@ -373,12 +371,12 @@ private function createSignupForm(array $values = []) {
<table class="festivalForm">
<tr>
<th><label for="[[field-name-name]]">[[name]]</label></th>
<td><input type="text" name="[[field-name-name]]"></td>
<td><input required type="text" name="[[field-name-name]]"></td>
<td>*</td>
</tr>
<tr>
<th><label for="[[field-name-email]]">[[email]]</label></th>
<td><input type="text" name="[[field-name-email]]"></td>
<td><input required type="email" name="[[field-name-email]]"></td>
<td>*</td>
</tr>
<tr>
Expand Down Expand Up @@ -439,9 +437,6 @@ protected function signupAction(HyphaRequest $request) {
// create form
$form = $this->createSignupForm($request->getPostData());

$form->validateRequiredField(self::FIELD_NAME_NAME);
$form->validateRequiredField(self::FIELD_NAME_EMAIL);
$form->validateEmailField(self::FIELD_NAME_EMAIL);
$form->validateMoneyField(self::FIELD_NAME_AMOUNT);
$form->validate();

Expand Down Expand Up @@ -636,12 +631,12 @@ private function createContributionForm(array $values = []) {
<table class="festivalForm">
<tr>
<th><label for="[[field-name-name]]">[[name]]</label></th>
<td><input type="text" name="[[field-name-name]]"/></td>
<td><input required type="text" name="[[field-name-name]]"/></td>
<td>*</td>
</tr>
<tr>
<th><label for="[[field-name-title]]">[[title]]</label></th>
<td><input type="text" name="[[field-name-title]]"/></td>
<td><input required type="text" name="[[field-name-title]]"/></td>
<td>*</td>
</tr>
<tr>
Expand All @@ -664,7 +659,7 @@ private function createContributionForm(array $values = []) {
</tr>
<tr>
<th><label for="[[field-name-category]]">[[category]]</label></th>
<td><select name="[[field-name-category]]">
<td><select required name="[[field-name-category]]">
<option disabled="disabled" selected="selected">select</option>
<option value="lecture">Lecture</option>
<option value="workshop">Workshop</option>
Expand Down Expand Up @@ -769,9 +764,6 @@ protected function contributionSaveAction(HyphaRequest $request) {

$form = $this->createContributionForm($request->getPostData());

$form->validateRequiredField(self::FIELD_NAME_NAME);
$form->validateRequiredField(self::FIELD_NAME_TITLE);
$form->validateRequiredField(self::FIELD_NAME_CATEGORY);
$form->validate();

if (array_key_exists('image_upload', $_FILES))
Expand Down
19 changes: 5 additions & 14 deletions system/datatypes/mailinglist.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,6 @@ protected function editAction(HyphaRequest $request) {
$form = $this->createEditForm($request->getPostData());

// validate form
$form->validateRequiredField(self::FIELD_NAME_SENDER_EMAIL);
$form->validateRequiredField(self::FIELD_NAME_SENDER_NAME);
$form->validateEmailField(self::FIELD_NAME_SENDER_EMAIL);
$form->validate();

// return form if there are errors
Expand Down Expand Up @@ -323,8 +320,6 @@ protected function subscribeAction(HyphaRequest $request) {
$form = $this->createSubscribeForm($request->getPostData());

// validate form
$form->validateRequiredField(self::FIELD_NAME_EMAIL);
$form->validateEmailField(self::FIELD_NAME_EMAIL);
$form->validate();

// process form if there are no errors
Expand Down Expand Up @@ -758,8 +753,6 @@ protected function mailingNewAction(HyphaRequest $request) {
// create form
$form = $this->createMailingForm($request->getPostData());

$form->validateRequiredField(self::FIELD_NAME_SUBJECT);
$form->validateRequiredField(self::FIELD_NAME_MESSAGE);
$form->validate();

if (!$form->isValid()) {
Expand Down Expand Up @@ -861,8 +854,6 @@ protected function mailingEditAction(HyphaRequest $request) {
$form = $this->createMailingForm($request->getPostData());

// process form if it was posted
$form->validateRequiredField(self::FIELD_NAME_SUBJECT);
$form->validateRequiredField(self::FIELD_NAME_MESSAGE);
$form->validate();

if (!$form->isValid()) {
Expand Down Expand Up @@ -894,7 +885,7 @@ protected function mailingEditAction(HyphaRequest $request) {
protected function createSubscribeForm(array $values = []) {
$html = <<<EOF
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
<label for="[[field-name-email]]">[[email]]</label>: <input type="text" id="[[field-name-email]]" name="[[field-name-email]]" placeholder="[[email]]" />
<label for="[[field-name-email]]">[[email]]</label>: <input required type="email" id="[[field-name-email]]" name="[[field-name-email]]" placeholder="[[email]]" />
</div>
EOF;

Expand All @@ -916,8 +907,8 @@ protected function createEditForm(array $values = []) {
$html = <<<EOF
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
<strong><label for="[[field-name-page-name]]">[[title]]</label></strong> <input type="text" id="[[field-name-page-name]]" name="[[field-name-page-name]]" />
<strong><label for="[[field-name-sender-name]]"> [[sender-name]] </label></strong><input type="text" id="[[field-name-sender-name]]" name="[[field-name-sender-name]]" />
<strong><label for="[[field-name-sender-email]]"> [[sender-email]] </label></strong><input type="text" id="[[field-name-sender-email]]" name="[[field-name-sender-email]]" />
<strong><label for="[[field-name-sender-name]]"> [[sender-name]] </label></strong><input required type="text" id="[[field-name-sender-name]]" name="[[field-name-sender-name]]" />
<strong><label for="[[field-name-sender-email]]"> [[sender-email]] </label></strong><input required type="email" id="[[field-name-sender-email]]" name="[[field-name-sender-email]]" />
<strong><label for="[[field-name-private-page]]"> [[private-page]] </label></strong><input type="checkbox" name="[[field-name-private-page]]" id="[[field-name-private-page]]" />
</div>
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
Expand Down Expand Up @@ -956,10 +947,10 @@ protected function createEditForm(array $values = []) {
protected function createMailingForm(array $values = []) {
$html = <<<EOF
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
<label for="[[field-name-subject]]"> [[subject]] </label> <input type="text" id="[[field-name-subject]]" name="[[field-name-subject]]" />
<label for="[[field-name-subject]]"> [[subject]] </label> <input required type="text" id="[[field-name-subject]]" name="[[field-name-subject]]" />
</div>
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
<strong><label for="[[field-name-message]]"> [[message]] </label></strong><editor id="[[field-name-message]]" name="[[field-name-message]]"></editor>
<strong><label for="[[field-name-message]]"> [[message]] </label></strong><editor required id="[[field-name-message]]" name="[[field-name-message]]"></editor>
</div>
EOF;

Expand Down
18 changes: 3 additions & 15 deletions system/datatypes/peer_reviewed_article.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,12 +829,6 @@ protected function discussionAction(HyphaRequest $request) {

// create form to start a discussion
$form = $this->createCommentForm($type, $request->getPostData());
if (!$review && !isUser()) {
$form->validateRequiredField(self::FIELD_NAME_DISCUSSION_COMMENTER_NAME . $dataFieldSuffix);
$form->validateRequiredField(self::FIELD_NAME_DISCUSSION_COMMENTER_EMAIL . $dataFieldSuffix);
$form->validateEmailField(self::FIELD_NAME_DISCUSSION_COMMENTER_EMAIL . $dataFieldSuffix);
}
$form->validateRequiredField(self::FIELD_NAME_DISCUSSION_COMMENT . $dataFieldSuffix);
$form->validate();

// process form if it was posted
Expand Down Expand Up @@ -927,12 +921,6 @@ protected function discussionCommentAction(HyphaRequest $request) {

// create form to comment on a discussion
$form = $this->createCommentForm($type, $request->getPostData(), $discussion);
if (!$review && !isUser()) {
$form->validateRequiredField(self::FIELD_NAME_DISCUSSION_COMMENTER_NAME . $dataFieldSuffix);
$form->validateRequiredField(self::FIELD_NAME_DISCUSSION_COMMENTER_EMAIL . $dataFieldSuffix);
$form->validateEmailField(self::FIELD_NAME_DISCUSSION_COMMENTER_EMAIL . $dataFieldSuffix);
}
$form->validateRequiredField(self::FIELD_NAME_DISCUSSION_COMMENT . $dataFieldSuffix);
$form->validate();

// process form if it was posted
Expand Down Expand Up @@ -1747,7 +1735,7 @@ protected function createCommentForm($type, array $values = [], HyphaDomElement
$html = <<<EOF
<div class="new-comment [[type]]">
<div>
<strong><label for="[[field-name-comment]]">[[comment]]</label></strong><textarea name="[[field-name-comment]]" id="[[field-name-comment]]"></textarea>
<strong><label for="[[field-name-comment]]">[[comment]]</label></strong><textarea required name="[[field-name-comment]]" id="[[field-name-comment]]"></textarea>
</div>
EOF;

Expand All @@ -1771,10 +1759,10 @@ protected function createCommentForm($type, array $values = [], HyphaDomElement
// only non-users need to enter their name
$html .= <<<EOF
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
<strong><label for="[[field-name-name]]">[[name]]</label></strong><div class="label_suffix">[[un-anonymous]]</div> <input type="text" id="[[field-name-name]]" name="[[field-name-name]]" />
<strong><label for="[[field-name-name]]">[[name]]</label></strong><div class="label_suffix">[[un-anonymous]]</div> <input required type="text" id="[[field-name-name]]" name="[[field-name-name]]" />
</div>
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
<strong><label for="[[field-name-email]]">[[email]]</label></strong> <input type="text" id="[[field-name-email]]" name="[[field-name-email]]" />
<strong><label for="[[field-name-email]]">[[email]]</label></strong> <input required type="email" id="[[field-name-email]]" name="[[field-name-email]]" />
</div>
<div class="section" style="padding:5px; margin-bottom:5px; position:relative;">
<strong><label for="[[field-name-subscribe]]">[[subscribe]]</label></strong> <input type="checkbox" id="[[field-name-subscribe]]" name="[[field-name-subscribe]]" /> [[info-button-subscribe]]
Expand Down
9 changes: 3 additions & 6 deletions system/datatypes/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function createEditForm(array $values = []) {
$html = <<<EOF
<div class="section">
<label for="[[field-name-title]]">[[title]]</label>
<input type="text" id="[[field-name-title]]" name="[[field-name-title]]" onblur="validatePagename(this);" onkeyup="validatePagename(this);" />
<input required type="text" id="[[field-name-title]]" name="[[field-name-title]]" onblur="validatePagename(this);" onkeyup="validatePagename(this);" />
<input type="checkbox" id="[[field-name-private]]" name="[[field-name-private]]" />
<label for="[[field-name-private]]">[[private]]</label>
</div>
Expand Down Expand Up @@ -155,7 +155,6 @@ protected function editAction(HyphaRequest $request) {
$form = $this->createEditForm($request->getPostData());

// validate
$form->validateRequiredField(self::FIELD_NAME_PAGE_NAME);
$form->validate();

// process form if it was posted
Expand Down Expand Up @@ -220,9 +219,7 @@ protected function translateAction(HyphaRequest $request) {
$form = $this->createTranslationForm($request->getPostData());

// validate
$form->validateRequiredField(self::FIELD_NAME_PAGE_NAME);
// TODO: validate that the language is a valid one
$form->validateRequiredField(self::FIELD_NAME_LANGUAGE);
$form->validate();

// process form if it was posted
Expand Down Expand Up @@ -282,11 +279,11 @@ protected function createTranslationForm(array $values = []) {
$html = <<<EOF
<div class="section">
<label for="[[field-name-language]]">[[language]]</label>
<select id="[[field-name-language]]" name="[[field-name-language]]">[[option-list-language]]</select>
<select required id="[[field-name-language]]" name="[[field-name-language]]">[[option-list-language]]</select>
</div>
<div class="section">
<label for="[[field-name-title]]">[[title]]</label>
<input type="text" id="[[field-name-title]]" name="[[field-name-title]]" onblur="validatePagename(this);" onkeyup="validatePagename(this);" />
<input required type="text" id="[[field-name-title]]" name="[[field-name-title]]" onblur="validatePagename(this);" onkeyup="validatePagename(this);" />
</div>
<editor name="[[field-name-content]]"></editor>
EOF;
Expand Down

0 comments on commit 692e358

Please sign in to comment.