Hi,
Sorry for the confusion. Now I get it - you directly specify your
validation messages to be binary strings. In my case I was specifying them
as regular list-strings - like:
[{fun()->length(UserPassword)>7 end,"Password length must at least 8!
<br>"},
{fun()->length(UserEmail)>5 end, "Invalid E-mail address! <br>"},
{fun()->boss_db:find(user_record,[{user_email,'equals',string:to_lower(UserEmail)},{id,'not_in',[Id]}])==[]
end, "This e-mail address is already registered! <br>"}].
So in this case when I do
case ....:save() of
{ok, _} ->
{error, E} -> {json, [{errors, E}]}
end
then the json output is an array of numbers instead of strings
{
"errors": [
[
80,
97,
115,
115,
119,
111,
114,
100,
32,
108,
101,
110,
103,
116,
104,
32,
109,
117,
115,
116,
32,
97,
116,
32,
108,
101,
97,
115,
116,
32,
56,
33,
32,
60,
98,
114,
62
],
[
84,
104,
105,
115,
32,
101,
45,
109,
97,
105,
108,
32,
97,
100,
100,
114,
101,
115,
115,
32,
105,
115,
32,
97,
108,
114,
101,
97,
100,
121,
32,
114,
101,
103,
105,
115,
116,
101,
114,
101,
100,
33,
32,
60,
98,
114,
62
]
]
}
So, I have to do this instead:
case ....:save() of
{ok, _} ->
{error, E} -> {json, [{errors, *list_to_binary*(E)}]}
end
This gives the correct json output:
{
"errors": "Password length must at least 8! <br>This e-mail address is
already registered! <br>"
}
This is why I had suggested the list_to_binary or the io_lib:write_string
function -- to convert the numbered lists into binary again.
But since you keep them in binary by default you have no need of this
conversion. Obviously it is better to keep it in binary by default like you
do to avoid the extra conversion step. But if that's not possible for
someone then I guess they would have to use the list_to_binary function.
Since OP was also getting numbers instead of characters in JSON and
list_to_binary is what I used in my case - hence I suggested that.
Sorry again for the confusion - I was only trying to help :)
Thanks
On Thursday, 30 October 2014 04:45:26 UTC+5:30, can2nac wrote:
>
> The validation function returns {error, [ErrorMessage*s*]} - correct if u
> have only one error
> It returns {error, [Error1, Error2, ..., ErrorN]} in general
>
>
> validation_tests() -> [
> {fun()-> false end, <<"email">>},
> {fun()-> false end, <<"password">>},
> ].
>
> case ....:save() of
> {ok, _} ->
> {error, E} -> {render_other, [{action,"show"}], [{errors, E}]} % or {json,
> [{errors, E}]}
> end
>
>
> On frontend don't forget to check name against array of errors, so this
> will not work if we consider given above case {% if email == errors %}
> has-error {% endif %}
>
> I use custom tag for this {%if errors|has:"email"%}has-error{%endif%}
>
> has(undefined,_)->false;
> has([],_)->false;
> has(Errors,Key)->has(Errors,Key,false).
>
> has(_,_,true)->true;
> has([],_,Res)->Res;
> has([H|T],Key,_)->has(T,Key,H=:=Key).
>
>
>
> On Monday, October 20, 2014 10:42:55 AM UTC+4, Alex V wrote:
>>
>>
>>
>> Hi everyone.
>>
>> Saw similar, but unanswered questions.
>>
>> When returning json from a controller, the browser often gets arrays
>> instead of strings (even from tutorial examples, "greeting-N" always
>> becomes an array) and vice versa. How do you control this behavior?
>>
>
--
You received this message because you are subscribed to the Google Groups
"ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit
https://groups.google.com/d/msgid/chicagoboss/dbf5fb9d-ec50-43de-abdb-537788015dc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.