All Questions

Tagged with
Filter by
Sorted by
Tagged with
110 votes
9 answers
111k views

Rails: Validating min and max length of a string but allowing it to be blank

I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am using the below ...
bgadoci's user avatar
  • 6,433
106 votes
4 answers
69k views

Rails "validates_uniqueness_of" Case Sensitivity

Here is the model (I am using SQLLite3): class School < ActiveRecord::Base validates_uniqueness_of :name end For example, after I add "Yale", I cannot add "Yale" but can add "yale." How can I ...
GeekJock's user avatar
  • 11.2k
96 votes
7 answers
103k views

Raise a validation error in a model's save method in Django

I'm not sure how to properly raise a validation error in a model's save method and send back a clear message to the user. Basically I want to know how each part of the "if" should end, the one where ...
Bastian's user avatar
  • 5,715
77 votes
16 answers
59k views

Rails before_validation strip whitespace best practices

I would like my User model to sanitize some input before before save. For now some simple whitespace stripping will do. So to avoid people registering with "Harry " and pretend to be "Harry", for ...
berkes's user avatar
  • 27.3k
65 votes
4 answers
122k views

Email model validation with DataAnnotations and DataType

I have following model: public class FormularModel { [Required] public string Position { get; set; } [Required] [DataType(DataType.EmailAddress)] public string Email { get; set; } ...
lifeofbenschi's user avatar
65 votes
1 answer
14k views

Is it bad practice to include non-validating methods in a pydantic model?

I'm using pydantic 1.3 to validate models for an API I am writing. Is it common/good practice to include arbitrary methods in a class that inherits from pydantic.BaseModel? I need some helper methods ...
ccred's user avatar
  • 653
58 votes
3 answers
78k views

Ruby on Rails: errors.add_to_base vs. errors.add

I have read that errors.add_to_base should be used for errors associated with the object and not a specific attribute. I am having trouble conceptualizing what this means. Could someone provide an ...
Tony's user avatar
  • 18.9k
57 votes
6 answers
38k views

How do you validate uniqueness of a pair of ids in Ruby on Rails?

Suppose the following DB migration in Ruby: create_table :question_votes do |t| t.integer :user_id t.integer :question_id t.integer :vote t.timestamps end Suppose ...
dfrankow's user avatar
  • 20.6k
51 votes
5 answers
42k views

How to add boolean required attribute in mvc?

I have one model class like: public class Student { [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "...
Sonu K's user avatar
  • 2,652
51 votes
3 answers
51k views

Rails model validation on create and update only

If I want to have validation only on create, then I can do validates_presence_of :password, :on => :create But how do I say on create and update? I tried this but it didn't work: ...
Jakub Arnold's user avatar
  • 86.4k
47 votes
4 answers
107k views

Check if laravel model got saved or query got executed

I've seen alot of people using this way to check if a laravel model got saved. So now I wonder if it is a safe way. And also can I check if the queries bellow got executed like this Check if model ...
user2722667's user avatar
  • 8,495
43 votes
6 answers
8k views

Ruby on Rails: Is it better to validate in the model or the database?

Is it generally better practice (and why) to validate attributes in the model or in the database definition? For (a trivial) example: In the user model: validates_presence_of :name versus in the ...
William Jones's user avatar
40 votes
3 answers
25k views

Rails Internationalization (I18n) in model validations: Possible or not?

I have the following validation in a model: validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever') It seems that the translation does not work in ...
TomDogg's user avatar
  • 3,908
37 votes
4 answers
17k views

What is the Rails3 version of errors.add_to_base?

I want to write a custom validation in Rails 3, but following this example I get: 'ActiveModel::DeprecatedErrorMethods.add_to_base' call is deprecated in Rails 3.0 What is the rails3 version of: ...
Marius Butuc's user avatar
  • 18.1k
36 votes
4 answers
20k views

Required Attribute on Generic List Property

Is it possible to put a [Required] attribute onto a List<> property? I bind to a generic list on POST and was wondering if I could make ModelState.IsValid() fail if the property has 0 items in it?
Nick Reeve's user avatar
  • 1,698
36 votes
1 answer
6k views

Best approach to model validation in PHP? [closed]

I've learned that there are often many ways to solve one programming problem, each approach typically having its own benefits and negative side affects. What I'm trying to determine today is the best ...
Jonathan's user avatar
  • 18.7k
34 votes
3 answers
28k views

rails validate in model that value is inside array

I have a form where i pass a field named :type and i want to check if it's value is inside an array of allowed types so that no one is allowed to post not-allowed types. the array looks like @...
stergosz's user avatar
  • 5,778
34 votes
10 answers
22k views

How do I validate that two values do not equal each other in a Rails model?

I have a User model, which has an email and a password field. For security, these may not be equal to each other. How can I define this in my model?
user avatar
34 votes
4 answers
32k views

Kohana 3: Example of model with validation

I find examples and tutorials about models and about validation. And I places that say the validation (or most of it at least) should be in the model, which I agree with. But I can't any examples or ...
Svish's user avatar
  • 155k
33 votes
6 answers
14k views

Rails: how to require at least one field not to be blank

I know I can require a field by adding validates_presence_of :field to the model. However, how do I require at least one field to be mandatory, while not requiring any particular field? thanks in ...
deb's user avatar
  • 12.6k
31 votes
2 answers
14k views

Rails put validation in a module mixin?

Some validations are repetitive in my models: validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true validates :name_seo, :length => { :minimum => 2 },...
Victor Pudeyev's user avatar
27 votes
5 answers
47k views

attribute dependent on another field

In a model of my ASP.NET MVC application I would like validate a textbox as required only if a specific checkbox is checked. Something like public bool retired {get, set}; [RequiredIf("retired",...
Ahmet Dalyan's user avatar
24 votes
4 answers
20k views

Rails - How to validate a field only if a another field has a certain value?

In my form I have a Select with three values: Apple, Banana and Cherry. If I choose Apple from the select I hide another Select- and a Text-field with some Javascript, because when Apple is chosen, ...
TehQuila's user avatar
  • 658
23 votes
7 answers
10k views

Rails: validate presence of parent_id in has_many association

I have a projects resource that has many tasks. I want to ensure that every task has a project_id by adding validates_presence_of :project_id to the tasks model. However, when creating a new project ...
deb's user avatar
  • 12.6k
23 votes
2 answers
15k views

Yii: validation rules that always apply except one scenario

I know that you can have a validation rule that applies only for one scenario: array('username', 'exist', 'on' => 'update'), Now i would like to know if it's possible to do the opposite: a rule ...
darkheir's user avatar
  • 8,900
22 votes
2 answers
14k views

Ruby on Rails: How to validate a model without Active Record?

I'm currently trying to validate fields without having an ActiveRecord::Base inheritance. My model stores the data on a cache server so I do not need ActiveRecord. Anyway, I would like to validate the ...
Nicolas Guillaume's user avatar
20 votes
4 answers
24k views

Cannot skip validation in Rails 3?

I'm working on a project in Rails 3 where I need to create an empty record, save it to the database without validation (because it's empty), and then allow the users to edit this record in order to ...
Andrew's user avatar
  • 42.8k
19 votes
2 answers
16k views

Smarter paperclip validations

I'm using paperclip in a rails app and have the following three validations in my model validates_attachment_presence :photo validates_attachment_size :photo, :less_than=>1....
stephenmurdoch's user avatar
19 votes
1 answer
23k views

How to use full_clean() for data validation before saving in Django 1.5 gracefully?

I think Django's model validation is a little inconvenient for those models that don't use built-in ModelForm, though not knowing why. Firstly, full_clean() needs called manually. Note that ...
ray6080's user avatar
  • 893
18 votes
3 answers
9k views

Is there a way to transparently perform validation on SQLAlchemy objects?

Is there a way to perform validation on an object after (or as) the properties are set but before the session is committed? For instance, I have a domain model Device that has a mac property. I would ...
Beau Simensen's user avatar
18 votes
3 answers
22k views

Rails validation from controller

There is a contact page, which offers to enter name, telephone, email and message, after that it sends to an administrator's email. There is no reason to store message in DB. Question. How to: Use ...
Roman's user avatar
  • 363
17 votes
5 answers
28k views

Check user's age with laravel validation rules

How can I check the age of a user upon registration? I want to set the minimum age to be 13 years old. I ask for the user's date of birth and when I validate the other credentials, I want to check ...
Michael's user avatar
  • 4,332
17 votes
6 answers
11k views

Rails 3 validate uniqueness of one value against a column?

I have a model with an active column, which is a boolean. I want to validate the uniqueness of all newly added records against company_id, such that I can add as many records as I want with the same ...
neezer's user avatar
  • 20.2k
16 votes
4 answers
16k views

Rails form validation conditional bypass

I have a rails model that validates uniqueness of 2 form values. If these 2 values aren't unique the validation errors are shows and the "submit" button is changed to "resubmit". I want to allow a ...
hacintosh's user avatar
  • 3,822
16 votes
3 answers
16k views

Rails Validation for users email - only want it to validate when a user signs up or updates email address

I have a User model with the usual attributes such as email and hashed_password etc. I want to write a validation that checks for the presence of an email address but only when 1) there isn't one ...
robodisco's user avatar
  • 4,212
15 votes
3 answers
15k views

Conditionally Require Only One Field In Django Model Form

Anyway to make a field conditionally required based on whether or not another field in the same form has been filled out? If field1 has no data, but field2 does form is valid. If field1 has no ...
Derek Reynolds's user avatar
15 votes
2 answers
12k views

cakephp one form, multiple models, not displaying one model's validation messages

I have a registration form and I am creating a record in both User and Identity tables (a user hasMany identities) the form looks like this <?php echo $this->Form->create('User');?> &...
Elwhis's user avatar
  • 1,241
15 votes
4 answers
21k views

data validation for SQLAlchemy declarative models

I'm using CherryPy, Mako templates, and SQLAlchemy in a web app. I'm coming from a Ruby on Rails background and I'm trying to set up some data validation for my models. I can't figure out the best ...
Sarah Vessels's user avatar
14 votes
2 answers
3k views

Rails 3 - validating one of two fields has been completed

I'm writing a Rails app to manage a series of banners along the top of our website; each one should link either to a URL provided by the editor, or a specific product selected from a drop-down list. ...
PaulC's user avatar
  • 491
14 votes
2 answers
7k views

Display errors using Knockout JS + MVC + Server-side Model Validation?

Html form is controlled using Knockout JS and jQuery templates. Basic jQuery validation is in use to validate fields. Form gets serialized to JSON and submitted to MVC controller action using AJAX. ...
mb666's user avatar
  • 443
12 votes
9 answers
25k views

Yii form model validation- either one is required

I have two fields on the form ( forgotpassword form ) username and email Id . User should enter one of them . I mean to retrieve the password user can enter user name or the email id . Could some ...
Bujji's user avatar
  • 1,717
12 votes
3 answers
25k views

ModelState.IsValid or Model.IsValid?

I'm writing a controller and unit tests for it, when I came across two ways (equally valid I think) to do something. All my models have an IsValid property, which I can check to ask the model if it's ...
Andy's user avatar
  • 8,532
11 votes
4 answers
8k views

validates_presence_of + :message shows the name of the field

I'm creating a Rails app, and I have a model called User. In this model I have a boolean value called isagirl. A user must specify if it is a girl or not, which is done by two radio buttons. In my ...
user avatar
10 votes
7 answers
38k views

Disable Model Validation in Asp.Net MVC

How do I disable Model validation for a single Action in a Controller ? Or can I do it per model by registering the model type at startup somewhere ? I want the ModelBinder to bind to the model, but ...
MartinF's user avatar
  • 5,949
10 votes
3 answers
3k views

Two columns must not equal each other in Rails

I'm creating a social network in Rails and I have a model like this: create_table "friendships", :force => true do |t| t.integer "user1_id" t.integer "user2_id" t.boolean "...
user avatar
9 votes
2 answers
6k views

Why do we no longer need to manually validate models in higher versions of ASP.NET Core?

All my models are automatically validated before hitting the endpoint, and return appropriate errors if some form of validation has failed. I remember back in ASP.NET Core 2.2 we needed to manually ...
SpiritBob's user avatar
  • 2,494
9 votes
1 answer
4k views

Yii2: Prevent empty string in attribute of ActiverRecord

What's the best way to prevent that an empty string gets inserted into a field of an MySQL's InnoDB table? Allowed values are strings with more than zero characters or NULL. I'm asking that because ...
robsch's user avatar
  • 9,518
9 votes
2 answers
6k views

Using a ASP .NET MVC model value to validate another model value?

I have a model class like this: public class SomeClass { public int EmployeeId {get;set;} public int DayTotal {get;set} } For this model class I am creating a custom ValidationAttribute for ...
John M's user avatar
  • 14.5k
8 votes
6 answers
12k views

How do I display a user friendly error when Spring is unable to validate a model's java.util.Date field?

I'm using Spring 3.1.2.RELEASE. I want to display an error message on my JSP if my date field isn't formatted properly. I thought I'd followed all the right steps. I binder a converter in my ...
Dave's user avatar
  • 16.4k
8 votes
1 answer
5k views

Can multiple values be used in a model validator in Django?

I've got a model using a validation class called CompareDates for my model validators and I want to pass the validator two field values. However I'm unsure of how to use multiple field values in a ...
markwalker_'s user avatar
  • 12.6k

1
2 3 4 5
14