วันอาทิตย์ที่ 16 ตุลาคม พ.ศ. 2554

Ruby on Rails - Validating Values Passed to Models

Today I will continue from the RoR article I wrote about the Relationship Between Model. After the model relationship is defined, it is important to check if values passed to the model are valid. In the RoR, we can validate the values by using the validates method provided by the RoR as the example shown below.


class Quote < ActiveRecord::Base
  validates :iname, :presence => true
  validates :bid, :presence => true

  has_many :quote_news
end  


The validates method check whether a given field, :iname and :bid for example, has a value or not. If not the error will be raised to the application and we can check the error in the errors field or the model.

Beside the :presence validation helper, the RoR also provide the other helpers for checking values sent to the model. Please refer to this link for the helpers available in the RoR.

http://guides.rubyonrails.org/active_record_validations_callbacks.html

This is quite handy feature, reducing the times required to write the code for validating the values. In past, I developed the web applications by using just PHP, no web framework. It is quite painful and take sometime to write these things and fix bugs made by my mistake. Beside it helps to reduce the time and bugs in developing these validation methods, one benefit is maintainability of the code base. Since the code we need to write for these things are provided by the framework, we have not to maintain them also test when there are changes in those validation methods.

Referring to the project I'm building, Real-Time Quotes, all the fields should have a value and its type should follow this specification.

The iname field is a name of an instrument in a quote.
The bid field is a bid price of an instrument in a quote.
The ask field is an ask price of an instrument in a quote.
The trdprc field is a trade price of an instrument in a quote.

Therefore, three validations need to be added to the Quote model class to check whether a value is presence and a floating point number if it is a type of price field as the example shown below.


class Quote < ActiveRecord::Base
  validates :iname, :presence => true
  validates :bid, :presence => true, :numericality => true
  validates :ask, :presence => true, :numericality => true
  validates :trdprc, :presence => true, :numericality => true
  
  has_many :quote_news
end


From the code above, you will see that the price type fields can be validated if they are a floating by using the :numericality helper. Try to put a non-numeric character to the bid, ask or price will produce the error as shown below.



The error dialog displayed is the default one generated by the RoR. We can change the style of the dialog by  modifying the style sheet for the error in the /assets/scaffolds.css.css file. For example, if you like to change the background color of the field when an error occurs, modifying the .field_with_errors style as shown below.



.field_with_errors {
  padding: 2px;
  background-color: orange;
  display: table; }


The error will be displayed with the background color in orange instead as illustrated below.


You can also add other attributes, border-color or font-size for example, to the style sheet to make a look-and-feel you prefer. That's all I have learned today. Hope you enjoy my post and have a nice day :)

ไม่มีความคิดเห็น:

แสดงความคิดเห็น