Added validation on Token adding and editing
This commit is contained in:
parent
ea899d955c
commit
cb9819aa2e
1 changed files with 17 additions and 5 deletions
|
@ -17,12 +17,17 @@ sub index {
|
|||
# POST
|
||||
sub create {
|
||||
my $c = shift;
|
||||
my $validation = $c->validation;
|
||||
$validation->required('token-name', 'trim')->like(qr/^[\w]*$/);
|
||||
|
||||
my $token_name = $c->param('token-name');
|
||||
my $token_name = $validation->param('token-name');
|
||||
|
||||
my $token_rs = $c->result_set;
|
||||
|
||||
if ( $token_rs->find({ accounttokenname => $token_name }) ) {
|
||||
if ( $validation->has_error ) {
|
||||
# Only one validator, fairly obvious whats broke
|
||||
$c->flash( error => 'Token name not valid - Alphanumeric characters and Underscore only' );
|
||||
} elsif ( $token_rs->find({ accounttokenname => $token_name }) ) {
|
||||
$c->flash( error => 'Token Already Exists' );
|
||||
} else {
|
||||
$c->flash( success => 'Token Created' );
|
||||
|
@ -48,13 +53,20 @@ sub read {
|
|||
# POST
|
||||
sub update {
|
||||
my $c = shift;
|
||||
my $validation = $c->validation;
|
||||
$validation->required('token-name', 'trim')->like(qr/^[\w]*$/);
|
||||
$validation->required('token-used')->in( qw/ 0 1 / );
|
||||
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $token = $c->result_set->find($id) ) {
|
||||
if ( $validation->has_error ) {
|
||||
my $names = $validation->failed;
|
||||
$c->flash( error => 'Error in submitted data: ' . join(', ', @$names) );
|
||||
$c->redirect_to( '/admin/tokens/' . $id );
|
||||
} elsif ( my $token = $c->result_set->find($id) ) {
|
||||
$token->update({
|
||||
accounttokenname => $c->param('token-name'),
|
||||
used => $c->param('token-used'),
|
||||
accounttokenname => $validation->param('token-name'),
|
||||
used => $validation->param('token-used'),
|
||||
});
|
||||
$c->flash( success => 'Token Updated' );
|
||||
$c->redirect_to( '/admin/tokens/' . $id );
|
||||
|
|
Reference in a new issue