setRule

setRule(string $fieldName, string $rule[, array $parameters])

The setRule is the validation rule that you add for the insert/update form. We wanted to use a very simple to use library and we chose Valitron. You can see all parameters that you can add for valitron for the rule method at: https://github.com/vlucas/valitron. Have in mind that the fieldName however comes first and the rules as a second parameter. We did that on purpose in order to have consistency with all the GroceryCRUD functions.

$crud->setRule('nickname', 'slug')

Important notice: The only difference that the GroceryCrud setRule is having by the rule of Valitron is that if you need a 4rd parameter then you will need to add it as an array. This was the only proper way that grocery CRUD could handle it. Valitron is using func_get_args() and GroceryCRUD enterprise is avoiding using that as it is easy to lose control of the parameters (a lesson well learned from grocery CRUD community edition).

So for example instead of the Valitron rule:

rule('fieldName', 'lengthBetween', '4','6');

you should write it instead:

$crud->setRule('fieldName', 'lengthBetween', ['4', '6'])

If you don't need a 4rd parameter then you can keep using it as the Valitron is. For example:

$crud->setRule('credit_card', 'creditCard', ['visa', 'mastercard'])

Valitron as library is very simplistic and powerful at the same time. You can create your own custom rules with an easy way. So for example if you need to create a custom rule you can easily do it with the below steps:

Step1. Create the custom rule in your function. In our example let's say that we want to have a custom validation that a phone is acceptable only if it will start with +30. In our case we will add this before the $crud->render() is called. We are suggesting to add it at the beginning of your function. So in our example we will have:

\Valitron\Validator::addRule('checkGreekPhoneNumber', function($field, $value, array $params, array $fields) {

    if (strstr($value, '+30')) {
        return true;
    }

    return false;
}, 'must start with +30');

Have in mind that as Grocery CRUD is automatically adding the label, you don't need to add the name of the field name.

Step2. Add the extra rule for the field that you need. For example in our case:

$crud->setRule('mobile_phone', 'checkGreekPhoneNumber');
$crud->setRule('home_phone', 'checkGreekPhoneNumber');

At the above example the way that this will work is that when a field is inserted or updated then the custom set rule will take place. Just to make it more full as an example this is how your code will look like:

\Valitron\Validator::addRule('checkGreekPhoneNumber', function($field, $value, array $params, array $fields) {

    if (strstr($value, '+30')) {
        return true;
    }

    return false;
}, 'must start with +30');
...

$crud->setTable('customers');
...
$crud->displayAs('mobile_phone', 'Mobile');
$crud->displayAs('home_phone', 'Home Number');
...
$crud->setRule('mobile_phone', 'checkGreekPhoneNumber');
$crud->setRule('home_phone', 'checkGreekPhoneNumber');
...
$output = $crud->render();

So at the above example if the mobile_phone for example will not start with the string '+30' (or in other words the validation will return false) then an error will be displayed at the screen with the message: "Mobile must start with +30"

The rules are very simple to use. You can see a full example below:

$crud->setTable('customers');
$crud->setSubject('Customer', 'Customers');
$crud->columns(['customerName','phone','addressLine1','creditLimit']);

$crud->setRule('creditLimit', 'min', '100');
$crud->setRule('postalCode', 'lengthBetween', ['4','6']);

$output = $crud->render();

You can try the validation rules below. Try to add a creditLimit lower than 100 or try to add a postalCode that doesn't have length between 4-6 digits. As you will also notice, empty values are acceptable. In case we need a validation of a field to be required although you can use the Valitron required, it is recommended to use the GroceryCRUD function requiredFields instead of the rule.