hyperstack-org / hyperstack-org/hyperstack

ActiveModel validators on the client

Đang mở
#297 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
538
Fork
41
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

ActiveModel provides a standard way to validate attributes, and it would be conveniently to use it on the client. Instead of validating a full record we can validate single attributes using validators directly:

```
def validate_attribute attr
value = send attr
self.class.validators_on(attr).each do |validator|
validator.validate_each(self, attr, value)
end
errors[attr].empty? ? nil : errors[attr]
end
```

And then validate user input in forms via existing model validators, something like that:
```
before_mount do
@user = User.new
end

render do
# Material UI
TextField(id: 'name', label: 'Name', required: true,
error: !@user.validate_attribute(:name).nil?,
helperText: @user.validate_attribute(:name)&.join("; ") || 'enter your name'
).
on(:change) do |evt|
@user.name = evt.target.value
end
end
```

Currently the model validators work only on the server, and aren't visible on the client, i.e. validators_on(attr) returns nil on the client. Is there some simple way to get it on the client too?

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.