Validate polymorphic votes – Controller or Model?

I have a question because I don’t want to do something wrong.

I have a polymorphic relation called votes. Well, I want to save votes but I need to validate if a user already has voted. But even if, in some cases a user can vote multiple times. It depends on the type (as said, polymorphic relation). I’ve set this data in a config file.

Now, where should I validate the votes? I thought I could do it in the model with a static method but then I’d be retrieving the config files in the model and also I’d have to return multiple errors. Something like “you have already voted” or “you can vote again in 1 hour”.

I’d do that with some kind of error codes which I’d pass to the controller and then to the view to inform the user. But it seems wrong to me.

What would be the right way of doing this?

12

I think you should validate your models in… model validators 😉

In my current architecture each model has an associated validator class, which contains validation logic. (Controllers have validators as well, but that’s not the point). This is because validation logic can sometimes be complex enough, especially when models receive states.

To validate a model I just pass the result of $model->getAttributes() to a validator. The part, responsible for creating a validator and passing it attributes is now a part of my custom BaseModel class.

I’ve set it up so validation runs when model is saved. If model is invalid, exception is occured. So, the controller or service, saving the model, are responsible for it’s validation.

Validation result is empty when validation succeeds or a hash of errors (‘fieldName1’ => ErrorsArray1). ErrorsArray is a standard Laravel’s MessageBag, using which it’s easy to render errors to user.

So, to model/controller it doesn’t matter if User can’t vote at all, or will be able to vote again in 1 hour. Model validator creates an error message (cannot_vote or cannot_vote_tmp) and just bubbles it to controller, which passes it to view. View translates message if needed and presents it to the user.

Currently errors are just enumerated and presented to user using a simple list next to the input field it is related to. But this logic can be extended. If it becomes complex enough, you can place some of it into a ViewComposer, which will take errors from controller, calculate some state from them and then pass this state into a View freeing it from complex calculations.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật