Is there any way to keep validate field optional in rails? - ruby-on-rails

I have User model which includes 7 fields. for all these fields validation is written.i have two form where i am displaying fields depend on condition. in one form i have name password and city and other form i have role,phone and name.
When i try to submit the first form i got the error which says phone and role field are required resulting into failure of form.
Is there any way by which i can submit both form without getting the validation errors ??
Note : i want my logic to be in model only.. Please help me with this problem.

You could use a conditional validation to achieve what you want:
See here: http://guides.rubyonrails.org/active_record_validations.html#conditional-validation
However, this can quickly get hard to manage. Depending on the condition you're switching on, it'd probably be a cleaner design to use a 'Form Object' which will give you more control and let you do validations without the messy conditional logic.
See section #3 of this blog post for more detail:
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
Using this pattern, you would check for your condition in the controller then determine which form object to send to the view.

Related

When to perform additional processing on the form in Rails?

I'm writing a form where some additional processing is required before sending the values. The image below depicts the simplified example.
where test_date and remind_date are saved on the server as a string of format YYYY/MM/DD.
However, the text field shown above allows users to enter an integer. The value from this field needs to be processed (basically, test_date - form_value).
I'm currently doing this conversion before sending the value to the server, within a saveForm() function that is triggered when the Submit button is pressed.
I feel like this might not be the ideal way, and wanted to get your opinions. Would it be better to handle this within the controller, by allowing a new param?
For a simple field like this you can just create another instance variable in the controller.
For more complex calculations or additional fields you could use a form object, eg: using the https://github.com/andypike/rectify gem or Reform.

How to return custom error codes in a Rails API?

Given a RESTful API, implemented in Rails, I want to include in the responses not only the errors messages, generated by ActiveModel::Validations, but also custom error codes. First of all I would like to point out that I am not talking about HTTP Status codes. I am talking about having an error of any type (from general errors like record not found to small validation errors like username can't be blank) be mapped to a unique numeric code, that is custom application-specific error codes. Let me give an example - given a list of error codes, like:
1: record not found
... some other errors
# Validation errors for User model between 1000 to 2000
1001: first name can't be blank
1002: first name must contain at least 3 characters
1003: last name can't be blank
1004: last name must contain at least 3 characters
...some other errors
If I have a form for a user and submit it with empty fields for first and last name, I want to have in the response body something like:
{error_codes: [1001, 1002, 1003, 1004]}
or something similar (for example I could have an array of error objects, each with a code, message for developer, message for user etc.). Let me give an example with the Twilio API, taken from RESTful API Design: what about errors?:
Here, 20003 is some custom Twilio-specific code. The question is - how can this be implemented in Rails? I see several difficult aspects:
how do I get a list of all possible errors that can be encountered. It is hard to get such a list even only for the validation errors, let alone the other types of errors that can occur.
how should this list be organized - maybe in a YAML file?
how do I access the list - maybe something similar to the way translations are accessed via I18n.t?
I will really appreciate any advice on the topic. Thank you.
P.S. I think this is a similar question.
ActiveModel built-in validators can be found here. Sometimes one validator can check for more than one thing and output different messages. The easiest way to see them all is, as you've guessed, in its I18n yaml file, which can be found here.
One way of doing what you want is overwriting those messages with your custom codes. Another way is passing a custom message when explicitly attaching a validator to your models.
validates :name, message: 'code:001 - my custom message'
Those two options won't help you with structure, though. You won't have a different key code on your json out of the box.
One way you can accomplish that is to can create a helper to parse the error messages and extract the codes after they have been assigned to a model instance. Something along the lines of:
def extract_error_codes(error_messages)
error_messages.map{ |message| message.match('^code:(\d+)\s-')[1] }
end
That would give you an array of error codes for that instance if you'd used the format code:001 - my custom message.
Another, much more involved way, is to tap into ActiveModel's Validator class and store an error code when a validation fails. That would require going into each validator to assign the code.

Check in view that form have errors?

(ZF2) Is there a possibility to check in view that the form is displayed for the first time, or "returned" because of errors?
Of course I can in controller base on isValid add additional variable, but I'm curious if there is a ready solution to this?
You can check if the form has been validated with $form->hasValidated() if it hasn't you prob can be pretty sure it's displayed for "the first time".
Additionally you could check if the form contains any error messages $form->getMessages(). That way you could tell if it is validated and contains validation errors.

How do i filter and validate form fields in symfony 1.4?

Im trying to integrate a content filtering API. My plan was to use pre/post validators but I've lost may way somehow.
What i need to do is send the values to the content filtering service. If the response comes back that the content has been filtered it will also return a modified value for the field (basic profanity filtering... matches are replace with asterisks). Thats all well and good i can throw validation errors no problem - simple stuff.
However i dont want just throw errors. What needs to happen is that validation errors are thrown as normal, but the values are modified in the form for re-display.
Basically if someone posts something naughty i want them to get a validation error saying their post has been modified, they can re-submit the now "clean" post, or they can go about editing it to make it clean without the word replacements.
But do clean on a validator either throws an error OR returns cleaned values, not both. How can i go about implementing both? This will be used on many different forms with many different field names, so modifying methods on the form or a form base class isnt really an option - it needs to happen in the validation sub-framework somehow.
You can adjust this plugin for your needs http://www.symfony-project.org/plugins/WebPurifyPlugin

.NET MVC Multiple Versions of Same form on One View

I have a section of my site where users can add addresses to their account. They may add as many as they need (shipping, billing, etc).
I set things up so that after an address is added, the users sees the address in an update form with a "save" and "delete" button. The user can adjust any of the addresses they have added.
The problem I am having is with validation. Let's say Line 1 is required. If I am updating the second of three addresses and leave Line 1 empty the controller is raising an error (using the same technique from Nerd Dinner, BTW). This is good. What is bad is that all of the address info on all of the addresses listed in the view, now show as values from the address where the error was raised.
I know this has something to do with model binding, but I am confused, as the form data is set up as follows:
<%= Html.TextBox("Line1", Model.Address.Line1)%>
The Model that is passed in is unique to the address we are on in the list of client addresses. I am not sure why the value in "Model.Addres.Line1" is being overridden by data in the ModelState ModelErrors Collection. I guess the default behavior is to use the values from the errors collection when they are present. This is a problem when there is more than one form on the View and the form is using the same Names for input fields as each of the other forms.
Is my only work-around to avoind the Html Helper function here and hard-code the inputs in HTML?
Correct me if you're wrong, but it sounds like all 3 addresses will have the same field names in the same form? If so, you've got bigger problems than just the validation message.
Regardless (even if you're using separate forms), I would suggest you preface the field names with the address ID:
<%= Html.TextBox("Address" + Model.Address.ID + ".Line1", Model.Address.Line1)%>
You won't be able to automatically bind (in the method parameters), but you can use UpdateModel() and set the prefix to
"Address" + Address.ID
I solved this by getting rid of the html helper methods on the form(s). If someone sees a means of keeping the helper methods please let me know.

Resources