Custom validation in mvc - asp.net-mvc

I have a dropdown list conatining some options. The last one is "other". When the user chooses the other option a textbox appears next to the drop down list. When the user chooses "other" he must enter a value into the textbox. Both, the dropdown list and the textbox, point to one single property of the the model.
How do I achieve this using custom validation? what is the best solution?

It seems what you're looking for is conditional validation. A very nice example is already mentioned in another question where you could create a custom RequiredIf attribute.
The topic has also been addressed frequently on stackoverflow.com

Related

Creating Managed Lists Like Versions and Components

In JIRA, you can create custom fields which is good, but I feel that their field types are relatively weak.
For example, you can choose the 'Label' which you can add custom labels. That is good, but I need to be able to choose from a list that already exists and do not want users to be able to create new ones freely.
So that leaves us with the option of using a Single Selection List or a Multi-Selection List. The problem with this is the limited way of using it... drop-down. I wish you could populate an item similar to the way you can add a predefined 'Version' or 'Component'. The drop-down feels primitive.
So my question is: Are there a way of changing the selection interface of a list so that it mimics the way you would set a 'Target Version' for example (simply type it in freely and you can remove an item by clicking on the 'X' button).
Or if not, is there a way to create completely separate groups so that they can behave exactly like the 'Version' and the respective 'Version Picker' field.
I want my 'Customer' field to look and behave exactly like my 'Version' fields... not drop-down like.
Versions and Components fields use autocomplete renderer, but
For custom fields of type Multi Select, only the Select List Renderer is available. Furthermore, when modifying a field configuration, you will not be able to configure a Multi Select custom field's renderer.
see this link https://answers.atlassian.com/questions/16669/looking-for-a-custom-field-similar-to-the-component-field

Create new field in SugarCRM

I'm trying to add new field in SugarFields and i have many problems. I have to create a new field which is combination of relate field and multienum, when user clicks to select one/many item(s) the list will be loaded to dropdown list (multiselect). I have copied and modified relate field but i can't see how to get data from pop-up window
Have anyone tried this before? Please give me a clue. I'm using SugarCE-6.5.13
Thank you.
I can't upload image so this is a link from mediafire :(
http://www.mediafire.com/view/523c1kwewld18hn#
You do not need to create a custom field. What you actually want to do is add some custom JavaScript that will make an ajax call to the server to obtain the values for the multi-select from the relate.
There are two different types of data for a relate field. Data when you type ahead and data that is returned from a popup.
For the popup: you will need to override the function set_return(popup_reply_data)
I can't remember off the top of my head, how to handle the type ahead.

groovy: save gsp state

I have 2 gsp's say, "a.gsp" and "b.gsp".
"a.gsp" have combobox, from which user can select options.
My question is that, if the user shift to "b.gsp" and then come back to "a.gsp", the combobox selected option should be still there.
How to achieve this ?
You can do multiple things to achieve this. Depending on your requirements you may:
Use javascript and cookies.
Pass the value selected in the combobox into the controller when you naviate to b.gsp and into a hidden field in b.gsp, but then you have to pass it back to the controller once more when you want to see a.gsp.
Pass the selected value inside session/flash scope
From your question it sounds like you are not fully embracing Grails' MVC architecture and using controllers correctly to prepare data and pass structured data through to your view.
To begin with, rename your controller actions and views to something meaningful rather than a,b. Even if just for testing a small sample, as taking shortcuts can lead to long term bad habits...
Secondly, if you are using a tag then you would use the value attribute to indicate what should be selected.
Read about the tag and its attributes here : http://grails.org/doc/2.1.0/ref/Tags/select.html
Give us more details and perhaps we can help.
Thanks for the clarification, so I'm assuming that there could be any number of ways a user might leave the current a.gsp page and come back, but whatever happens you want the browser to remember the selected option. In this case I would use the jQuery cookie library, its very small, won't impact performance as you're doing very litte work and should be very quick for you to setup.. See: https://github.com/carhartl/jquery-cookie
Set a cookie in an onChange handler based on your selects val() value.
When your page loads (document ready), if you get a value when reading the cookie, then try and set the select value.

ASP.NET MVC Using view models in different views

I have 2 customer views one for create and one for edit. I am using the same customer view model for both. I want to make the 'customer no field' required on the add, but not the edit.
If I put the requiredfield attribute on the view model property then both views flag 'Customer No' as required (as you would expect).
Is there a built in solution to get around this problem or Am I going to have to create 2 seperate view models, one with the attribute and one without.
Thanks
This is similar to this question.
I would strongly advise you to tailor 2 View Models for edit and create actions. It is a lot cleaner. The last answer in the link I gave you makes a workaround and disables the errors on the ModelState.
How is Customer No. required on create but not edit?
If you create it, it requires the number, and when you edit it, the number is still there.
Do you mean they can remove the customer number on edit? Or do you mean you want Customer no. to be non-editable on edit?
If it's the latter, then you can keep customer no as required, you just display the customer no in your edit view (not in a textbox) and use a hidden input to contain the number so it gets posted.
Just a concept type of suggestion. Remove the required validation attribute from your model. In your controller, make the parameter optional and depending on which Action (Edit or Create), you'll manually add in some type of validation.
The jQuery validation can be used to validate from the client based on the input if you go towards the manual route.

How to make validation optional for a complex type in asp.net mvc?

I want to display an editor for a type User. User contains a field Address of type Address. I made an editor template for the type Address so that it is reusable.
I don't want the field Address to be required for creating a user. But some fields are required for Address, for example country, state etc.
I want to validate Address if I receive any data for it, if I don't receive anything, then I don't want to return any validation error to the UI for Address. I would return only validation errors for User then.
What would be the best way to do this?
Thanks,
I used some code from Simon J Ince of Microsoft. He has it here on his blog. It also has client side validation which is also nice. It has a RequiredIf attribute that only makes a field required if another field has a certain value. Just being able to see how he implemented it helped me figure out how to do some of this stuff by myself and I even retrofitted it to allow multiple values.
You might want to look into a Custom Model Binder for your User type. That way you can choose to override the validation of the Address item inside a User.
I have found that more complex custom validation is easier with FluentValidation. The documentation provided is very helpful, and you will be able to achieve your validation goal with this open source validator.

Resources