Symfony 2.0 validator, Blank() doesn't work - validation

I am new on Symfony and I meet the following problem.
I'd like to generate a form without class.
I want to add a Blank() validator on one field.
See below.
class searchPropertyType extends AbstractType
{
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'keywords' => new blank()
));
return array('validation_constraint' => $collectionConstraint);
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('keywords')
->add('neighborhood')
->add('price_min')
->add('price_max')
->add('type')
->add('date_from' , 'date')
->add('date_to' , 'date')
;
}
public function getName()
{
return 'searchProperty';
}
}
The form is properly displayed but still, I can't send the form, I got a HTML5 alert saying that I must fill out this field.
ANy idea? I have been working on that the full day and it drives me crazy.
Thank you so much if you have time to help ;-)

To disable HTML5 client side validation add 'required' => false to getDefaultOptions:
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'keywords' => new blank()
));
return array(
'validation_constraint' => $collectionConstraint,
'required' => false
);
}

public function buildForm(FormBuilder $builder, array $options) {
$builder
->add('neighborhood','text',array('required' => false,))
->add('price_min','text',array('required' => false,))
->add('date_from', 'date', array('widget' => 'single_text', 'format' => 'dd MMM yyyy', 'required' => false))
);
}
Add required=>false
Hope this helps.

Related

an added value of array of request disappears in Laravel Controller

the user id is existed Before doing create. so it causes an error in the first one.
I made it the other way. the second one below works correctly.
I would like to know why the first one is wrong and it's gone.
//Error
public function store(ContactRequest $request)
{
$request->user_id = $request->user()->id;
Log::debug($request->user()->id);
Log::debug($request);
Contact::create($request->all());
}
//OK
public function store(ContactRequest $request,Contact $contact)
{
$request->user_id = $request->user()->id;
$contact->title = $request->title;
$contact->body = $request->body;
$contact->user_id = $request->user()->id;
$contact->save();
}
the log of the first one is here.
What happened to the user_id!?
[2020-05-30 15:59:10] local.DEBUG: 59
[2020-05-30 15:59:10] local.DEBUG: array (
'_token' => 'gGWuxW6C2JRSCYDuCAC9HauynGclKQEQB7qUh6Rw',
'title' => 'TITLE',
'body' => 'MESSAGE',
'action' => 'SEND',
)
Contact is model class.
ContactRequest is here.
class ContactRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|max:100',
'body' => 'required|max:1000'
];
}
public function attributes() {
return [
'title' => 'title',
'body' => 'CONTENTS'
];
}
}
You will have to use $request->merge(['user_id'=>$request->user()->id]).
Another tips is that you can simply use Auth::user()->id which also return the user id of current user.
What if you do this:
Auth::user() - >contact($request->all()) - >save() ;
Or also as an experiment:
$contact = new Contact($request->all()) ;
$contact->user_id = Auth::user() - >id;
$contact->save() ;
Actually the second snippet will surely work. The first one I did not test though it looks nice. :)

Drupal 8 Form alter the ajax callback is not working

I am getting my ajax callback in normal custom form, but on form alter its not working.
function sample_ajax_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id === 'node_sampleajax_form' || $form_id === 'node_sampleajax_edit_form') {
$form['field_nametrain']= array(
'#title' => t('training name'),
'#type' => 'select',
'#options' => _load_training(),
'#required' => FALSE,
'#ajax' => [
'callback' => [$this, 'changeOptionsAjax'],
// 'callback' => '::changeOptionsAjax',
'wrapper' => 'second_field_wrapper',
],
);
$form['field_namedomain'] = [
'#type' => 'select',
'#title' => t('Domain program'),
'#options' => $this->getOptions($form_state),
'#prefix' => '<div id="second_field_wrapper">',
'#suffix' => '</div>',
];
return $form;
}
}
function _load_training() {
$training = array('- Select domain training -');
$query = db_select("node__field_trainingname", "a");
$query->fields("a", array('field_trainingname_value', 'entity_id'));
$query->orderBy("a.field_trainingname_value");
$result = $query->execute();
while($row = $result->fetchObject()){
$training[$row->entity_id] = $row->field_trainingname_value;
}
return $training;
}
function changeOptionsAjax(array &$form, FormStateInterface $form_state) {
return $form['field_namedomain'];
}
function getOptions(array &$form, FormStateInterface $form_state) {
$cvvv = $form_state->getValue('field_nametrain');
<!-- return ["shgsh", $form_state->get(['field_nametrain'])]; -->
$options = array('- Select subdomain category -');
$query = db_select("node__field_trainingname", "a");
$query->fields("a", array('field_trainingname_value', 'entity_id'));
$query = db_select("node__field_cms", "b");
$query->fields("b", array('field_cms_value', 'entity_id'));
$query->join("node__field_trainingname", "b", "b.entity_id=a.entity_id");
$query->condition("a.entity_id", $cvvv);
$result = $query->execute();
while($row = $result->fetchObject()){
$options[$row->entity_id] = $row->field_cms_value;
}
return $options;
}
On using $this->getOptions($form_state) it represent the error log it is not an object and throws website encounter error in front end. But on custom form no error came only in formalter it throws error.
Kindly suggest me ideas to apply in form_alter of Drupal 8
The .module file, where your form alter hook is located, is not a class, therefore there is no $this. Your custom form however is a class (usually in your_module/src/Form/YourForm.php), that's why it works there but not in the .module file.
Further reading: http://www.php.net/manual/en/language.oop5.basic.php
and What does the variable $this mean in PHP?
In your case you should be able to just call
'#options' => getOptions($form, $form_state),
And more on a side note: I would strongly recommend to do some code refactoring.
In your custom submit handler, firt get the form object from the form state.
$formObj = $formState->getFormObject();
then call submitForm() on the form object and pass the form and form state variables.
$formObj->submitForm($form, $formState);
and finally, you just need to simply trigger the save() function on the object.
$formObj->save($form, $formState);
So the whole solution is something like
function YOR_CUSTOM_SUBMIT_HANLDLER(array $form, FormStateInterface $form_state) {
/** #var Drupal\user\RegisterForm $entity */
$formObj = $form_state->getFormObject();
$formObj->submitForm($form, $form_state);
$formObj->save($form, $form_state);
}

Yii issue retrieving relational data

I'm create a new table(s) in my Yii application thats going to be used as a relational table, just storing IDs, it's really simple, but i cannot manage to actually get any information passed through from the model, even though the IDs match. Can anyone see where i'm going wrong?
User Model (has many relations, but i've just included the one im having a problem with)
public function relations() {
return array(
'onsiteGroup' => array(self::HAS_MANY, 'EventAttendeesGroups', 'user_id'),
);
}
EventAttendeeGroups Model
class EventAttendeesGroups extends CActiveRecord
{
public function tableName()
{
return '{{event_attendees_groups}}';
}
public function rules()
{
return array(
array('user_id, group_id', 'required'),
array('user_id, group_id', 'numerical', 'integerOnly'=>true),
array('user_id, group_id', 'safe'),
array('id, user_id, group_id', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'onsiteGroupName' => array(self::BELONGS_TO, 'EventAttendeesGroupName', 'group_id'),
);
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'user_id' => 'User',
'group_id' => 'Group',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('group_id',$this->group_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
My Data is correct, the user id exists in the user table and in the EventAttendeeGroups table, yet if try the following in the user view gridview table, i get 'Not Set'
array(
'name' => 'group_id',
'type' => 'raw',
'value' => $model->onsiteGroup->group_id,
),
Can anyone see where i'm going wrong???
Are you getting an error? What is it outputting? Are you trying to render CGridView or what, and if yes is it formed?
One thought, are you sure
$model->onsiteGroup->group_id
is the object name and not
$model->onsiteGroupName->group_id
Just a thought.

Override / remove server-side validation in Symfony2.5

Say I have the following form builder buildForm method:
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'travelTime',
'datetime',
array(
'label' => 'Time',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'invalid_message' => 'Please enter a valid date and time',
)
)
->add(
'acmeEntity',
'entity',
array(
'label' => 'Acme Entity:',
'class' => 'AcmeBundle:MyEntity',
'expanded' => false,
'multiple' => false,
)
)
}
How can I override (or remove) validation for the 'acmeEntity' form field (and only that field) so that if I call:
$form->handleRequest($request);
$form->isValid();
in a Controller, then acmeEntity will not be included in the validation that determines whether $form->isValid() returns true? I've tried adding constraints => false, to the field options, but I'm receiving this error message:
Notice: Trying to get property of non-object in /var/www/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php line 67
Does anyone know the proper way to disable server-side validation for a Symfony form field?
EDIT:
Note that I am not looking for how to disable validation completely. This can be done by adding:
// Disable form validation
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
$event->stopPropagation();
}, 900); // Always set a higher priority than ValidationListener
to the bottom of a form builder.
Rather, I want to know how to completely disable validation for a single form field. Thanks everyone and good hunting!
You can define a custom form type for your entity and use the 'validation_groups' => false. This should disable the validation only for this field.
The custom form type may look like that:
// .../Your/OwnBundle/Form/Type/AcmeEntityType.php
namespace Acme\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AcmeEntityType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => false
));
}
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'acme_entity';
}
}
You can then use:
$builder
->add(
'travelTime',
'datetime',
array(
'label' => 'Time',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'invalid_message' => 'Please enter a valid date and time',
)
)
->add(
'acmeEntity',
'acme_entity',
array(
'label' => 'Acme Entity:',
'class' => 'AcmeBundle:MyEntity',
'expanded' => false,
'multiple' => false,
)
)
}
I assume that you call buildForm() from MyEntityType extending AbstractType so just use the options resolver by adding the function setDefaultOptions() in your type as said in the symphony doc here
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => false,
));
}
You can try this, I have used it in the past :
Create a validation group for your MainEntity
Add this validation group to your form
Do not add this validation group to AcmeEntity

ZF2 + Duplicate Form Validation on composite key

I have a Form having primary key on two fields (gid, bid). I need to add validation to block duplicate entries into database.
I have checked with ZF2 Solution for this . http://framework.zend.com/manual/2.2/en/modules/zend.validator.db.html#excluding-records . While this approach of handling composite keys is not look the ideal way, But still I am trying it because it look like only buil-in way. Now it require me to provide second field's value (value option in exclude), which is again a problem. As I am trying it
$inputFilter->add(array(
'name' => 'gid',
'required' => true,
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
'isEmpty' => 'required'
),
),
),
array (
'name' => 'Zend\Validator\Db\NoRecordExists',
'options' => array (
'table' => 'gtable',
'field' => 'gid',
'adapter' => $this->dbAdapter,
'messages' => array(
\Zend\Validator\Db\NoRecordExists::ERROR_RECORD_FOUND => 'The specified key already exists in database'
),
'exclude' => array(
'field' => 'bid',
'value' => [?],
),
)
),
)
));
How do I get this value, As Form is absolute separate Class/File than controller where I have the submitted form values. Is some better architecture solution of this problem exists Or Some hack to pass submitted field value to Form Class is only solution ?
Note : I am not in favor of Build My Validation Plugin for this task as short time is constraint for functionality.
You can do all the job in your form. To achieve that, you could define your forms as factories in your module Module.php.
Module.php
use MyNamespace\MyForm;
//NOTE THAT THE SERVICE MANAGER IS INJECTED. YOUR FORM COULD RECEIVE IT THROUGH THE CONSTRUCTOR
public function getServiceConfig()
{
return array(
'factories' => array(
'my_form' => function( $sm ) {
$form = new MyForm( $sm );
return $form;
},
),
);
}
When you want to use the form is as easy as use this code in your controller:
class MyController extends AbstractActionController
{
public function createAction() {
$form = $this->getServiceLocator()->get( 'my_form' ) );
(...)
}
}
And your MyForm.php
use Zend\Form\Form;
class MyForm extends Form
{
public $serviceManager, $request, $postData;
public function __construct( $serviceManager ) {
parent::__construct( null );
$this->serviceManager = $serviceManager;
$this->request = $serviceManager->get( 'Application')->getMvcEvent()->getRequest();
$this->postData = get_object_vars( $this->request->getPost() );
}
}
This way you can get advantage of the Service Manager within your form. And the public postData, where you'll find the bid value you're looking for to build your NoRecordExists filter.
You could add the parameters to the getInputFilter, like this :
getInputFilter($gid, $bid)
And then on the controller, when you set the filter you pass the 2 parameters, and then just check as $form->isValid(); ...
Alternative try this:
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'table' => 'gtable',
'field' => 'gid',
'adapter' => $this->dbAdapter,
),
),
I'm unsure on your use case. If you were to add a database entry the primary keys for that table would not be known until you insert anyway - If you have foreign key constraints you could handle the exception from the database.
I am not in favor of Build My Validation Plugin for this task
The validator is also not designed to validate multiple fields as they are attached to a form element on a 1-1 basis. You will therefore need to create your own.
The below example has NOT been tested, so take it as an example of the approach rather than working code.
The key bit is the isValid method.
namespace MyModule\Validator\Db;
use Zend\Validator\Db\NoRecordExists;
class CompositeNoRecordExists extends NoRecordExists
{
protected $field2;
protected $field2Value;
public function __construct($options = null)
{
parent::__construct($options);
if (array_key_exists('field2', $options)) {
$this->setField2($options['field2']);
} else {
throw new \BadMethodCallException('Missing field2 option!');
}
}
protected function setField2Value(array $context)
{
if (! isset($context[$this->field2])) {
throw new \BadMethodCallException('Unable to find value for field 2');
}
$this->field2Value = $context[$this->field2];
}
public function isValid($value)
{
// The isValid() method is actually given a 2nd argument called $context
// Which is injected by the inputFilter, via the input and into the validator chain
// $context contains all of RAW form element values, keyed by thier element name.
// Unfortunately due to the ValidatorInterface you are unable to add this to the method
// signature. So you will need to be 'creative':
$args = func_get_args();
if (isset($args[1]) && is_array($args[1])) {
$this->setField2Value($args[1]);
} else {
throw new \BadMethodCallException('Missing validator context');
}
return parent::isValid($value);
}
public function getSelect()
{
$select = parent::getSelect();
$select->where->equalTo($this->field2, $this->field2Value);
return $select;
}
}
Then all you would need to do is update the validator config, adding the field2 field name.
array (
'name' => 'MyModule\Validator\Db\CompositeNoRecordExists',
'options' => array (
'table' => 'gtable',
'field' => 'gid',
'field2' => 'bid',
'adapter' => $this->dbAdapter,
'messages' => array(
\Zend\Validator\Db\NoRecordExists::ERROR_RECORD_FOUND => 'The specified key already exists in database'
),
)
),

Resources