[Development log ⑬] Do not let 0 be entered in the form !!

About the premise

Nice to meet you, my name is Irifune and I go to a programming school. This article is used for my own output by writing a record of the development of personal apps, which is a school task. If anyone has read it, I would be grateful if you could give us feedback.

We will develop a "paid leave management tool". For specifications, see Past Articles.

The app will be deployed, but it is not provided as a service. Please understand that this is just a part of self-study. Then go to the main subject.

About the form of this app

Although not mentioned in the article, I have created a form for registering paid holidays and digestion and a log to display them. Below is a photo.

This is the form. Master Taihei is trying to take a rest for some unknown reason, but this time he will miss it. スクリーンショット 2020-05-15 10.53.14.png

Next is a view to check the history of paid holidays and digestion. We have set it up because we think it will be necessary for those who enter it, such as wanting to confirm the reason for the employee's digestion, or wanting to confirm that they have registered on holiday but have not registered incorrectly. You can also delete the paid holiday registration. スクリーンショット 2020-05-17 16.05.12.png

In this digestion history, the method is set so that "grant", "digestion", and "grant and digestion" are displayed in the lower right, but the conditions here are related to this theme. Below is the method part.

holiday.rb


class Holiday < ApplicationRecord
~ Omitted ~
  def add_or_delete
    if add_day.nil?
      "#{delete_day}Digestion"
    elsif delete_day.nil?
      "#{add_day}Date grant"
    else
      "#{add_day}With date grant#{delete_day}Digestion"
    end
  end
end

The condition is whether add_day (number of days granted) or delete_day (number of days digested) is nil (empty), but the problem is the existence of "0".

0 is also data and different from nil

For example, if you register 0 for grant and 1 for digestion when you register for paid holidays, "0 day grant and 1 day digestion" will be displayed in the history, which is very clunky. To avoid this, you can change the condition of the method defined in the model file to "not nil or 0", but after that, it is troublesome to set this condition for each function to be implemented. So I want to prevent "0" from being registered when filling out the form.

Contents of this implementation

As always, the introduction has become long, but from here it is the true main subject. Make some improvements so that the grant date and digestion date columns only contain nil or a number greater than or equal to 1. Specifically, we did the following: I don't know if I needed them all.

--Validation "exclusion" --Form input restrictions --Installation of reset button

Validation

The first thing I searched for was validation because I didn't want to save 0s. When I looked it up, there was a validation called "exclusion" that did not save certain characters.

Article that I used as a reference [Rails] Complete explanation! Master how to use Rails validation!

Based on the article, I tried to validate it so that the character "0" does not enter on the grant date and digestion date.

holiday.rb


class Holiday < ApplicationRecord
  validates :reason, presence: true
  validates :add_day, exclusion: { in: [0]}
  validates :delete_day, exclusion: { in: [0]}
~ Omitted ~
end

I put it in 0 and tried it, but it seems that it is not saved. Just in case, I gave it for 10 days, but I was able to save it properly.

Form input restrictions

However, since it returns to the index without being saved, it becomes difficult for the user to understand why it was not saved. For example, I typed the grant date as 2 and the digestion date as 0, but I noticed a mistake before sending, corrected the grant date to 0 and the digestion date to 2, and pressed the send button, but it was not saved. Therefore, we will limit the form itself so that numbers less than 1 cannot be entered.

haml:holidays/new.html.haml


~ Omitted ~
            .field
              .field-label
                = f.label "Digestion days"
              .field-input
                = f.number_field :delete_day, max: "50", min: "1"
            .field
              .field-label
                = f.label "Number of days granted"
              .field-input
                = f.number_field :add_day, max: "50", min: "1"
~ Omitted below ~

Added maximum 50 and minimum 1 to number_field. This makes it possible to select only 1 to 50 with the buttons on the top and bottom of the form. In the unlikely event that you manually enter 0 and try to send, an error statement will appear due to the limitation of this form. スクリーンショット 2020-05-15 14.11.21.png

Installation of reset button

Now you can set it so that 0s are not saved and 0s cannot be submitted on the form, but just in case, we will also set up a reset button on the form. I thought that there are few people who bother to manually enter 0, so I made a mistake and it became 0, but I can not change it with the up and down buttons on the form! !! This is to prevent the situation.

haml:holidays/new.html.haml


~ Omitted ~
          .btns
            = f.button "reset", type: :reset, class: 'btn'
            = f.button "Registration", type: :submit, class: 'btn'

At first, I wrote "f.reset ..." in the same way as submit, but it didn't work, so I checked it again and solved it in the article below. The article I referred to "How to implement a reset button in Rails"

View view

I forgot to take a screenshot of the final view, so it will be in the state after CSS is applied, but it is completed as follows. スクリーンショット 2020-05-17 16.46.51.png

Today's stack

Until now, I've focused only on flashy features, so I learned that these features are also important. I think that as a developer, you need to be careful because there are no obvious errors. On the other hand, such validation may be added more and more if necessary. I also felt that the amount of work would increase too much if the amount was not kept to the necessary and sufficient amount.

Recommended Posts

[Development log ⑬] Do not let 0 be entered in the form !!
Line breaks in the entered text are not displayed in view
What to do when the changes in the Servlet are not reflected
Let me do VS Code Remote Development + Java development in Proxy environment
[Java] Do not use "+" in append!
What to do if the prefix c is not bound in JSP
[Rails / simple_format] Helper method that reflects line breaks entered in the form
The story that the Servlet could not be loaded in the Java Web application
Do not use instance variables in partial
must not return in the for statement
Do not accept System.in in gradle run
Do not return when memoizing in Ruby
Test-driven development in the functional language Elm
Do not declare variables in List in Java
[CentOS8] Since the javac command could not be used, install the java development environment.
What to do if the changes are not reflected in the jar manifest file
What to do when rails db: seed does not reflect in the database
Summarize the life cycle of Java objects to be aware of in Android development
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec