RailwayJS
Validations invoked after
create,saveandupdateAttributes, it also possible to skip validations when usesavemethod:obj.save({validate: false});Validations can be called manually using
isValidmethod of objectNormally all validations result in
errorsmember of object, which is a hash of arrays of error messages:obj.errors { email: [ 'can\'t be blank', 'format is invalid' ], password: [ 'too short' ] }It also can raise exception, if you want, just pass
throws: trueas param ofsavemethod:// be carefull, now it will throw Error object obj.save({throws: true});To setup validation, call it configurator on class:
Person.validatesPresenceOf('email', 'name') Person.validatesLengthOf('password', {min: 5})Each configurator accepts set of string arguments, and one optional last argument, which is actually specify how validator should work, of course it depends on each validator, but there's few common options:
- if
- unless
- message
- allowNull
- allowBlank
ifandunlessmethods is for skipping validations depending on conditions, it can be function, or string. Function invoked in context of object, where validation performed. If string passed, then one of object's member checked.
messagemember allows to configure error message, it can be string or object (depends on validator), see usage examples
allowNullandallowBlankis self explanatory :)Examples of different types of validations:
length
User.validatesLengthOf 'password', min: 3, max: 10, allowNull: true User.validatesLengthOf 'state', is: 2, allowBlank: true user = new User validAttributes user.password = 'qw' test.ok not user.isValid(), 'Invalid: too short' test.equal user.errors.password[0], 'too short' user.password = '12345678901' test.ok not user.isValid(), 'Invalid: too long' test.equal user.errors.password[0], 'too long' user.password = 'hello' test.ok user.isValid(), 'Valid with value' test.ok not user.errors user.password = null test.ok user.isValid(), 'Valid without value' test.ok not user.errors user.state = 'Texas' test.ok not user.isValid(), 'Invalid state' test.equal user.errors.state[0], 'length is wrong' user.state = 'TX' test.ok user.isValid(), 'Valid with value of state' test.ok not user.errorsnumericality
User.validatesNumericalityOf 'age', int: true user = new User validAttributes user.age = '26' test.ok not user.isValid(), 'User is not valid: not a number' test.equal user.errors.age[0], 'is not a number' user.age = 26.1 test.ok not user.isValid(), 'User is not valid: not integer' test.equal user.errors.age[0], 'is not an integer' user.age = 26 test.ok user.isValid(), 'User valid: integer age' test.ok not user.errorsinclusion
User.validatesInclusionOf 'gender', in: ['male', 'female'] user = new User validAttributes user.gender = 'any' test.ok not user.isValid() test.equal user.errors.gender[0], 'is not included in the list' user.gender = 'female' test.ok user.isValid() user.gender = 'male' test.ok user.isValid() user.gender = 'man' test.ok not user.isValid() test.equal user.errors.gender[0], 'is not included in the list'exclusion
User.validatesExclusionOf 'domain', in: ['www', 'admin'] user = new User validAttributes user.domain = 'www' test.ok not user.isValid() test.equal user.errors.domain[0], 'is reserved' user.domain = 'my' test.ok user.isValid()format
User.validatesFormatOf 'email', with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i user = new User validAttributes user.email = 'invalid email' test.ok not user.isValid() user.email = 'valid@email.tld' test.ok user.isValid()
re:think
I love it when companies start to re:think things they have been doing for ages for the sake of doing what they have always been doing because it's easy. Essentially fighting their lizard brains with the end benefit of saving costs. Which also usually goes long ways to help the environment in the process. Take a look a Puma's new shoe "box" design:Flash Ghromirum
"Just when we thought that Google was the champion of HTML5 they turn around and partner with Adobe on Flash to ensure that the web remains a mess of proprietary brain damage." — Daniel Hansen
http://blog.chromium.org/2010/03/bringing-improved-support-for-adobe.html
Shazamを使ってDaft PunkのAround The Worldという曲を見つけました。
![]() Shazamを使ってDaft PunkのAround The Worldを見つけたので、この曲をあなたに教えてあげようと思いました。 | |
| iPhoneまたはiPod touchでShazamを利用している場合は、ここをクリックして、タグリストにこの曲を加えることができます。 | ![]() |
Mein Bericht zum Barcamp Nürnberg 2
Um einiges verspätet — besser spät als nie — mein Beitrag zum Barcamp Nürnberg 2. Für mich war es das erste Barcamp in Nürnberg. Und alleine die Tatsache als alter Ex-Nürnberger mal wieder in der Stadt zu sein hat mich schon freudig gestimmt. Die lange Fahrt von Münster aus war also halb so wild. Zudem war ich nicht allein unterwegs, @pfandtasse und @Tamer_ waren mit dabei.




