Home > php > Validation Logic In Model

Validation Logic In Model

January 14Hits:2

I was debating putting validation logic in my models (setter functions). Is this something I should be using a separate object for? Below is something I had in mind.

class Person {      private $name, $setErrors = [];       public function setName($name){           if(strlen($name) > 0){               $this->name = $name;                return true;           }           $this->setErrors[] = "Name must be at least one character";           return false;      }      public function getErrors(){           return $this->setErrors;      } }  $person = new Person(); $person->setName("Bob"); if (count($person->getErrors()) > 0){      /* Return Errors To View */ } 


Separating concerns is best practice.

Each class should have a single responsibility.

Also, if you moved the validation logic into a separate class, it can be re-used on multiple fields, not to mention being much easier to unit test.

class NameValidator
    public function isValid($string) {
         // Logic


Related Articles

  • Validation Logic In ModelJanuary 14

    I was debating putting validation logic in my models (setter functions). Is this something I should be using a separate object for? Below is something I had in mind. class Person { private $name, $setErrors = []; public function setName($name){ if(st

  • Advantages of using business logic in model

    Advantages of using business logic in modelMarch 19

    In an MVC framework i have often heard that use business logic in a model. But i think using the business logic in controller would run faster as view is directly connected to controller. I want to know what are the advantages of using a business log

  • Do objects with validation logic in their domain really represent their real world counterparts?October 7

    I asked a question about whether validation behavior should be treated the same as other types of behavior in regard to the concept of OOP being "data + behavior". I got some good answers back that addressed the question from the philosophical p

  • Where we should put validation for domain modelNovember 15

    I still looking best practice for domain model validation. Is that good to put the validation in constructor of domain model ? my domain model validation example as follows: public class Order { private readonly List<OrderLine> _lineItems; public vi

  • Setup logical Database model for measurements

    Setup logical Database model for measurementsNovember 12

    I'm struggling to set up a logical model for the following spreadsheet: (See a close-up of the table .) My problem is that it is not clear to me how to deal with the "gap" values where each gap (nominal, min, max) contains values (forces) from t

  • Button Validation Logic

    Button Validation LogicNovember 16

    I came across a simple example for validating that when a button is clicked, it verifies that something is true or false in the Record before allowing the normal button logic. {!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} {!REQUIRESCRIPT(&qu

  • Best way (logic) to model an XOR?October 17

    Some people say that the best way to model an XOR is to use NMOS pass logic. Others say its better to use a transmission gate (or simply, pass logic). The only difference I could find in these implementations was the number of transistors required an

  • Put Business Logic and Model Declaration in Different DLL March 12

    I am new to MVC and started working on a small application. I have a basic idea about what to do and want to use MVC and EntityFramework. I want to create a separate Business Layer. I want to put all the Model declaration in business layer and want t

  • Refactor presentation logic from modelSeptember 2

    I've seen this pattern used in a considerable amount of MVC applications. Let's assume we have a User model with the methods: hasSessionExpired displaySessionExpired The first method (hasSessionExpired) looks up in the database if the user's session

  • Different ways of implementing cross-validation for SVM model in MATLABFebruary 13

    Suppose that we have this code in MATLAB R2015b: SVMModel = fitcsvm(INPUT, output,'KernelFunction','RBF','BoxConstraint',1); CVSVMModel = crossval(SVMModel); z = kfoldLoss(CVSVMModel) In first line using fitcsvm model trained by hole data. what is pu

  • Entity Framework 5, separating business logic from model - Repository?October 27

    I am working on my first public-facing web application and I'm using MVC 4 for the presentation layer and EF 5 for the DAL. The database structure is locked, and there are moderate differences between how the user inputs data and how the database its

  • Do I have too many regexes in my validation logic?July 31

    The below function uses Javascript to validate a Belgian telephone number and format it according to the guidelines for phone number formatting. function acm_mscrm2011_psa_FormatPhoneNumber(executioncontext) { //get the phone number from the field th

  • List Form Validation Logic Not Working as ExpectedJanuary 21

    I'm trying to include Validation Settings for my List Form where I've got a column called Food Service and another column called Food Service Type, where Food Service is a "Yes/No" type column and Food Service Type is a "Choice Menu" w

  • Validating data in model builder

    Validating data in model builderSeptember 3

    I have FGDB with points feature classes that I want to convert to lines using using a ready-made script called TrackBuilder. I am using model builder: When I populate the Track Builder script tool with the parameters everything looks fine. But when I

  • form validation with nested models in Rails

    form validation with nested models in RailsJanuary 22

    I have this problem. I need to validate the attributes of two models in the same form in Rails. One is the parent of the other. The form is like this: <%= semantic_form_for @professional do |pro| %> <%= pro.inputs :id => "information"

  • How can I run ZF2 Validation on a model without a form?January 22

    I am trying to run validation on a class in ZF2. I have followed the Album example and understand how the validator works with a form but how can I run it against an array? Below is the code from the example. $form->setInputFilter($album->getInputFi

  • Before validation on nested modelJanuary 9

    I have a nested model items and I am trying to multiply two columns together cost and quantity to set the last column price. I need to set the column price before the form is saved and i need to also validate the model. The before_validation call bac

  • Best practices for architecturing data validation in a Django multi sided project [on hold]February 11

    I know it's a broad question. But let's imagine you have a Django project with an API used by mobile app clients. There are many places where you can add validation logic: Models fields themselves Models, by overriding save() method Django forms and

  • Getting Started with Doorkeeper and OAuth 2.0

    Getting Started with Doorkeeper and OAuth 2.0February 8

    Authentication in Rails Getting Started with Doorkeeper and OAuth 2.0 OAuth 2 All the Things with oPRO: Customization Authenticate All the Things with oPRO, the Basics Simple Rails Authentication with Clearance Rails Authentication with Authlogic Mag

  • Business rules, business logic, input validationJuly 22

    This might be asked a thousand times but couldn't find the answer. I just wonder how you people handle the business logic errors? Im trying to do a nice api for my business model. Some methods have quite a lot of validation and I just wonder how shou

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.510 s.