Rails Validations - Reverse of Confirmation Validation - ruby-on-rails-4

I want to be sure, two attributes don't have the same value with a validation in my Rails4 application. I know about confirmation validation but I need exactly the opposite of that.
Does Rails have this kind of validation?

You need to create a custom validation I think:
validate :check_attribute1_and_attribute2
def check_attribute1_and_attribute2
if attribute_1 == attribute_2
errors.add( :column_2, ' Value 2 cannot be similar to Value 1!')
end
end
Hope it helps :)

I had a similar need and wanted a simple solution. I thought this worked out pretty well in the end.
validates :applicant_id, exclusion: {
in: -> (reference_request) { [reference_request.reference_id] },
message: 'cannot also be a reference'
}

Related

Nest.js serializer like Django

I am currently using Nest.js, Postgresql, Typeorm for my backend. Now I am trying to do is to see an specific entity field differently by each user.
For example there are 10 posts and one user has bookmarked 3 of them. Only a user who bookmarked the post can get isBookmarked = true, others isBookmarked = false.
I used Django a bit and I used serializer to implement the same logic. I looked for Nest.js serializer (https://docs.nestjs.com/techniques/serialization) but I think it is a bit different than what I thought. Please tell me how to use this serializer as Django does, or any other ways to implement the logic.
Since NestJS is using class-transform you can follow the documentation to achieve what you wants => https://github.com/typestack/class-transformer#additional-data-transformation
import { Transform } from 'class-transformer';
export class Post {
id: number;
#Transform(({ value }) => isBookmarkedByTheUser())
bookmarked: boolean;
}
Something like that ;)

Is there a way to add custom validations on user-input data while using Flask-appbuilder inbuilt forms?

I am building a web-app using flask appbuilde and stuck at the following issue for a week now.
On the documentation, the instructions seem pretty straight forward:
class MyView(ModelView):
datamodel = SQLAInterface(MyModel)
validators_columns = {
'my_field1':[EqualTo('my_field2', message=gettext('fields must match'))]
}
However, when I implement this exactly, on UI it says "invalid input" even when inputs are correct thus hindering form submission.
My Code(views.py):
class DelModelView(ModelView):
datamodel = SQLAInterface(Dell)
base_filters = [['cap_id', EqualTo, get_user]] #current user
list_columns = ["cap_id", "s_code", "s_name", "sos", "date_of_change"]
#base_order = ("cap_id", "asc")
validators_columns = {
'cap_id':[FilterEqualFunction(get_user, message=_('fields must match'))]
}
def get_user():
return g.user.username
I want to add a validation that checks if while adding new entry cap_id == username.
Am I missing any link here? I have tried multiple solutions but nothing seems to work.
Any help will be appreciated!
I managed to solve this by doing the following:
I removed cap_id from add_coloumns. I am pre-populating it by using the default value, which is current_user and now it works perfectly fine. Yay!!

Pattern validation in angular 7

I have a contact number field in my Angular 7 form.I used 'form builder' and 'validators.pattern' for validation.In the HTML, I tried two ways to determine whether there was an error ,but both didnt work.
TypeScript:
mobnumPattern = "^[6-9][0-9]{9}$";
this.myForm = this.formbuilder.group({
contact_no: ['', [Validators.required,Validators.pattern(this.mobnumPattern)]],}
)
1.When I used below HTML, validation always shows true
*ngIf="((myForm.controls['contact_no'].touched) && (myForm.controls['contact_no'].hasError(pattern)))"
2.When I used below HTML, validation always shows false
*ngIf="((myForm.controls['contact_no'].touched) && (myForm.controls['contact_no'].errors.pattern))"
Any idea how to solve this?.
Thanks in Advance.
Lets go over both the cases you have mentioned.
1.When I used below HTML, validation always shows true
I tried recreating the issue in stackblitz but it is always false unlike what you said. Anyway the check myForm.controls['contact_no'].hasError(pattern) returns false since hasError() is expecting a string as its parameter, but pattern here is undefined.
Use this to check if the form control has pattern validation errors.
*ngIf="((myForm.controls['contact_no'].touched)&& myForm.controls['contact_no'].hasError('pattern')))"
2.When I used below HTML, validation always shows false
myForm.controls['contact_no'].errors will be null if the form control does not have any validation errors. So when checking myForm.controls['contact_no'].errors.pattern in the template will throw an error and return undefined. Use a safe navigation operator to protect against a view render failure if the myForm.controls['contact_no'].errors is null.
Like this:
*ngIf="((myForm.controls['contact_no'].touched) && (myForm.controls['contact_no'].errors?.pattern)"
I have made a stackblitz with the above mentioned fix. Check the link to see the working demo.

Laravel 4 regex email validation

I am trying to add validation, inside my User model to validation emails using regex.
However, it's spits a dummy out at the first apostrophe.
'email' => 'required|regex:/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/',
Have you tried the 'email' validation rule?
'email' => 'required|email|unique:users,email'
http://laravel.com/docs/4.2/validation#rule-email
As the answer to this question on SO states, there is no simple regular expression to validate an email-address. Using your RegEx could maybe catch valid addresses (although that's just speculation of mine). Using the email-validation-rule would be my first choice.
But you are right, this is just the server side in the first place, if you ignore redirecting users back with input and error messages..
On the client-side, you would have some options. The first one would be to simply rely on the build in browser-validation, by declaring the corresponding input-field as an email-address which you should do anyway:
{{ Form::email($name, $value = null, $attributes = array()) }}
Another, more advanced way would be to create some kind of helper to check the typed input via Ajax using the same validation rule and returning the error messages or sth. similar. This could be an additional route to your Model-Resource for example. This way, you would be stable and consistent.

Fake select field for Simple Form

I'm using Simple Form, and I have a few fields that are not associated with my model. I found using this fake field option to be a good solution:
https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes
I thought this was cleaner than adding an attr_accessor value for my fields, and it works great for text input fields. I'm hoping to accomplish the same thing with a Select Field.
I found this thread, but I couldn't find anything else:
https://github.com/plataformatec/simple_form/issues/747
Has anyone found a similar option for a Fake Select Input? Thanks!
Assuming you'll use that "fake select" for UI purposes (probably as a mean to modify the form fields to present the user using Javascript?) and you don't want to deal with the value in the controller, you could just use select_tag with any field name, instead of the simple_form f.input. The value will be sent to the server, but it will be outside the model params, so you can just ignore it in the controller.
If I misunderstood your question, please clarify.
If your just trying to get the name='whatever' instead of having name='model[whatever]' I've found it easiest to just specify the name attribute in input_html { name: 'whatever', id: 'whatever' } hash which over rides the default model[attribute].
Otherwise you could create a fake_select_input.rb which would be similar to fake_input.rb but obviously use a select_tag instead and do something like as: :fake_select