regex to validate router parameter not working - regex

I want to validate if an email is being send correctly in routes, however it isn't working when I put a regex condition (like explained here)
Basically, I added:
Route::group([
'namespace' => 'Auth',
'middleware' => 'api',
'prefix' => 'auth'
], function () {
Route::post('login', 'AuthController#login');
// ...
Route::get('checkemail/{email}', 'AuthController#check_email')->where('email', '#.*?\.');
});
even if I pass a valid email I get an 404 not found. what can I do to make the code works well?

It looks like you're missing .+ before # in your regex.
Edit: I forgot to mention, you can put the same thing after #, and the current regex seems like it's wrong as well. I won't go into it, but validating email should not be complicated for a lot of reasons.

Related

Regex don't work in reactive form control

I have the following regex for URL that require to start from http or https
(http:|https:)+[^\s]+[\w] that work very good for what I want (https://regexr.com/598hs)
But when I try to run it in angular validation, it doesn't work.
this.form = fb.group({
numberCode: ['', Validators.pattern(`(http:|https:)+[^s]+[w]/g`)]
})
https://stackblitz.com/edit/angular-reactive-forms-validation-lulnas
Why so ?
You can also try matching by this:
Validators.pattern(/^(http:|https:)+[^\s]+.+\.+.+$/)

Yii2 - Url Manager - Specific rule failing

I have the following url:
http://example.com/user/login
If I input the url in the browser, it matches de rule:
'<module:user>/<slug:[\w\-]+>' => '<module>/<slug>'
If I create the url:
Yii::$app->urlManager->createAbsoluteUrl(["user/index", "slug" => "login"]);
It should create the same url as above but instead it creates:
http://example.com/user/index?slug=login
But If I change the rule to:
'<module:user>/<slug:[\w\-]+>' => '<module>/index'
It works ok, any ideas why? I guess for some reason:
It is passing slug empty to or
It is an invalid value.
Any ideas?
That is because slug is part of the route pattern: '<module>/<slug>'. So <slug:[\w\-]+> is not treated as a named GET param, but as a part of the route. That means that URL /user/something will point to route user/something without any GET params.
You should not use the same name for route patterns and named params. You should either use different name:
'<module:user>/<action:[\w\-]+>/<slug:[\w\-]+>' => '<module>/<action>'
Or hardcode action for specified rule (as you already did in second example):
'<module:user>/<slug:[\w\-]+>' => '<module>/index'
Note that this will also match standard actions, like user/view.

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.

Rails Validations - Reverse of Confirmation Validation

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'
}

Django: How can I make a part of the URL optional

I have a url in which I would like to make the status token optional. If the status token is not provided in the url I give a default value in the view method argument. I tried replacing the token with this (?:/(?P<status>\d+))?$ but doesn't seems to work well. Thanks
url(r'^(?P<status>\d+)/$', frequest_list, name="frequest_list"),
def request_list(request, status=1):
...
...
Update:
This was the pattern I was trying:
url(r'^(?:/(?P<status>\d+))?$', frequest_list, name="frequest_list"),
So, if I try localhost/features/ works well
But if I do localhost/features/1/ it fails
Just create a second url entry that calls the same view:
url(r'^features/$', frequest_list, name="frequest_list_default"),
url(r'^features/(?P<status>\d+)/$', frequest_list, name="frequest_list"),
I use single url optional captures in some of my projects, and they work fine. You might want to adjust your pattern to make the trailing / optional. I think that is what is causing your url to not match. Django does have an "APPEND_SLASH" settings bool that will add that on to your urls if they are missing it and don't match:
url(r'^features(?:/(?P<status>\d+))?/?$', frequest_list, name="frequest_list")
The optional / could probably also be written like this:
url(r'^features/?(?:(?P<status>\d+)/?)?$', frequest_list, name="frequest_list")