Frage

I am now working on implementing a validator but I have a problem and do not want to fall in DRY. I will try to explain clearly...

When someone submits a form, I have the properties of each field that will be checked to return TRUE|FALSE.

eg:

$post = array (
         'name' => $_POST['name'] ,
         'body' => $_POST['body']
         );

$rule = array (
         'name' => 'required|min:2' ,
         'body' => 'required|min:3'
         );

$v = Validator::make($post , $rule);

1: submit.php
check all fields

2: check_ajax.php
verify certain field

Applying this same form an individual check of each field using ajax, how can I solve the problem of 'DRY' and avoid reperir the same rules?

I'm days looking for a solution but not got success.
thank you

War es hilfreich?

Lösung

$rule = array (
         'name' => 'required|min:2' ,
         'body' => 'required|min:3'
         );

$post = array();
foreach ($name, $value in $rule) {
    $post[$name] = isset($_POST, $name) ? $_POST[$name] : '';
}

$v = Validator::make($post , $rule);

Have'nt use php for years, the syntax not right, but you got the idea.

For client side validation, no better way, need do it again. But I like to write code generator to generate both server side and client side validation code.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top