Problem/Motivation

YouTube Link field has no validation.

Steps to reproduce

Add any value on YouTube link, it will be accepted.

Proposed resolution

Form validate.

Issue fork modal_page-3333627

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

diegors created an issue. See original summary.

diegors’s picture

Assigned: diegors » Unassigned
Status: Active » Needs review

Added the form validation to accept URL from YouTube in the formats:

https://www.youtube.com/watch?v=<VIDEO_ID>
https://youtu.be/<VIDEO_ID>

Moving to NR.

jasjeet kaur brar’s picture

StatusFileSize
new90.6 KB
new75.19 KB

I applied the patch on Drupal version 9.5.1 with PHP 8.2, it's fixed the problem. Please check attachments.

renatog’s picture

Status: Needs review » Needs work

Good catch @diegors! Thanks a lot

Thanks for the review @jasjeet-kaur-brar

if (!$validModalVideoLink) {
      $form_state->setErrorByName('modal_video_link',
      t("Invalid video YouTube URL"));
      ;
    }

I think on this case we could use $this->t() that's right?

Example: $this->t('Invalid video YouTube URL'));

atul_ghate’s picture

Assigned: Unassigned » atul_ghate

I will work on this

atul_ghate’s picture

Assigned: atul_ghate » Unassigned
Status: Needs work » Needs review
StatusFileSize
new337 bytes
new1.46 KB

The patch has been rerolled, please review it

renatog’s picture

Status: Needs review » Needs work

Thank you so much team!

On this case we're using #element_validate and technically this is used to validate this specific element (field) on form submit

The good side is that this is used to validate this field in all entities that this field is being applied

But in the other hand, this field will be used only on Modal entity so if we do this for all fields on Modal entity we'll have a lot of methods to validate all elements

Since this field will be used only on Modal entity, I recommend apply the validation inside of method validateForm as the other fields is being validated as well

diegors’s picture

Assigned: Unassigned » diegors

Make sense @RenatoG, I will work on that.

diegors’s picture

Assigned: diegors » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.86 KB
new1.18 KB

I did changes as @RenatoG suggested in #8

renatog’s picture

Status: Needs review » Needs work

Thanks a lot @diegors

+++ b/src/Form/ModalForm.php
@@ -1111,6 +1111,26 @@ class ModalForm extends EntityForm {
+    if ($values['modal_video_link']) {

I recommend using
if (!empty($values['modal_video_link'])) { since the empty has more verifications, like, 0, NULL, FALSE, empty strings "", etc

+++ b/src/Form/ModalForm.php
@@ -1111,6 +1111,26 @@ class ModalForm extends EntityForm {
+      if (!$validModalVideoLink) {
+        $form_state->setErrorByName(
+          'modal_video_link',
+          $this->t('Invalid video YouTube URL')
+        );
+        ;
+      }

Here seems that there is a typo os char

;

Current:

if (!$validModalVideoLink) {
    $form_state->setErrorByName(
      'modal_video_link',
      $this->t('Invalid video YouTube URL')
    );
    ;
  }

Expected:

if (!$validModalVideoLink) {
    $form_state->setErrorByName(
      'modal_video_link',
      $this->t('Invalid video YouTube URL')
    );
  }

And the last (but not least):
I think we can create a separated method with this validation instead of putting the entire logic inside of "validateForm". This is because with the time, adding 10 validations for example this method will be huge and SonarQube will open points (because it recommends methods with few lines instead of long methods)

Recommendation:
Inside of validateForm call a helper, for example:

Helper class called: "ModalFieldValidationHelper.php"
Inside of this class have a method called: "validateVideoLink"
This method will have this logic on validation of "video link" and set the "setErrorByName" if necessary

So inside of validateForm we'll have only 1 line, E.g:

public function validateForm(array &$form, FormStateInterface $form_state) {

  $values = $form_state->getValues();

  // Verify if "Video Link" is a valid YouTube URL.
  $this->modalFieldValidations->validateVideoLink($values);

Is that makes sense?

diegors’s picture

Status: Needs work » Needs review
StatusFileSize
new4.64 KB
new4.48 KB

Thanks a lot @RenatoG, did the changes suggested in #11, but I created an ModalFieldValidationService instead of a ModalFieldValidationHelper to pass to in the create method , I am not sure if that is ok, but here is my contribution.

renatog’s picture

I created an ModalFieldValidationService instead of a ModalFieldValidationHelper to pass to in the create method , I am not sure if that is ok

Yeah, for sure! That's awesome!

The file name was just an example, your service is perfect

renatog’s picture

Status: Needs review » Needs work
Issue tags: +Novice

Small adjustments:

+++ b/src/Service/ModalFieldValidationService.php
@@ -0,0 +1,58 @@
+   * @return bool
+   *   Return TRUE if is a valid YouTUbe url, or NO otherwise.

Since will return "bool" the comment can be updated. Because says that will return TRUE or NO instead of FALSE

Note that "U" of YouTube is also in Caps. I think can be something like:
Return TRUE if is a valid YouTube url, or FALSE otherwise.

I'm putting the tag "Novice" since is a small adjustment

sahilgidwani’s picture

Assigned: Unassigned » sahilgidwani
sahilgidwani’s picture

StatusFileSize
new4.15 KB
new2.98 KB

I did changes @RenatoG suggested.

sahilgidwani’s picture

Status: Needs work » Needs review
renatog’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Novice

It's working fine! #RTBC

  • diegors authored 5f27f4f3 on 5.0.x
    Issue #3333627 by diegors, SahilGidwani, atul ghate, Jasjeet Kaur Brar,...
renatog’s picture

Status: Reviewed & tested by the community » Fixed

Moved to the dev branch 5.0.x

Thanks a lot @diegors for providing a great solution and thanks everyone for your contribution helping testing that as well

Great job!

sahilgidwani’s picture

Assigned: sahilgidwani » Unassigned

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.