2

This is my current code.

$rules = array(
    'label'        => 'required',
    'short_name'   => 'required',
);

$result = ee('Validation')->make($rules)->validate($_POST);

Now I want it like this:

$rules = array(
   'label'        => 'required',
   'short_name'   => 'required|trim|check_name["test_name"]',
);

but I am getting error in trim rule.

Unknown validation rule trim.

Please help.

2

You are getting error because trim function is not defined in current validation library. You need to add your own function in EE3 before you run validation.

According to the documentation:

You first need to create an object of Validation class.

$validator = ee('Validation')->make();

Then you need to define your custom rule:

$validator->defineRule('trim', function($key, $value, $parameters)
{
  return ** Depends on result return TRUE or Error Message **;
});

Then you need to use that rule with setRules.

$validator->setRules(array(
   'age' => 'required|trim'
));

Final step is to validate your post data:

$result = $validator->validate($data);

$result will either contains TRUE or object of errors.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.