本文整理汇总了PHP中Symfony\Component\Form\FormBuilder::setAttribute方法的典型用法代码示例。如果您正苦于以下问题:PHP FormBuilder::setAttribute方法的具体用法?PHP FormBuilder::setAttribute怎么用?PHP FormBuilder::setAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Form\FormBuilder
的用法示例。
在下文中一共展示了FormBuilder::setAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: buildForm
public function buildForm(FormBuilder $builder, array $options)
{
$sonataAdmin = array(
'name' => null,
'admin' => null,
'value' => null,
'edit' => 'standard',
'inline' => 'natural',
'field_description' => null,
'block_name' => false
);
$builder->setAttribute('sonata_admin_enabled', false);
if ($options['sonata_field_description'] instanceof FieldDescriptionInterface) {
$fieldDescription = $options['sonata_field_description'];
$sonataAdmin['admin'] = $fieldDescription->getAdmin();
$sonataAdmin['field_description'] = $fieldDescription;
$sonataAdmin['name'] = $fieldDescription->getName();
$sonataAdmin['edit'] = $fieldDescription->getOption('edit', 'standard');
$sonataAdmin['inline'] = $fieldDescription->getOption('inline', 'natural');
$builder->setAttribute('sonata_admin_enabled', true);
}
$builder->setAttribute('sonata_admin', $sonataAdmin);
}
示例2: getBuilder
protected function getBuilder($name = 'name', $propertyPath = null)
{
$builder = new FormBuilder($name, $this->factory, $this->dispatcher);
$builder->setAttribute('property_path', new PropertyPath($propertyPath ?: $name));
$builder->setAttribute('error_mapping', array());
return $builder;
}
示例3: buildForm
public function buildForm(FormBuilder $builder, array $options)
{
$builder->setAttribute('url', $options['url']);
$builder->setAttribute('url_params', $options['url_params']);
$builder->setAttribute('callback', $options['callback']);
$builder->setAttribute('select_callback', $options['select_callback']);
parent::buildForm($builder, $options);
}
示例4: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
$changemonth = $options['changeMonth'];
$changeyear = $options['changeYear'];
$mindate = $options['minDate'];
$maxdate = $options['maxDate'];
$buttonImage = $options['buttonImage'];
$builder->setAttribute('changemonth', $changemonth);
$builder->setAttribute('changeyear', $changeyear);
$builder->setAttribute('mindate', $mindate);
$builder->setAttribute('maxdate', $maxdate);
$builder->setAttribute('buttonImage', $buttonImage);
parent::buildForm($builder, $options);
}
示例5: buildForm
public function buildForm(FormBuilder $builder, array $options)
{
$formatter = new \IntlDateFormatter(\Locale::getDefault(), $options['format'], \IntlDateFormatter::NONE, \DateTimeZone::UTC);
if ($options['widget'] === 'single-text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE));
} else {
$yearOptions = $monthOptions = $dayOptions = array();
$widget = $options['widget'];
if ($widget === 'choice') {
// Only pass a subset of the options to children
$yearOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT));
$monthOptions = array('choice_list' => new MonthChoiceList($formatter, $options['months']));
$dayOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT));
}
$builder->add('year', $widget, $yearOptions)->add('month', $widget, $monthOptions)->add('day', $widget, $dayOptions)->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day')));
}
if ($options['input'] === 'string') {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d')));
} else {
if ($options['input'] === 'timestamp') {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone'])));
} else {
if ($options['input'] === 'array') {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day'))));
} else {
if ($options['input'] !== 'datetime') {
throw new FormException('The "input" option must be "datetime", "string", "timestamp" or "array".');
}
}
}
}
$builder->setAttribute('formatter', $formatter)->setAttribute('widget', $options['widget']);
}
示例6: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
$formatter = new \IntlDateFormatter(\Locale::getDefault(), $options['format'], \IntlDateFormatter::NONE, \DateTimeZone::UTC);
if ($options['widget'] === 'single_text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE));
} else {
$yearOptions = $monthOptions = $dayOptions = array();
if ($options['widget'] === 'choice') {
if (is_array($options['empty_value'])) {
$options['empty_value'] = array_merge(array('year' => null, 'month' => null, 'day' => null), $options['empty_value']);
} else {
$options['empty_value'] = array('year' => $options['empty_value'], 'month' => $options['empty_value'], 'day' => $options['empty_value']);
}
// Only pass a subset of the options to children
$yearOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT), 'empty_value' => $options['empty_value']['year'], 'required' => $options['required']);
$monthOptions = array('choice_list' => new MonthChoiceList($formatter, $options['months']), 'empty_value' => $options['empty_value']['month'], 'required' => $options['required']);
$dayOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT), 'empty_value' => $options['empty_value']['day'], 'required' => $options['required']);
}
$builder->add('year', $options['widget'], $yearOptions)->add('month', $options['widget'], $monthOptions)->add('day', $options['widget'], $dayOptions)->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day')));
}
if ($options['input'] === 'string') {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d')));
} else {
if ($options['input'] === 'timestamp') {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone'])));
} else {
if ($options['input'] === 'array') {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day'))));
}
}
}
$builder->setAttribute('formatter', $formatter)->setAttribute('widget', $options['widget']);
}
示例7: buildForm
/**
* Adds a CSRF field to the form when the CSRF protection is enabled.
*
* @param FormBuilder $builder The form builder
* @param array $options The options
*/
public function buildForm(FormBuilder $builder, array $options)
{
if (!$options['csrf_protection']) {
return;
}
// use a low priority so higher priority listeners don't remove the field
$builder->setAttribute('csrf_field_name', $options['csrf_field_name'])->setAttribute('csrf_provider', $options['csrf_provider'])->setAttribute('csrf_intention', $options['intention'])->setAttribute('csrf_factory', $builder->getFormFactory())->addEventSubscriber(new CsrfValidationListener($options['csrf_field_name'], $options['csrf_provider'], $options['intention']));
}
示例8: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
$parts = array('hour', 'minute');
$format = 'H:i';
if ($options['with_seconds']) {
$format = 'H:i:s';
$parts[] = 'second';
}
if ('single_text' === $options['widget']) {
$builder->appendClientTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format));
} else {
$hourOptions = $minuteOptions = $secondOptions = array();
if ('choice' === $options['widget']) {
if (is_array($options['empty_value'])) {
$options['empty_value'] = array_merge(array('hour' => null, 'minute' => null, 'second' => null), $options['empty_value']);
} else {
$options['empty_value'] = array('hour' => $options['empty_value'], 'minute' => $options['empty_value'], 'second' => $options['empty_value']);
}
$hours = $minutes = array();
foreach ($options['hours'] as $hour) {
$hours[$hour] = str_pad($hour, 2, '0', STR_PAD_LEFT);
}
foreach ($options['minutes'] as $minute) {
$minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT);
}
// Only pass a subset of the options to children
$hourOptions = array('choices' => $hours, 'empty_value' => $options['empty_value']['hour']);
$minuteOptions = array('choices' => $minutes, 'empty_value' => $options['empty_value']['minute']);
if ($options['with_seconds']) {
$seconds = array();
foreach ($options['seconds'] as $second) {
$seconds[$second] = str_pad($second, 2, '0', STR_PAD_LEFT);
}
$secondOptions = array('choices' => $seconds, 'empty_value' => $options['empty_value']['second']);
}
// Append generic carry-along options
foreach (array('required', 'translation_domain') as $passOpt) {
$hourOptions[$passOpt] = $minuteOptions[$passOpt] = $options[$passOpt];
if ($options['with_seconds']) {
$secondOptions[$passOpt] = $options[$passOpt];
}
}
}
$builder->add('hour', $options['widget'], $hourOptions)->add('minute', $options['widget'], $minuteOptions);
if ($options['with_seconds']) {
$builder->add('second', $options['widget'], $secondOptions);
}
$builder->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], $parts, 'text' === $options['widget']));
}
if ('string' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'H:i:s')));
} elseif ('timestamp' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone'])));
} elseif ('array' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts)));
}
$builder->setAttribute('widget', $options['widget'])->setAttribute('with_seconds', $options['with_seconds']);
}
示例9: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
if ($options['choice_list'] && !$options['choice_list'] instanceof ChoiceListInterface) {
throw new FormException('The "choice_list" must be an instance of "Symfony\\Component\\Form\\Extension\\Core\\ChoiceList\\ChoiceListInterface".');
}
if (!$options['choice_list']) {
$options['choice_list'] = new ArrayChoiceList($options['choices']);
}
if ($options['expanded']) {
// Load choices already if expanded
$choices = $options['choice_list']->getChoices();
// Flatten choices
$flattened = array();
foreach ($choices as $value => $choice) {
if (is_array($choice)) {
$flattened = array_replace($flattened, $choice);
} else {
$flattened[$value] = $choice;
}
}
$options['choices'] = $flattened;
foreach ($options['choices'] as $choice => $value) {
if ($options['multiple']) {
$builder->add((string) $choice, 'checkbox', array('value' => $choice, 'label' => $value, 'required' => false));
} else {
$builder->add((string) $choice, 'radio', array('value' => $choice, 'label' => $value));
}
}
}
// empty value
if ($options['multiple'] || $options['expanded']) {
// never use and empty value for these cases
$emptyValue = null;
} elseif (false === $options['empty_value']) {
// an empty value should be added but the user decided otherwise
$emptyValue = null;
} elseif (null === $options['empty_value']) {
// user did not made a decision, so we put a blank empty value
$emptyValue = $options['required'] ? null : '';
} else {
// empty value has been set explicitly
$emptyValue = $options['empty_value'];
}
$builder->setAttribute('choice_list', $options['choice_list'])->setAttribute('preferred_choices', $options['preferred_choices'])->setAttribute('multiple', $options['multiple'])->setAttribute('expanded', $options['expanded'])->setAttribute('required', $options['required'])->setAttribute('empty_value', $emptyValue);
if ($options['expanded']) {
if ($options['multiple']) {
$builder->appendClientTransformer(new ArrayToBooleanChoicesTransformer($options['choice_list']));
} else {
$builder->appendClientTransformer(new ScalarToBooleanChoicesTransformer($options['choice_list']))->addEventSubscriber(new FixRadioInputListener(), 10);
}
} else {
if ($options['multiple']) {
$builder->appendClientTransformer(new ArrayToChoicesTransformer());
} else {
$builder->appendClientTransformer(new ScalarToChoiceTransformer());
}
}
}
示例10: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
if ($options['allow_add'] && $options['prototype']) {
$prototype = $builder->create('$$' . $options['prototype_name'] . '$$', $options['type'], $options['options']);
$builder->setAttribute('prototype', $prototype->getForm());
}
$listener = new ResizeFormListener($builder->getFormFactory(), $options['type'], $options['options'], $options['allow_add'], $options['allow_delete']);
$builder->addEventSubscriber($listener)->setAttribute('allow_add', $options['allow_add'])->setAttribute('allow_delete', $options['allow_delete']);
}
示例11: buildForm
/**
* Adds a CSRF field to the form when the CSRF protection is enabled.
*
* @param FormBuilder $builder The form builder
* @param array $options The options
*/
public function buildForm(FormBuilder $builder, array $options)
{
if (!$options['csrf_protection']) {
return;
}
$listener = new EnsureCsrfFieldListener($builder->getFormFactory(), $options['csrf_field_name'], $options['intention'], $options['csrf_provider']);
// use a low priority so higher priority listeners don't remove the field
$builder->setAttribute('csrf_field_name', $options['csrf_field_name'])->addEventListener(FormEvents::PRE_SET_DATA, array($listener, 'ensureCsrfField'), -10)->addEventListener(FormEvents::PRE_BIND, array($listener, 'ensureCsrfField'), -10);
}
示例12: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
$type = $options['date_type'];
// unset($options['date_type']);
$opt = $this->defDateOption();
$opt['format'] = $options['format'];
$builder->add('start', $type, $opt)->add('end', $type, $opt);
$builder->setAttribute('widget', $options['widget']);
}
示例13: buildForm
public function buildForm(FormBuilder $builder, array $options)
{
if (!$options['choices'] && !$options['choice_list']) {
throw new FormException('Either the option "choices" or "choice_list" is required');
}
if ($options['choice_list'] && !$options['choice_list'] instanceof ChoiceListInterface) {
throw new FormException('The "choice_list" must be an instance of "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface".');
}
if (!$options['choice_list']) {
$options['choice_list'] = new ArrayChoiceList($options['choices']);
}
if ($options['expanded']) {
// Load choices already if expanded
$options['choices'] = $options['choice_list']->getChoices();
foreach ($options['choices'] as $choice => $value) {
if ($options['multiple']) {
$builder->add((string)$choice, 'checkbox', array(
'value' => $choice,
'label' => $value,
// The user can check 0 or more checkboxes. If required
// is true, he is required to check all of them.
'required' => false,
));
} else {
$builder->add((string)$choice, 'radio', array(
'value' => $choice,
'label' => $value,
));
}
}
}
$builder->setAttribute('choice_list', $options['choice_list'])
->setAttribute('preferred_choices', $options['preferred_choices'])
->setAttribute('multiple', $options['multiple'])
->setAttribute('expanded', $options['expanded']);
if ($options['expanded']) {
if ($options['multiple']) {
$builder->appendClientTransformer(new ArrayToBooleanChoicesTransformer($options['choice_list']));
} else {
$builder->appendClientTransformer(new ScalarToBooleanChoicesTransformer($options['choice_list']));
$builder->addEventSubscriber(new FixRadioInputListener(), 10);
}
} else {
if ($options['multiple']) {
$builder->appendClientTransformer(new ArrayToChoicesTransformer());
} else {
$builder->appendClientTransformer(new ScalarToChoiceTransformer());
}
}
}
示例14: getBuilder
private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
{
$builder = new FormBuilder($name, $dataClass, $this->dispatcher, $this->factory);
$builder->setPropertyPath(new PropertyPath($propertyPath ?: $name));
$builder->setAttribute('error_mapping', array());
$builder->setErrorBubbling(false);
$builder->setMapped(true);
return $builder;
}
示例15: buildForm
public function buildForm(FormBuilder $builder, array $options)
{
if (empty($options['validation_groups'])) {
$options['validation_groups'] = null;
} else {
$options['validation_groups'] = is_callable($options['validation_groups']) ? $options['validation_groups'] : (array) $options['validation_groups'];
}
$builder->setAttribute('validation_groups', $options['validation_groups'])->setAttribute('validation_constraint', $options['validation_constraint'])->setAttribute('cascade_validation', $options['cascade_validation'])->addValidator(new DelegatingValidator($this->validator));
}