Laravel validation service doesnt work with custom rules - laravel

I have implemented a Gateway -> Repository pattern and want to integrate validation service with it. I have created the service
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/ServiceProviders/ValidationServiceProvider.php
Which adds the CustomValidator
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/Extensions/Validation/CustomValidator.php
This is the actual validation that is getting performed on the user data from the controller
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/Gateways/UserGateway.php
This are the rules for uservalidator
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/Services/Validators/UserValidator.php
But the hex validator is never getting called !
I have followed these articles
http://culttt.com/2013/07/29/creating-laravel-4-validation-services/
http://culttt.com/2014/01/20/extending-laravel-4-validator/
Please help.
Edit : Full code is available here : https://github.com/octabrain/Laravel4-Patterns-Full

It was my mistake, I was not passing anything in the request so the rule was not getting triggered !
Updated the code
See here : https://github.com/octabrain/Laravel4-Patterns

Related

Sonarqube Update Rule through Web-API

I am currently trying to update update a rule through the web api of sonarqube. I was trying to pass information like Serverity, Description and Remediation Function with the post request with the parameters according to the web api documentation. Everytime I tried to do that I got a response of 400.
But when I edited the request parameters to pass only the markdown_note and the key it worked and the note was set.
I honestly don't know what I am missing. According to the documentation it should work.
The description of the api/rules web services is :
Get and update some details of automatic rules, and manage custom
rules.
You can only update custom rules, not rules provided by language plugins.
The only exception to this is indeed the fact that you can add some notes on all rules.

How to stop grails erasing custom validation errors

In my Grails app, I am using methods in a service to do complicated validation for user submitted data. Unfortunately, Grails is quietly sabotaging me.
I populate the domain instance with the user submitted data
I hand the instance off to the service, which analyzes the properties.
If errors are found I add them using
instance.errors.rejectValue('myValue','errors.customErrorCode','Error')
BEHIND THE SCENES, when the service passes the domain instance back to the controller grails checks for changed properties and calls validate() before returning the instance. (verifiable by seeing the beforeValidate event called on returning a domain instance from a service to a controller where one or more properties has changed)
That behavior clears any custom errors I have added and the instance I get back in the controller is now incorrectly without error.
How can I
A) stop grails from validating between service and controller
OR
B) prevent a validate() call from wiping my custom errors.
EDIT
So far I've found one partial answer,
If you use instance.get(params.id), grails will self validate behind the scenes wiping custom errors.
If you use instance.read(params.id) you can bypass this behavior to an extent.docs
But this solution is limited by domain relationships. Any other solutions welcome.
Seems that it is not custom validation. It can be because of transactional service. Service opens separate transaction for each method and clears entities after method end. You can find this mentioned in docs(read the last paragraph of part ). So errors dessappear not because of validation.
Don't know if your service is transactional. But if it is - you can add #NotTransactional annotation to method were you want not to loose errors. And
errors will be saved.
Hope it helped,
Matvei.
Not sure how your code looks like or what is causing the problem, but in any case I strongly suggest implementing custom validators in the domain class or in a command object within the constrains.
Here are some examples from grails docs:
http://docs.grails.org/2.4.0/ref/Constraints/validator.html

How to debug Web API model binding

I've been working with quite some Web API projects now and find myself bumping into the same problem every time and that's when I do a POST or GET the value / model etc is null or I get a 404.
There is a checklist like:
- did I use the correct content-type?
- has routing been set up correctly
- is the signature of the model that I'm posting really the same as the model that the endpoint accepts?
It would be nice if there is a trace one could follow where it fails. Now it just looks like a black box, you put something in and it works or not, if it doesn't: see checklists or SO.
Is there something that you can setup in Web API so you can debug the model binding process?
I would implement action filter.
One of the methods that can be overridden there is :
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
In this action you can check the response status and if it is an error to perform all your checks
This article could be a good starting point

Struts 2 validation - clearing the error message

Good day!
I used the Struts2 xml validation as instructed in this website.
The problem is when I clicked the submit button twice. The error message also appears twice...
My question is... how to clear the first error message before another action is processed to accommodate new set of error messages.
Thank you in advance.
If you are using the spring integration you have to define your bean as scope="prototype", then you get a new instance of your Action for every request.
The default scope for spring is singleton.
It's a good idea to do this for every Action.
Remove validate="true" from form tag.
There is a validator interceptor in the default stack. You just need to use that and using that is very simple. Just make a method in your action class by the name public void validate(). Within that validate() you can access the fields using their getters & then put the required validation onto them.
Also, with this implementation you would not have to worry about the multiple messages being shown, because it will show only the message what you set in the addFieldError method and removes any previously kept messages.
NOTE: Be sure to use getters of the variables in your validate(), because the variables in the action are not set at the time this interceptor is invoked.
Here is a link to a very nice tutorial.
http://www.vaannila.com/struts-2/struts-2-example/struts-2-validation-example-1.html
If you will keep validate=false, then validation xml will be called on submit action.
To clear messages on each submit click, if you are using table in your jsp then keep table outside of form. It will work.

DRY Remote Validation in ASP.NET MVC 3

I've read David Hayden's great post on MVC 3 Remote validation.
However there is presented what you should do to enable remote (javascript) validation. If the user has javascript disabled the post would still be made even if data is not valid. Therefore a server-side validation should occur.
How could we make this check as DRY (Don't Repeat Yourself) as possible? Of course, including the same check code in the post action as in the remote validation action (or just the same call) can work but I am wondering if a one-liner or something more elegant is available.
Perfectly acceptable answers include "no, it can't be done". :)
See my MSDN article How to: Implement Remote Validation in ASP.NET MVC
I use the remote client validation code in the HttpPost Create method to test server side when JavaScript is disabled.
[HttpPost]
public ActionResult Create(CreateUserModel model) {
// Verify user name for clients who have JavaScript disabled
if (_repository.UserExists(model.UserName)) {
ModelState.AddModelError("UserName", ValidationController.GetAltName(model.UserName, _repository));
return View("Create", model);
}
It 'can' be done.. but you would need to write your own custom attribute that basically emits for client side and is validated server side. For me I just extract the validation code into a method and check on the server.
Something similar came up recently as well:
Prevent form from submitting when using unobtrusive validation in ASP.NET MVC 3
I wonder if one couldnt inherit from the remote attribute and add their own server side code them. hmm.. maybe I'll have to try this.
I would be happy though if someone here said they already did this : )
I have done this, it's a bit of a long solution, so it's all available on my blog here:
http://www.metaltheater.com/tech/technical/fixing-the-remote-validation-attribute/
I had to create a new subclass of the RemoteAttribute class, create my own custom model binder by inheriting from DefaultModelBinder, and then use reflection to call the validator on the controller.

Resources