Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Accessible Laravel Package

antares/accessible

View on GitHub
Deep Wiki
Context7

How to enable / disable the constraints validation

By default, the constraints validation is enabled, but for some reason, you may want to disable it, or you will need to enable it manually. For example if you are using Symfony's forms, you may want to disable constraints validation in order to validate the entire class later.

There are two ways to enable or disable the validation: using annotations and using methods provided by this library.

Using annotations

These annotations are useful when you are sure that constraints validation will always or never be used on a class.

Disable the constraints validation on a class

To do this, simply add the [@DisableConstraintsValidation](https://github.com/DisableConstraintsValidation) annotation on the docblock of your class.

use Accessible\Annotation\DisableConstraintsValidation;

/**
 * [@DisableConstraintsValidation](https://github.com/DisableConstraintsValidation)
 */
class Foo
{
  // ...

  /**
   * Given the following constraints, $bar should only be set to a string of 3 characters or more.
   *
   * [@Access](https://github.com/Access)({Access::GET, Access::SET})
   * [@Assert](https://github.com/Assert)\Type("string")
   * [@Assert](https://github.com/Assert)\Length(min=3)
   */
  private $bar;
}

$foo = new Foo();
$foo->setBar('bar'); // This is ok
$foo->setBar('a'); // This is not ok, but it will work anyway

This example's Foo class and every class extending it will have the constraints validation disabled.

Enable the constraints validation on a class

Now let's say you want to create a class that extends Foo and uses constraints validation. To override Foo behavior, add the [@EnableConstraintsValidation](https://github.com/EnableConstraintsValidation) annotation to the child class.

use Accessible\Annotation\EnableConstraintsValidation;

/**
 * [@EnableConstraintsValidation](https://github.com/EnableConstraintsValidation)
 */
class Bar extends Foo
{

}

$bar = new Bar();
$bar->setBar('a'); // This will throw an \InvalidArgumentException

Using the provided methods

This library provides 3 methods related to constraints validation:

  • isPropertiesConstraintsValidationEnabled(): This method indicates wether the validation is enabled or not.
  • setPropertiesConstraintsValidationEnabled($enabled = true): This method enables the validation (or disables it, if $enabled is set to false).
  • setPropertiesConstraintsValidationDisabled($disabled = true): This method disables the validation (or enables it, if $disabled is set to false).

Note that setPropertiesConstraintsValidationEnabled() and setPropertiesConstraintsValidationDisabled() override the behavior you may define using [@EnableConstraintsValidation](https://github.com/EnableConstraintsValidation) and [@DisableConstraintsValidation](https://github.com/DisableConstraintsValidation).

These methods are typically useful in cases such as in the Symfony's forms example. Here is an example of how to use these methods with forms.

The entity:

class User
{
  use AutomatedBehaviorTrait;

  /**
   * [@Access](https://github.com/Access)({Access::GET, Access::SET})
   * [@Assert](https://github.com/Assert)\NotNull
   * [@Assert](https://github.com/Assert)\Email
   */
  private $email;
}

The controller:

public function newAction(Request $request)
{
  $user = new User();
  $user->setPropertiesConstraintsValidationDisabled(); // disable the validation for this object

  $form = $this->createFormBuilder($user)
    ->add('email', EmailType::class)
    ->add('save', SubmitType::class, array('label' => 'Create User'))
    ->getForm();

  $form->handleRequest($request);

  if ($form->isSubmitted() && $form->isValid()) {
    // ... perform some action, such as saving the user to the database

    return $this->redirectToRoute('user_success');
  }

  return $this->render('default/new.html.twig', array(
    'form' => $form->createView(),
  ));
}

Specify the default behavior

By default, constraints validation is enabled. This behavior can be changed by changing the global configuration:

Configuration::setConstraintsValidationEnabled(false);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager