Erlang/OTP Forums

Author Message

<  Erlyweb mailing list  ~  ErlyDB & Reflection

Guest
Posted: Wed Sep 12, 2007 1:50 am Reply with quote
Guest
I'm trying to write some generic code to do ErlyDB record validation.
Basically, what I want to do is pass a ErlyDB record and a list of
fields to validate to a data validation function like so:

validate(School) ->
validate_school(School, school:db_field_names()).

validate_school(School, [FieldName|T]) ->
case validate_field(School, FieldName,
school:get_field_value_via_reflection(FieldName)) of
{ok, School} ->
validate_school(School, T);
{error, Msg} ->
{error, Msg}
end;
validate_school(School, []) ->
{ok, School}.

validate_field(School, name, undefined) ->
{error, "School name is required"};
validate_field(School, city, undefined) ->
{error, "City is required"};
validate_field(School, postal_code, undefined) ->
{error, "Postal code is required"};
validate_field(School, _, _) ->
{ok, School}.

The part I'm missing is what I need to call to get a field name based
on value, the school:get_field_value_via_reflection() bit. I'm
certain that I'm missing something obvious, given my Erlang/ErlyWeb
newb status, so any pointers are really appreciated. Of course, if
there's a better, more Erlang-y way to do this I'd love to hear about
that too.

Thanks,
Kevin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist
Guest
Posted: Fri Sep 14, 2007 5:13 am Reply with quote
Guest
To get the value of a field in an ErlyDB record, just do

school:FieldName(School).

FieldName is a variable in this case. For setters, use

school:FieldName(School, NewVal).

Yariv

On 9/11/07, Kevin A. Smith <kevin@hypotheticalabs.com> wrote:
>
> I'm trying to write some generic code to do ErlyDB record validation.
> Basically, what I want to do is pass a ErlyDB record and a list of
> fields to validate to a data validation function like so:
>
> validate(School) ->
> validate_school(School, school:db_field_names()).
>
> validate_school(School, [FieldName|T]) ->
> case validate_field(School, FieldName,
> school:get_field_value_via_reflection(FieldName)) of
> {ok, School} ->
> validate_school(School, T);
> {error, Msg} ->
> {error, Msg}
> end;
> validate_school(School, []) ->
> {ok, School}.
>
> validate_field(School, name, undefined) ->
> {error, "School name is required"};
> validate_field(School, city, undefined) ->
> {error, "City is required"};
> validate_field(School, postal_code, undefined) ->
> {error, "Postal code is required"};
> validate_field(School, _, _) ->
> {ok, School}.
>
> The part I'm missing is what I need to call to get a field name based
> on value, the school:get_field_value_via_reflection() bit. I'm
> certain that I'm missing something obvious, given my Erlang/ErlyWeb
> newb status, so any pointers are really appreciated. Of course, if
> there's a better, more Erlang-y way to do this I'd love to hear about
> that too.
>
> Thanks,
> Kevin
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum