Jump to content

Form API and FieldtypeOptions


blacksrv
 Share

Recommended Posts

Hi everyone, I'm having a problem with a form I'm creating using the api.

The form consist on multiple text fields, one file, and an Options field, everything works fine, except for the options field.

The form creates fine, the main problem is on submit.

I'm getting this error: Warning: Invalid argument supplied for foreach() in C:\Local\htdocs\wire\modules\Fieldtype\FieldtypeOptions\FieldtypeOptions.module on line 277

 

I already tried to check the type and use $post_value->id but it's not working.

My code:

if($input->post->submit_save) {
    // user submitted the form, process it and check for errors
    $form->processInput($input->post);
	$errors = array();
	$required_fields = array();
	$fields = array();
	$post_value = $input->post->{$f->name};
    switch ( $f->type ) {
        case 'text':
            $the_value = $sanitizer->text( $post_value );
            break;
        case 'textarea':
            $the_value = $sanitizer->textarea( $post_value );
            break;
        case 'checkbox':
            $the_value = isset( $post_value ) ? 1 : 0;
            break;
        default:
            $the_value = $post_value;
            break;
    }
    
    $fields[$f->name] = $the_value;

	foreach ( $fields as $key => $value ) {
		$page->$key = $value;
	}

	$page->of(false);
	$page->save();
}

Any Ideas?

Thanks

Link to comment
Share on other sites

Without running your code I have just a question: Are you sure that $f->type is a string ? really sure it's not an object ?

Have to say that options field is a little bit weird.

$options = $f->getOptions();
foreach ($options as $key => $option) {
    if ($field->isOptionSelected($key)) {
        $the_value = $option;
        break;
    }
}
Link to comment
Share on other sites

Without running your code I have just a question: Are you sure that $f->type is a string ? really sure it's not an object ?

Have to say that options field is a little bit weird.

$options = $f->getOptions();
foreach ($options as $key => $option) {
    if ($field->isOptionSelected($key)) {
        $the_value = $option;
        break;
    }
}

Thanks for your help, I just tested with 

if(!is_object($f->type)) {

The same happens.

Link to comment
Share on other sites

Just an FYI - you only need one of these - they are identical.

Some nit-picking: There not 100% identical.

/**
 * @return bool Last output formatting value
 */
$of = $page->of(false);
$page->field = "value";
$page->save();
$page->of($of); // Reset output formatting

/**
 * @return obj Returns the page object for chaining
 */
$page->setOutputFormatting(false)->set("field", "value")->save();
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...