Symfony\Component\Validator\Constraint::getDefaultOption PHP Method

getDefaultOption() public method

Override this method to define a default option.
See also: __construct()
public getDefaultOption ( ) : string
return string
    public function getDefaultOption()
    {
        return null;
    }

Usage Example

 public function validate($value, Constraint $constraint)
 {
     $usernameErrors = array();
     $roleNameErrors = array();
     $workspace = $constraint->getDefaultOption();
     $wsRoleNames = array();
     $workspaceRoles = $this->roleManager->getRolesByWorkspace($workspace);
     foreach ($workspaceRoles as $workspaceRole) {
         $wsRoleNames[] = $workspaceRole->getTranslationKey();
     }
     $data = $this->ut->formatCsvOutput(file_get_contents($value));
     $lines = str_getcsv($data, PHP_EOL);
     foreach ($lines as $line) {
         $linesTab = explode(';', $line);
         $nbElements = count($linesTab);
         if (trim($line) !== '' && $nbElements !== 2) {
             $this->context->addViolation($constraint->message);
             return;
         }
     }
     foreach ($lines as $i => $line) {
         if (trim($line) !== '') {
             $datas = explode(';', $line);
             $username = $datas[0];
             $roleName = $datas[1];
             $user = $this->userManager->getOneUserByUsername($username);
             if (is_null($user)) {
                 $msg = $this->translator->trans('workspace_user_invalid', array('%username%' => $username, '%line%' => $i + 1), 'platform') . ' ';
                 $usernameErrors[] = $msg;
             }
             if (!in_array($roleName, $wsRoleNames)) {
                 $msg = $this->translator->trans('line_number', array('%line%' => $i + 1), 'platform') . ' ';
                 $msg .= $this->translator->trans('unavailable_role', array('%translationKey%' => $roleName), 'platform');
                 $roleNameErrors[] = $msg;
             }
         }
     }
     foreach ($usernameErrors as $error) {
         $this->context->addViolation($error);
     }
     foreach ($roleNameErrors as $error) {
         $this->context->addViolation($error);
     }
 }
All Usage Examples Of Symfony\Component\Validator\Constraint::getDefaultOption