1

I am using this to learn play-framework with slick. I have this url form in my controller

    val urlForm = Form(
          mapping(
            "id" -> ignored(Some(0): Option[Long]),
            "link" -> nonEmptyText,
            "shortLink" -> nonEmptyText)(Url.apply)(Url.unapply))

On the view side, it is rendered as

@form(routes.Application.save()) {  
                    <fieldset>@formElements(urlForm)</fieldset>

                    <div class="actions">
                        <input type="submit" value="Create this url" class="btn btn-primary"> or
                        <a href="@routes.Application.list()" class="btn btn-default">Cancel</a>
                    </div>          
                }

I have two questions:
1- How can I fill shortLink from data filled in link field and make it hidden for user.
2- How can I validate if data filled in link field is a valid URL

Things I have tried:
1- Reading this and trying to use it in my project, but couldn't apply it.
2- Seeing answers on how to make fields hidden, but didn't work on this project because the view is just creating all the fields from formElements(urlForm) making me unable to set them hidden manually.

saadi
  • 646
  • 6
  • 29

1 Answers1

0

Three things things you asked:

Hidden Field and using the values that was passed from controller. If you want to make it hidden in general, it relates to HTML, and you can do this in your views file:

<input type="hidden" id="hiddenField" value="@whateverThatPassedToViews"> 

Then from this point it is all "dot notation" principle, like if you want to hide the students name and the field fullname is in the Student case class, then when you pass the list of users, you use @student.fullname for the value.

How to check if the data is a valid URL: Two ways you can do this:

  1. Pass it to the controller when the form is submitted: Basically make it a NonEmptyString and pass it to the controller, and check for the validity of the URL from there.

  2. Use Javascript: You can use Javascript, to check for the validity of the URL.

For checking the correctness of the URL, you can look at this answer here.

o-0
  • 1,713
  • 14
  • 29
  • All my fields are rendered through this one line `
    @formElements(urlForm)
    `, so how can I access individual fields like you told above? Like, by using dot notation? Also, as I asked, what can I do to fill the `shortLink` field from the value filled in `link` field? The transformation function for `link` to `shortLink` is in controller, but the view won't pass the form to controller unless all fields are filled.
    – saadi Sep 18 '18 at 05:25
  • The `@formElements.somefield` should access the field of the form. You can pass the pre calculated field (in your case `shortLink`) and pass it with the form to the views: `whatEverForm.fill(whatEverData)`. So Here you fill the form with whatever data you want to filled it in. If this is the answer, let me know, so I update the above answer. – o-0 Sep 18 '18 at 06:12
  • I think I didn't quite ask my question well, how can I fill the `shortLink` field without knowing what's filled in `link` field? I can not pass `shortLink` data without knowing `link` data. I can only fill `shortLink` AFTER somehow getting the data of `link` field in controller. `shortLink` field has nothing to do with user or views, I only have to pass it to view (possibly as a hidden field) because the sql database model has a `shortLink` column which I need to fill with the processed data from `link` field. Is it making sense? – saadi Sep 18 '18 at 07:04