<% my $api = gitprep_api; my $op = param('op') || ''; my $user_id = param('id'); my $errors; if (lc $self->req->method eq 'post') { if ($op eq 'update') { # Parameters my $id = param('id'); my $name = param('name'); my $email = param('email'); # Validator my $vc = app->vc; # Validation result my $validation = $vc->validation; # "id" check if (!(defined $id && length $id)) { $validation->add_failed(id => 'User id is empty.'); } elsif (length $name > 300) { $validation->add_failed(id => 'User id is too long.'); } # "name" check $name //= ''; # "email" check if (!(defined $email && length $email)) { $validation->add_failed(email => "Mail must be not empty"); } elsif (length $email > 300) { $validation->add_failed(email => "Mail is too long"); } elsif ($email !~ /\@/) { $validation->add_failed(email => "Invalid mail address"); } else { my $where = app->dbi->where; my $clause = [ 'and', ':email{=}', ':id{<>}' ]; my $param = { email => $email, id => $user_id }; $where->clause($clause); $where->param($param); my $row = app->dbi->model('user')->select(where => $where)->one; if ($row) { $validation->add_failed(email => "Mail $email already exists"); } } if ($validation->is_valid) { # Encrypt password my $params = {}; $params->{name} = $name; $params->{email} = $email; # Update user eval { app->dbi->model('user')->update($params, where => {id => $id}) }; if (my $e = $@) { app->log->error(url_for . ": $e"); $errors = ['Internal Error']; } else { $self->flash(success => 1); $self->flash(id => $id); $self->redirect_to(url_for->query(id => $id)); } } else { $errors = $validation->messages } } } my $user = app->dbi->model('user')->select( where => {id => $user_id, admin => 0} )->one; %> % layout 'common', title => 'Update User'; %= include '/include/header';
% my $id = ''; % if (flash('success')) {
Success: User <%= flash('id') %> is updated.
% } % if ($errors) {
% for my $error (@$errors) {

<%= $error %>

% }
% }
Update User
%= hidden_field op => 'update';
ID
<%= $user->{id} %>
Name
<%= text_field 'name' => $user->{name}, placeholder => 'Name' %>
Mail
<%= text_field 'email' => $user->{email}, placeholder => 'Mail' %>
Users
%= include '/include/footer';