Is there any event like OnValidate in LINQ where I can add my business validation rules so that they are ALWAYS called whenever I use Add() or Update()?
If not: What do you suggest to do? I could add a partial class and wrap the Add/Update methods with own ones but then one could still use the original ones ignoring any business validation rules.
Thanks in advance and keep up the excellent work with subsonic!
I haven't used much of the LINQ parts of SubSonic3, but the ActiveRecord.tt file allows you to customize the code so you could insert an validate method. We talked about it over at How to intersept the save method on ActiveRecord?
Can you modify the tt file to add in your validation call before saving the record?
Related
I was trying to make field Default Price List of Product entity to read only. I tried to do that from field properties by checking read only but it didn't work. Then I wrote a Business rule which will make the field read only that also didn't work.
Then I wrote a JavaScript code and attached that code in onLoad event of Product entity and it eventually worked.
Xrm.Page.getControl(attrLogicalName).setDisabled(true);
Actually I am trying to figuring out why business rules and field properties changes didn't work.
Any idea?
This is expected behavior as the Product entity has some OOB scripts locking/unlocking the fields with an onChange logic.
Probably Microsoft don't want you to interfere with that OOB implementation by using Business rule. As you said, custom javascript is the only possible approach.
It's more a general question, I want someone to point me to the direction I should go.
1) FUNCTION FOR SAME CONTROLLER: I have two methods: Store and Update in the same controller. They both should contain some complex request validation (which I can't do via validator or form request validation because it's too complex). This means for me now using the same code twice in two methods... How can I take this code, create a function and use it in both Store and Update methods just with a single line, like:
do_this_function($data);
2) FUNCTION FOR DIFF. CONTROLLERS: I have another code which I use in many different Contollers. It transliterates Russian letters into Latin ones ('Сергей' = 'Sergey' and so on). How and where should I register this function to be able using it in different Contollers?
transliterate_this($data);
I have read something about helpers. Should I use them? If you an experienced Laravel develper and say so, I will read everything about them. Or, if you advice something else - I'll read about that.:) I just don't want to spend time reading about something useless or "not right way to-do-it".
Appreciate any help!
1) You could create a form request validation or you could just create a private function that would handle the validation and return the result.
2) You can create a helpers.php file, see here, and you put your translation function inside. Then you can call it from anywhere:
In a controller transliterate_this($data);
In a view {{ transliterate_this($data); }}.
You can do complex validation even inside a FromRequest. You can override the getValidatorInstance for example, or any other method from the base class to plug your validation logic on top of the basic validation rules. Else just consider making an external class to handle that complex validation and inject it in your controllers using Laravel's IoC container.
You should create a facade to provide that feature. Then you can easily use it anywhere (and additionally create a helper method for it if that makes you feel better). Something like Transliterate::toLatin($str)
everyone! Thank you all for great answers. But I have discovered that the problem was that I didn't know anything about Object-Oriented Programming.
(Ans I was working in Laravel:)).
After I took an Object Oriented Bootcamp from Laracasts, I started 'seeing' how Laravel actually works and I know can easily create methods inside classes and use them in other classes.
https://laracasts.com/series/object-oriented-bootcamp-in-php
(of course, you can read something else on OOP, but Jeffrey Way has really outstanding explanation talent!)
would it be considered a valid implementation if I do not use the model for certain parameters? For example a webform posting values directly to the controller which then passes them to another class. Is it necessary to make sure that all the fields in the webform are also referenced/stored in the model?
I consider it a valid implementation, but suggest that you do this only if the parameters you want to exclude from the Model are absolutely NOT going to be used by the View (other than for confirmation of data entry in your webform), AND there is no need for the parameters to be referenced again once handled by the Controller.
Yes, it would work, strictly speaking.
However, you probably want to use the model. You don't want to create a new variable every time you run the view, which would happen if you use the controller.
I would consider it valid implementation if you decided not to use the model for certain parameters. I believe there are instances where certain fields may not relate directly to the model in question therefore giving valid reason to break those fields/parameters off from the model.
I'm currently trying to programmatically insert a ModelClientValidationStringLengthRule ModelValidator via a custom attribute, and wishing to avoid adding to the AdditionalValues dictionary in order to make use of existing functionality.
This is due to using a CMS, and wanting to control the length of the string via the CMS rather than within a model.
I assume I would do this in the OnMetadataCreated event in the custom attribute, however I cannot see how to add to the ModelValidator collection, only get them via GetValidators...
Anyone have any ideas?
Thanks in advance,
Dave
Rather than adding a custom attribute, you want to inject a validator based upon a condition.
You can use the class I detail in this answer to inject your validator based upon the conditions in your CMS.
I have lots of MVC validation attributes on my model. Everything works great when the defaultModelBinder binds my model on submit. But, I need to create a custom modelbinder. I'd like to continue using my validation attributes. Can I? If so, how?
I'm not sure whether this is possible or not, but one thing I can say is that if it is possible then the extension points for default model binder don't make it very discoverable. I spent several hours one day trying to get this to work to no avail.
In lieu of getting this to work, you can use the Controller's TryValidateModel() methods.