diff --git a/tests/php/test_api_tag_name_required.php b/tests/php/test_api_tag_name_required.php new file mode 100644 index 0000000000..6929693ae8 --- /dev/null +++ b/tests/php/test_api_tag_name_required.php @@ -0,0 +1,52 @@ +validate[$field] as $rule) { + $required = isset($rule['required']) ? $rule['required'] : false; + if (!array_key_exists($field, $data)) { + if ($required === true or ($required === 'create' and $create)) return false; + continue; + } + $r = $rule['rule']; + $name = is_array($r) ? $r[0] : $r; + $args = is_array($r) ? array_slice($r, 1) : array(); + if ($name !== 'notBlank' and $name !== 'custom') continue; + if (!call_user_func_array(array('Validation', $name), array_merge(array($data[$field]), $args))) return false; + } + return true; +} + +$model = new Tag(); + +check(!accepts($model, 'Name', array(), true), "add without Name is refused"); +check(!accepts($model, 'Name', array('Name' => ''), true), "add with a blank Name is refused"); +check(!accepts($model, 'Name', array('Name' => ' '), true), "add with only spaces in Name is refused"); +check(accepts($model, 'Name', array('Name' => 'Dog'), true), "add with a non-blank Name is accepted"); +check(accepts($model, 'Name', array(), false), "an edit may leave Name out"); +check(accepts($model, 'Name', array('Name' => 'Dog'), false), "an edit may keep the stored Name"); +check(!accepts($model, 'Name', array('Name' => ''), false), "an edit may not blank Name"); + +exit($failures ? 1 : 0); diff --git a/web/api/app/Model/Tag.php b/web/api/app/Model/Tag.php index a0556bd154..f6205fe7a4 100644 --- a/web/api/app/Model/Tag.php +++ b/web/api/app/Model/Tag.php @@ -37,7 +37,9 @@ class Tag extends AppModel { public $validate = array( 'Name' => array( 'notBlank' => array( - 'rule' => array('notBlank')), + 'rule' => array('notBlank'), + 'required' => 'create', + 'message' => 'A tag needs a name'), 'unique' => array( 'rule' => array('uniqueIgnoringCase'), 'message' => 'A tag with that name already exists')),