setRules
setRules(array $rules)
setRules
method is to combine multiple setRule methods into one. The syntax is simple. You just need to provide an array with the below form:
[
'fieldName' => 'fieldName',
'rule' => 'rule',
'parameters' => 'parameters'
]
For example:
$crud->setRules(
[
[
'fieldName' => 'creditLimit',
'rule' => 'min',
'parameters' => '100'
],
[
'fieldName' => 'postalCode',
'rule' => 'lengthBetween',
'parameters' => ['4','6']
],
]
);
The above code is equivalent to:
$crud->setRule('creditLimit', 'min', '100');
$crud->setRule('postalCode', 'lengthBetween', ['4','6']);
For more information about the setRule please read the setRule method
A full working example can be found below:
$crud->setTable('customers');
$crud->setSubject('Customer', 'Customers');
$crud->columns(['customerName','phone','addressLine1','creditLimit']);
$crud->setRules(
[
[
'fieldName' => 'creditLimit',
'rule' => 'min',
'parameters' => '100'
],
[
'fieldName' => 'postalCode',
'rule' => 'lengthBetween',
'parameters' => ['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.